Combine power of PHP_CodeSniffer and PHP CS Fixer in 3 lines

Found a typo? Edit me
This post was updated at August 2020 with fresh know-how.
What is new?

Updated with ECS 5, Neon to YAML migration and checkers to services migration.
Updated ECS YAML to PHP configuration since ECS 8.


PHP_CodeSniffer has over 5 381 stars on Github and 210 default sniffs, PHP CS Fixer with 6 467 stars brings you 160 fixers.

Both powerful tools dealing with coding standards with huge communities behind them. Can you imagine using them both and actually enjoy it? Today I will show you how.

Right to The Answer

Let's say we want to check arrays. We add first checker that requires short PHP 5.4 [] syntax:

use PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();
    $services->set(DisallowLongArraySyntaxSniff::class);
};

Great start. Then we want to check for trailing commas, so every line has them.

So add one more checker:

use PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff;
use PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();
    $services->set(DisallowLongArraySyntaxSniff::class);
    $services->set(TrailingCommaInMultilineArrayFixer::class);
};

Great job! You have just combined PHP_CodeSniffer and PHP CS Fixer in 3 lines.

With a help of ECS. Now, when title promise is fulfilled, I will show how to install it, run it and how nice and clear reports it generates.

How to add ECS in 3 steps

1. Install Package

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

2. Configure

Create a ecs.php file in your project and desired checkers as above.

You can add a comment to groups, so everyone can easily orientate when there are more checkers.

Be Lazy with PHP

Do you use PHPStorm? Just use PHP to autocomplete everything as you're used to since ECS 8.

No more looking to documentation, what string matches what sniff or fixer, if there are any checkers for arrays or debugging typos.

3. Run it & Fix it

vendor/bin/ecs check src

# ...

vendor/bin/ecs check src --fix

That's all for short ECS intro.

Do you want to know more? Learn how to write own sniff or even better - a fixer.

Happy coding!


Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.