Combine power of PHP_CodeSniffer and PHP CS Fixer in 3 lines
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 short PHP 5.4
# ecs.yml services: PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff: ~
Great start. Then we want to check for trailing commas, so every line has them.
So add one more checker:
# ecs.yml services: PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff: ~ 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 symplify/easy-coding-standard --dev
ecs.yml file in your project and desired checkers.
services: # arrays PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff: ~ 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.
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 like what I write about? Or do you hate it but enjoy discussion? 😠
You can support my writing by throwing a couple bucks at my Patreon