symplify/easy-coding-standard
Easy Coding Standard (ECS) makes PHP coding standards effortless on PHP 7.2–8.5. Fast parallel runs, supports PHP_CodeSniffer and PHP-CS-Fixer, uses prepared rule sets, generates ecs.php config on first run, and can check and auto-fix code with --fix.
Installation:
composer require --dev symplify/easy-coding-standard
Add to composer.json under require-dev:
"extra": {
"laravel": {
"start": "php artisan serve",
"ecs": {
"config": "ecs.php"
}
}
}
Initialize Config:
vendor/bin/ecs init
This generates ecs.php with default PSR-12 rules.
First Run:
vendor/bin/ecs check src/
Fix issues automatically:
vendor/bin/ecs check src/ --fix
Integrate into CI/CD (e.g., GitHub Actions):
- name: Run ECS
run: vendor/bin/ecs check src/ --no-interaction --diff
Add to composer.json scripts:
"scripts": {
"cs-check": "ecs check src/ --no-interaction --diff",
"cs-fix": "ecs check src/ --fix"
}
Project-Wide Enforcement:
// ecs.php
return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__.'/src',
__DIR__.'/app',
__DIR__.'/routes',
]);
$ecsConfig->sets([
SetList::PSR_12,
SetList::SYMFONY,
SetList::LARAVEL, // Laravel-specific rules
]);
};
Per-Module Rules (for large apps):
$ecsConfig->paths([__DIR__.'/modules/auth']);
$ecsConfig->rules([
LineEnding::class => ['line_ending' => "\n"],
NoUnusedImports::class,
]);
Custom Rulesets:
$ecsConfig->ruleWithConfiguration(
ClassNameLengthRule::class,
['max' => 30] // Custom threshold
);
Laravel-Specific Rules:
Use SetList::LARAVEL for Eloquent/Blade conventions. Extend with custom rules:
$ecsConfig->rule(NoUnusedUsesRule::class);
$ecsConfig->rule(NoUnusedImportsRule::class);
CI Optimization:
Cache results with --cache flag or use --parallel for multi-core speedups:
vendor/bin/ecs check --parallel --cache
IDE Integration:
Add to .editorconfig:
[*.php]
ecs_ignore = false
Git Hooks:
Use pre-commit hook to auto-fix:
vendor/bin/ecs check --fix --skip=tests
Parallel Mode Conflicts:
--parallel cautiously in CI; some rules (e.g., NoUnusedImports) may misbehave.--debug to log parallelization issues.Laravel-Specific Quirks:
resources/views by default:
$ecsConfig->exclude([__DIR__.'/resources/views']);
$ecsConfig->paths([__DIR__.'/app/Console/Commands']);
Rule Overrides:
PSR_12 rules unless necessary; use skip for exceptions:
$ecsConfig->skip([
__DIR__.'/app/Exceptions/Handler.php',
]);
PHP Version Mismatches:
~12.6:
composer require symplify/easy-coding-standard:^12.6
vendor/bin/ecs check --verbose
vendor/bin/ecs check --dry-run
vendor/bin/ecs check --rules=NoUnusedImports --debug
Custom Rules:
Create a rule class (e.g., app/Rules/CustomLaravelRule.php) and register it:
$ecsConfig->rule(CustomLaravelRule::class);
Dynamic Configs: Load configs from environment variables:
$ecsConfig->paths([env('ECS_PATHS', __DIR__.'/src')]);
Output Formats: Generate CI-friendly reports:
vendor/bin/ecs check --format=checkstyle > ecs-report.xml
--fix and gradually enable stricter rules via sets().ecs --version to ensure all devs use the same ECS/PHP-CS-Fixer versions.vendor/ and node_modules/ from paths to speed up runs.// app/Console/Commands/ECSCommand.php
public function handle() {
$exitCode = (new ECS())->run();
exit($exitCode);
}
How can I help you explore Laravel packages today?