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.
Let's say we want to check arrays.
We add first checker that requires long
services: PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff: ~
Great start. Then we want to check for trailing commas, so every line has them.
So add one more checker:
services: PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff: ~ PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer: ~
Great job! You have just combined PHP_CodeSniffer and PHP CS Fixer in 3 lines.
With a help of EasyCodingStandard. Now, when title promise is fulfilled, I will show how to install it, run it and how nice and clear reports it generates.
composer require --dev symplify/easy-coding-standard
easy-coding-standard.yml file in your project and desired checkers.
services: # arrays PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff: ~ PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer: ~
You can add a comment to groups, so everyone can easily orientate when there are more checkers.
Do you use PHPStorm? If so, you can use Symfony Plugin. It allows you one amazing thing:
It autocompletes class names!
No more looking to documentation, what string matches what sniff or fixer, if there are any checkers for arrays or debugging typos.
vendor/bin/ecs check src # ... vendor/bin/ecs check src --fix
Well, unless you like videos...
I spoke about ECS last week in Dresden. If you have 11 minutes and you want to know more about it, go watch it here it here.
That's all for short intro of this tool. I'll post more articles about how to use it, about fast caching or how to write own checkers - both sniffs and fixers.
Thank you for any feedback. Here or in the issues.
Do you want more on this topic? You might like these related posts: How to Write Custom Sniff for Code Sniffer 3, How to Write Custom Fixer for PHP CS Fixer 2.4, How PHP Coding Standard Tools Actually Work