The Easiest Way to Use Any Coding Standard
Features
- Blazing fast parallel run
- Use PHP_CodeSniffer || PHP-CS-Fixer - anything you like
- 2nd run under few seconds with un-changed file cache
- Skipping files for specific checkers
- Prepared sets - PSR-12, arrays, use statements, spaces and more... - see
SetList
class for all - Prefixed version by default to allow install without conflicts on any PHP 7.2+ project
Are you already using another tool?
Install
composer require symplify/easy-coding-standard --dev
Usage
1. Create Configuration
- Create an
ecs.php
in your root directory
vendor/bin/ecs init
2. Setup Sets and Chekcers
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
return static function (ECSConfig $ecsConfig): void {
// A. full sets
$ecsConfig->sets([SetList::PSR_12]);
// B. standalone rule
$ecsConfig->ruleWithConfiguration(ArraySyntaxFixer::class, [
'syntax' => 'short',
]);
};
3. Run in CLI
# dry run
vendor/bin/ecs check src
# fix
vendor/bin/ecs check src --fix
Configuration
Configuration can be extended with many options. Here is list of them with example values and little description what are they for:
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
return static function (ECSConfig $ecsConfig): void {
// alternative to CLI arguments, easier to maintain and extend
$ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);
// bear in mind that this will override SetList skips if one was previously imported
// this is result of design decision in symfony https://github.com/symfony/symfony/issues/26713
$ecsConfig->skip([
// skip paths with legacy code
__DIR__ . '/packages/*/src/Legacy',
ArraySyntaxFixer::class => [
// path to file (you can copy this from error report)
__DIR__ . '/packages/EasyCodingStandard/packages/SniffRunner/src/File/File.php',
// or multiple files by path to match against "fnmatch()"
__DIR__ . '/packages/*/src/Command',
// generics paths
'*Sniff.php',
],
// skip rule completely
ArraySyntaxFixer::class,
// just single one part of the rule?
ArraySyntaxFixer::class . '.SomeSingleOption',
// ignore specific error message
'Cognitive complexity for method "addAction" is 13 but has to be less than or equal to 8.',
]);
// scan other file extensions; [default: [php]]
$ecsConfig->fileExtensions(['php', 'phpt']);
// configure cache paths & namespace - useful for Gitlab CI caching, where getcwd() produces always different path
// [default: sys_get_temp_dir() . '/_changed_files_detector_tests']
$ecsConfig->cacheDirectory('.ecs_cache');
// [default: \Nette\Utils\Strings::webalize(getcwd())']
$ecsConfig->cacheNamespace('my_project_namespace');
// indent and tabs/spaces
// [default: spaces]
$ecsConfig->indentation('tab');
// [default: PHP_EOL]; other options: "\n"
$ecsConfig->lineEnding("\r\n");
};
Parallel Run
ECS runs in X parallel threads, where X is number of your threads.
Do you have 16 threads? That will speed up the process from 2,5 minutes to 10 seconds.
This process is enabled by default. To disable it, use disableParallel()
method:
use Symplify\EasyCodingStandard\Config\ECSConfig;
return static function (ECSConfig $ecsConfig): void {
$ecsConfig->disableParallel();
};
Coding Standards in Markdown
How to correct PHP snippets in Markdown files?
vendor/bin/ecs check-markdown README.md docs/rules.md
# to fix them, add --fix
vendor/bin/ecs check-markdown README.md docs/rules.md --fix
FAQ
How do I clear cache?
vendor/bin/ecs check src --clear-cache
How to load Custom Config?
vendor/bin/ecs check src --config another-config.php
Acknowledgment
The parallel run is heavily inspired by phpstan/phpstan-src by Ondřej Mirtes. Thank you.