In the require-dev
section of composer.json
change the version constraint:
- "lmc/coding-standard": "^3.3",
+ "lmc/coding-standard": "^4.0",
Then run composer update
.
The configuration now uses ECSConfig
class instead of ContainerConfigurator
.
Update your ecs.php
file to use the new configuration style:
-use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
+use Symplify\EasyCodingStandard\Config\ECSConfig;
-return static function (ContainerConfigurator $containerConfigurator): void {
+return ECSConfig::configure()
+ ->withSets([
+ __DIR__ . '/vendor/lmc/coding-standard/ecs.php',
+ ]);
// ...
Now change the way you set rules, skip tests and import sets:
Old Method | New Method |
---|---|
$services->set() |
ECSConfig::configure()->withRules([]) or ECSConfig::configure()->withConfiguredRule() |
$parameters->set(Option::SKIP, ...) |
ECSConfig::configure()->withSkip() |
$containerConfigurator->import() |
ECSConfig::configure()->withSets() |
See examples in Usage section of our README or more configuration options in ECS documentation.
Some more reasoning and examples of configurations can also be seen in ECS author blogpost.
->withSets(__DIR__ . '/vendor/lmc/coding-standard/ecs.php')
- ->withSets(__DIR__ . '/vendor/lmc/coding-standard/ecs-7.4.php')
- ->withSets(__DIR__ . '/vendor/lmc/coding-standard/ecs-8.0.php')
- ->withSets(__DIR__ . '/vendor/lmc/coding-standard/ecs-8.1.php')
All the rules are now included in the main ecs.php
file. Only rules compatible with your PHP version are applied.
Paths definition could now be included directly in ecs.php
instead of repeating them on command line.
In ecs.php
:
// ...
return ECSConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) // optionally add 'config' or other directories with PHP files
->withRootFiles() // to include ecs.php and all other php files in the root directory
// ...
Now you can remove the explicit paths definition from composer.json
:
{
"scripts": {
"analyze": [
- "vendor/bin/ecs check --ansi src/ tests/"
+ "vendor/bin/ecs check --ansi"
],
"fix": [
- "vendor/bin/ecs check --ansi --fix src/ tests/"
+ "vendor/bin/ecs check --ansi --fix"
]
}
}
Or run directly from command line without a need of specifying them:
$ vendor/bin/ecs check --ansi src/ tests/ # old
$ vendor/bin/ecs check --ansi # new
On top of default rules included in ecs.php, there are some more opinionated ones you may want to add.
These suggested rules are listed in README.md.
Some of the new default fixers introduced in php-coding-standard 4.0 and 4.1 suggest changes, which - if not thoughtfully reviewed - can change the code behavior. Especially changes introduced by (but not limited to!):
- PhpdocToPropertyTypeFixer + PropertyTypeHintSniff
- PhpdocToParamTypeFixer + ParameterTypeHintSniff
- PhpdocToReturnTypeFixer + ReturnTypeHintSniff
Always carefully review the changes suggested by all fixers! You may want to skip some of the checks
(using withSkip()
) in the first phase of upgrading to the new version of the coding standard
or you can introduce some of the rules gradually or on a file-by-file basis.
Besides running your code style checks, you can ensure all predefined checks are loaded by running:
vendor/bin/ecs list-checkers
With php-coding-standard
4.1, the result should end with something like this:
40 checkers from PHP_CodeSniffer:
...
171 checkers from PHP-CS-Fixer:
...
2 checkers are skipped:
...
(or some close number, depending on your custom code-style settings).