craftcms/ecs
Easy Coding Standard (ECS) config presets for Craft CMS projects and plugins. PSR-12 aligned with a few Craft-specific tweaks. Install via Composer, add an ecs.php selecting Craft 3 or 4 set, then run ecs check (optionally --fix).
Installation Add the package via Composer:
composer require --dev craftcms/ecs
Ensure PHP_CodeSniffer (squizlabs/php_codesniffer) and ECS (symplify/easy-coding-standard) are also installed:
composer require --dev squizlabs/php_codesniffer symplify/easy-coding-standard
First Run Execute ECS with the default Craft CMS ruleset:
vendor/bin/ecs check src
Fix issues automatically (if desired):
vendor/bin/ecs fix src
Key Files
ecs.php: Main configuration file (auto-generated by the package)..php_cs: PHP_CodeSniffer config (optional, but often used alongside ECS).Pre-Commit Hooks Use ECS in a Git pre-commit hook to enforce standards before code is committed:
composer require --dev php-cs-fixer
echo 'vendor/bin/ecs check src --parallel' >> .git/hooks/pre-commit
Tip: Use ecs check --parallel for faster execution on large codebases.
CI/CD Pipeline Add ECS to your CI pipeline (e.g., GitHub Actions) to block non-compliant code:
- name: Run ECS
run: vendor/bin/ecs check src --no-interaction --ansi
Custom Rulesets
Extend the default ruleset by creating a custom ecs.php:
<?php
return [
'rules' => [
\CraftCMS\EasyCodingStandard\Sniffs\Arrays\ArrayKeySpacingSniff::class,
\Symplify\EasyCodingStandard\Sniffs\Arrays\ArrayKeySpacingSniff::class,
// Add custom rules here
],
];
Team Onboarding
Document the ECS rules in your CONTRIBUTING.md or README.md to onboard new developers:
## Code Standards
Run `composer lint` to check your code against Craft CMS standards.
Performance with Large Codebases
--parallel to speed up checks:
vendor/bin/ecs check src --parallel
ecs.php:
return [
'exclude' => [
'vendor/**',
'storage/**',
'web/assets/**',
],
];
Conflicting Rules
ecs.php:
return [
'rules' => [
\Symplify\EasyCodingStandard\Sniffs\Arrays\ArrayKeySpacingSniff::class => false,
],
];
Fixing vs. Checking
ecs fix may not handle all edge cases (e.g., complex logic). Review changes manually after auto-fixing.--dry-run to preview changes:
vendor/bin/ecs fix src --dry-run
Caching Issues
vendor/bin/ecs clear-cache
Custom Sniffs Add project-specific sniffs by extending the ruleset:
return [
'imports' => [
__DIR__ . '/rules/CustomSniff.php',
],
];
Partial Fixes Fix specific files without running the full suite:
vendor/bin/ecs fix src/plugins/MyPlugin/services/MyService.php
IDE Integration
Use PHPStorm’s "Reformat Code" with ECS by configuring the php-cs-fixer plugin to use ECS rules.
Version Pinning
Pin the ECS package version in composer.json to avoid rule changes across updates:
"require-dev": {
"craftcms/ecs": "^1.0"
}
Debugging Rules
Use --verbose to debug why a rule is failing:
vendor/bin/ecs check src --verbose
How can I help you explore Laravel packages today?