How to use PHP-CS-Fixer ruleset with Easy Coding Standard
This post is a quick guide on how to use the @Symfony
ruleset from PHP CS Fixer with Easy Coding Standard, since this ruleset is not shipped anymore with ECS.
What is Easy Coding Standard?
Easy Coding Standard is a tool that helps you to check your code against PHP coding standards.
The main advantage of Easy Coding Standard is that it wraps PHP CS Fixer and PHP_CodeSniffer to provide a unified interface for both tools, meaning that you can easily use both tools with the same configuration file and a single command.
It also comes with some ruleset out of the box, and even if they are great, there is no Symfony related ruleset anymore, so we need to find a way to use the @Symfony
ruleset from PHP CS Fixer.
Using PHP CS Fixer ruleset with Easy Coding Standard
Assuming that you already have Easy Coding Standard installed and configured through the following commands:
composer require --dev symplify/easy-coding-standard
./vendor/bin/ecs
composer require --dev symplify/easy-coding-standard
./vendor/bin/ecs
2
You will need to update your ecs.php
configuration file to use the FixerFactory
and RuleSet
classes from PHP CS Fixer, and manually register PHP CS Fixer fixers through the ECSConfig#rule
and ECSConfig#ruleWithConfiguration
methods:
<?php
declare(strict_types=1);
use PhpCsFixer\FixerFactory;
use PhpCsFixer\RuleSet\RuleSet;
use Symplify\EasyCodingStandard\Config\ECSConfig;
return function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
// Configure Symfony and Symfony Risky SetList from PHP-CS-Fixer, since they are not shipped anymore with Easy Coding Standard.
$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixerFactory->useRuleSet($ruleSet = new RuleSet([
'@Symfony' => true,
// You can also enable the risky ruleset if you want.
//'@Symfony:risky' => true,
]));
foreach ($fixerFactory->getFixers() as $fixer) {
if (null !== $fixerConfiguration = $ruleSet->getRuleConfiguration($fixer->getName())) {
$ECSConfig->ruleWithConfiguration($fixer::class, $fixerConfiguration);
} else {
$ECSConfig->rule($fixer::class);
}
}
};
<?php
declare(strict_types=1);
use PhpCsFixer\FixerFactory;
use PhpCsFixer\RuleSet\RuleSet;
use Symplify\EasyCodingStandard\Config\ECSConfig;
return function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
// Configure Symfony and Symfony Risky SetList from PHP-CS-Fixer, since they are not shipped anymore with Easy Coding Standard.
$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixerFactory->useRuleSet($ruleSet = new RuleSet([
'@Symfony' => true,
// You can also enable the risky ruleset if you want.
//'@Symfony:risky' => true,
]));
foreach ($fixerFactory->getFixers() as $fixer) {
if (null !== $fixerConfiguration = $ruleSet->getRuleConfiguration($fixer->getName())) {
$ECSConfig->ruleWithConfiguration($fixer::class, $fixerConfiguration);
} else {
$ECSConfig->rule($fixer::class);
}
}
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
And voilà! You can now run Easy Coding Standard with the Symfony ruleset from PHP CS Fixer.