2017 © Pedro Peláez
 

library easy-coding-standard

Use Coding Standard with 0-knowledge of PHP-CS-Fixer and PHP_CodeSniffer.

image

symplify/easy-coding-standard

Use Coding Standard with 0-knowledge of PHP-CS-Fixer and PHP_CodeSniffer.

  • PHP
  • 85 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 100 Versions
  • 48 % Grown

The README.md

The Easiest way to use Coding Standard

Downloads total, (*1)


, (*2)

Killer Features


, (*3)

Install

composer require symplify/easy-coding-standard --dev


, (*4)

Usage

vendor/bin/ecs

On the first run, ECS creates ecs.php config file with directories and first rule to kick off., (*5)

Then you can run again to see the suggested diffs:, (*6)

vendor/bin/ecs

To actually fix your code, add --fix:, (*7)

vendor/bin/ecs --fix

That's it!, (*8)


, (*9)

Configure

Most of the time, you'll be happy with the default configuration. The most relevant part is configuring paths, checkers and sets:, (*10)

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withConfiguredRule(
        ArraySyntaxFixer::class,
        ['syntax' => 'long']
    )
    ->withRules([
        ListSyntaxFixer::class,
    ])
    ->withPreparedSets(psr12: true);


, (*11)

Do you want to check all *.php files in your root (ecs.php, rector.php etc.)? Instead of listing them one by one, use ->withRootFiles() method:, (*12)

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withRootFiles();


, (*13)

Do you want to include one of 44 sets from php-cs-fixer?, (*14)

You can:, (*15)

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
    ->withPhpCsFixerSets(perCS20: true, doctrineAnnotation: true);


, (*16)

How to Skip Files/Rules?

Love the sets of rules, but want to skip single rule or some files?, (*17)

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
    ->withSkip([
        // skip single rule
        ArraySyntaxFixer::class,

        // skip single rule in specific paths
        ArraySyntaxFixer::class => [
            __DIR__ . '/src/ValueObject/',
        ],

        // skip directory by absolute or * mask
        __DIR__ . '/src/Migrations',

        // skip directories by mask
        __DIR__ . '/src/*/Legacy',
    ]);


, (*18)

Less Common Options

You probably won't use these, but they can give you more control over the internal process:, (*19)

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Option;

return ECSConfig::configure()
    // file extensions to scan
    ->withFileExtensions(['php'])

    // configure cache paths and namespace - useful e.g. Gitlab CI caching, where getcwd() produces always different path
    ->withCache(
        directory: sys_get_temp_dir() . '/_changed_files_detector_tests',
        namespace: getcwd() // normalized to directory separator
    )

    // print contents with specific indent rules
    ->withSpacing(indentation: Option::INDENTATION_SPACES, lineEnding: PHP_EOL)

    // modify parallel run
    ->withParallel(timeoutSeconds: 120, maxNumberOfProcess: 32, jobSize: 20);

Mentioned values are default ones., (*20)


, (*21)

Do you use ECS across variety of project? Do you want to run them always the same way in each of those project? Let's make use of Composer scripts, (*22)

This command adds 2 handy scripts to your composer.json:, (*23)

vendor/bin/ecs scripts

Run them always the same way - to check the code:, (*24)

composer check-cs

To apply fixes, run:, (*25)

composer fix-cs


, (*26)

Controlling Output Format

You may want to use ECS to generate reports for third-party tooling., (*27)

We currently provide formatters for:, (*28)

  • console: Human-oriented printing à la PHP CS Fixer.
  • json: A custom JSON blob for arbitrary tooling.
  • junit: JUnit format to be used in different CI environments.
  • checkstyle: Useful for Github Action Reports.
  • gitlab: For Gitlab code quality reports or Code Climate tooling.

For information on how each of these behave, refer to their respective implementations., (*29)


, (*30)

FAQ

How do I clear cache?

vendor/bin/ecs --clear-cache

How can I see all used rules?

vendor/bin/ecs list-checkers

Do you look for json format?, (*31)

vendor/bin/ecs list-checkers --output-format json


, (*32)

How to Migrate from another coding standard tool?

Do you use another tool and want to migrate? It's pretty straightforward - here is "how to":, (*33)

The Versions