How to reference a PHP CodeSniffer ruleset in EasyCodingStandard

This simple addition can make it much easier to migrate between tools

Joel Clermont
Joel Clermont

I'm a big fan of PHP Code Sniffer, and it's what we use on all our projects. But recently, there was a rule I wanted that was only available in PHP CS Fixer.

Switching from one tool to the other just for this one rule was too much work, but it gave me a nudge to instead try out Easy Coding Standards (ECS). This tool sits above both PHP Code Sniffer and PHP CS Fixer, allowing you to leverage rules from both projects.

My goal was to switch to ECS, but bring in our existing rules, and only adding this one new rule from the other project. Unfortunately, ECS had a different defintion of the PSR12 standard than PHP Code Sniffer did, leading to a large formatting diff.

I was able to figure out how to instead import the PSR12 ruleset from PHP Code Sniffer into ECS. Here's what I added to my ecs.php config file:

$codeSnifferConfig = new PHP_CodeSniffer\Config(["--standard=PSR12"]);
$codeSnifferRuleset = new PHP_CodeSniffer\Ruleset($codeSnifferConfig);

    // our existing PSR12 set from PHP Code Sniffer

    // and the two new rules I wanted from PHP CS Fixer

This simple addition got me exactly what I wanted, and allowed me to switch over to ECS without a giant diff. The fact that ECS uses PHP-based configuration instead of XML or YAML was also a nice selling-point.

Here to help,


P.S. We're full of good ideas! If you haven't downloaded our free Laravel tips ebook yet, go check it out.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.