定制 almacareer/coding-standard 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

almacareer/coding-standard

最新稳定版本:5.0.0

Composer 安装命令:

composer require almacareer/coding-standard

包简介

Coding standard used in Alma Career projects

README 文档

README

Latest Stable Version

PHP coding standard used in Alma Career (formerly LMC) products.

The standard is based on PSR-12 and partially PER 2.0 and adds various checks to make sure the code is readable, follows the same conventions, and does not contain common mistakes.

We use EasyCodingStandard to define and execute checks created for both PHP-CS-Fixer and PHP_CodeSniffer.

Switching from lmc/coding-standard

The package almacareer/coding-standard is 1:1 replacement for the previous deprecated lmc/coding-standard package.

⚠️ Note: Starting from version 5.0.0, the namespace has changed from Lmc\CodingStandard to AlmaOss\CodingStandard. See UPGRADE-5.0.md for detailed upgrade instructions.

To change the package, you only need to do the following changes in your project:

1. Update dependency in composer.json

-        "lmc/coding-standard": "^4.1",
+        "almacareer/coding-standard": "^5.0",

And then run composer update.

2. Change path to ecs.php in your ecs.php

You can also use SetList::ALMACAREER instead of explicitly specifying path to the file:

    ->withSets(
        [
-            __DIR__ . '/vendor/lmc-eu/coding-standard/ecs.php',
+            \AlmaOss\CodingStandard\Set\SetList::ALMACAREER,
        ]
    );

Installation

composer require --dev almacareer/coding-standard

Usage

  1. Create ecs.php file in the root directory of your project and import the code-style rules:
<?php

declare(strict_types=1);

use AlmaOss\CodingStandard\Set\SetList;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) // optionally add 'config' or other directories with PHP files
    ->withRootFiles() // to also check ecs.php and all other php files in the root directory
    ->withSets(
        [
            SetList::ALMACAREER,
        ]
    );
  1. Run the check command
vendor/bin/ecs check
  1. Optionally we recommend adding this to scripts section of your composer.json:
    "scripts": {
        "analyze": [
            "vendor/bin/ecs check --ansi",
            "[... other scripts, like PHPStan etc.]"
        ],
        "fix": [
            "...",
            "vendor/bin/ecs check --ansi --fix"
        ],
    }

Now you will be able to run the fix using composer analyze and execute automatic fixes with composer fix.

Add custom checks or override default settings

On top of the default code-style rules, you are free to add any rules from PHP-CS-Fixer or PHP_CodeSniffer. If needed, you can also override any default settings.

Below find examples of some more opinionated checks you may want to add depending on your needs:

<?php

declare(strict_types=1);

use AlmaOss\CodingStandard\Set\SetList;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    /* (...) */
    ->withSets(
        [
            SetList::ALMACAREER,
        ]
    )
    ->withRules(
        [
            // PHPUnit attributes must be used over their respective PHPDoc-based annotations. (Use with PHPUnit 10+.)
            PhpUnitAttributesFixer::class,
            // Single-line comments must have proper spacing (one space after `//`).
            SingleLineCommentSpacingFixer::class,
            // Convert multiline string to heredoc or nowdoc.
            MultilineStringToHeredocFixer::class,
        ]
    )
    // Enforce line-length to 120 characters.
    ->withConfiguredRule(LineLengthSniff::class, ['absoluteLineLimit' => 120])
    // Tests must have @test annotation.
    ->withConfiguredRule(PhpUnitTestAnnotationFixer::class, ['style' => 'annotation'])
    // Specify elements separation.
    ->withConfiguredRule(ClassAttributesSeparationFixer::class, ['elements' => ['const' => 'none', 'method' => 'one', 'property' => 'none']])
    /* (...) */

See EasyCodingStandard docs for more configuration options.

Exclude (skip) checks or files

You can configure your ecs.php file to entirely skip some files, disable specific checks, or suppress specific errors.

<?php

declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayDeclarationSniff;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    /* (...) */
    ->withSkip(
        [
            // Ignore specific check only in specific files
            ForbiddenFunctionsSniff::class => [__DIR__ . '/src-tests/bootstrap.php'],
            // Disable check entirely
            ArrayDeclarationSniff::class,
            // Skip one file
            __DIR__ . '/file/to/be/skipped.php',
            // Skip entire directory
            __DIR__ . '/ignored/directory/*',
        ]
    )
    /* (...) */

See EasyCodingStandard docs for more configuration options.

IDE integration

For integration with PHPStorm and other IDEs, follow instructions in EasyCodingStandard README.

Changelog

For the latest changes, see CHANGELOG.md file. This library follows Semantic Versioning.

License

This library is open-source software licensed under the MIT license.

统计信息

  • 总下载量: 22.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 8
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-28