New in Easy Coding Standard 4: Clean Symfony Standard with Yaml and Services

This post was updated at August 2020 with fresh know-how.
What is new?

Updated ECS YAML to PHP configuration since ECS 8.


I wrote about news in Easy Coding Standard 3 a while ago. EasyCodingStandard 4 is released yet (still in alpha), but soon you'll be able to use all the news I'll show you today.

And what are they? Neon to YAML, semi-static to Services, customizable caching, even simpler skipper, short bin and more.

1. Configure Caching Directory

Check the PR #661

Docker users will be happy for this feature, since it makes ECS much more usable. To enjoy speed of caching of changed files on second run, just tune your config.

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();
    $parameters->set('cache_directory', '.ecs_cache');
};

Thank you Marcin Michalski for adding this feature.


2. Skip Anything, Anywhere

Check the PR #661

One of the features I really like is skipping particular spots. PHP CS Fixer and PHP_CodeSniffer can ignore whole directory, 1 sniff everywhere or force to put annotation to your code and that's not the way to go. Your code should have no idea about tools you use to analyze it.

What you really need? Exclude 1 file but only for 1 checker. Or 1 checker for group of files and sometimes only 1 code from sniff on 1 file. That all is possible now.

Because details matters and it's pointless to think about code or class, you can now remove skip_codes key from your config and use skip section only.


3. Short vendor/bin/ecs is the King

Check the PR #647

One last detail. Did you use this bin file to run ECS?

vendor/bin/easy-coding-standard
# or
vendor/bin/easy-coding-standard.php

I know it's pain, mainly during live demo presentations with all that tyops :).

Now this is the only way to use ECS:

vendor/bin/ecs

Typo proof or at least less error prone. Just change it in you composer.json's script section or CI setups and you're ready to go!


4. DI Migration Finished: From Neon to YAML

Check the PR #651

Symplify used Nette\DI a long time ago and with it its markup language - Neon. Then it moved to Symfony\DependencyInjection in Symplify 2.0, because it was just impossible to reject all these awesome Symfony 3.3 features by Nicolas Grekas. But this was just partial migration - Neon files still worked.

That lead to situation, where 5 custom-cool-classes simulated loading transforming Neon to YAML format, merging it and then passing to Symfony Container, hoping all went well. And it worked. Well, most of the times.

Based on feedback from community around Symplify, rejection of ECS in Doctrine\CodingStandard where Neon was one of reasons and weird feeling from promoting "local-only standard", I decided to move to Symfony completely.

I had one problem - missed services autocomplete in Yaml files. But you know what they say:

There are no solutions. There are only trade-offs

I hear you community, so lets trade! From ECS 4, you can use Yaml everywhere with syntax you know, behavior from Symfony ecosystem you know and with no need to learn new standard.

How to Migrate?

Well just rename easy-coding-standard.neon or ecs.yml and then read about it in Neon vs. Yaml and How to Migrate Between Them.


5. From Semi-Static Checkers to Services as First-Class Citizen

Note: Symplify 8 now uses PHP configuration.

Check the PR #660

Thanks to Yaml, we could use finally use full power of Symfony\DependencyInjection component, constructor injection, autowiring... again, all that you probably already know from Symfony.

Why? ECS is basically a Symfony application with DI Container. It loads all checkers from config you provide, turns them into services and then uses those services to check the code.

YAML was the only missing part to do this. And ECS has it now, so does the explicit services! And you can do and use any feature you Symfony know. Magic no more #metoo.



Happy upgrading!




Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!