lcobucci/coding-standard
PHP_CodeSniffer ruleset based on Doctrine’s coding standard with small tweaks. Install and use it in your projects to enforce consistent formatting and code style via phpcs in CI and local development.
composer require --dev lcobucci/coding-standard
composer.json under scripts:
"scripts": {
"cs-check": "phpcs --standard=vendor/lcobucci/coding-standard",
"cs-fix": "php-cs-fixer fix --rules=@lcobucci"
}
composer cs-check
Add a GitHub Actions workflow (.github/workflows/coding-standard.yml) to block PR merges on violations:
name: Coding Standard
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer install
- run: composer cs-check
php-cs-fixer):
composer cs-fix # Auto-fix violations before committing
composer cs-check app/Http/Controllers/ # Scope to a directory
app/, tests/) to speed up CI.Laravel Valet/Laravel Sail:
Add a custom command to app/Console/Kernel.php:
protected function commands()
{
$this->load(__DIR__.'/Commands/CodingStandardCheck.php');
}
Then run:
php artisan coding-standard:check
Laravel Forge/Envoyer:
Add a deploy hook to run composer cs-check before code is merged to production.
.php-cs-fixer.dist.php:
return (new PhpCsFixer\Config())
->setRules([
'@lcobucci' => true,
'array_syntax' => ['syntax' => 'short'], // Override array syntax
]);
--ignore flag or .phpcsignore:
composer cs-check --ignore=vendor/
PHP Version Mismatch:
doctrine/coding-standard directly.php.ini or use a .php-version file in your repo.PHPCS 4.0+ Dependency:
php-cs-fixer and phpcodesniffer are updated:
composer require --dev php-cs-fixer:^3.30 phpcodesniffer:^4.0
False Positives in Legacy Code:
DisallowImplicitArrayCreation) may flag legacy code. Use --ignore or .phpcsignore to exclude problematic files temporarily.Attribute Formatting:
[Route('/')]). If your team uses a different style, override it in php-cs-fixer:
'attributes' => ['order' => 'attributes'],
-v for detailed error messages:
composer cs-check -v
php-cs-fixer to auto-fix common violations:
composer cs-fix --dry-run # Preview changes
composer cs-fix # Apply fixes
--report=full to see all violations:
composer cs-check --report=full
Add Custom Rules:
Extend the standard by creating a custom PHPCS rule and merging it with lcobucci/coding-standard:
composer require --dev your/custom-phpcs-rule
Then update your PHPCS config to include it.
Modify Rule Severity: Adjust severity levels (e.g., treat warnings as errors) in your PHPCS config:
composer cs-check --warning-severity=0
Parallel Execution: Speed up CI by running PHPCS on multiple files in parallel (requires PHPCS 4.0+):
composer cs-check --parallel=8
Pair with phpstan:
Combine with phpstan/extension-installer for static analysis:
composer require --dev phpstan/phpstan
Then add to composer.json:
"scripts": {
"test": "phpstan analyse --level=5"
}
Template for New Projects:
Use this composer.json template for new Laravel projects:
{
"require-dev": {
"lcobucci/coding-standard": "^12.0",
"php-cs-fixer": "^3.30",
"phpcodesniffer": "^4.0"
},
"scripts": {
"cs-check": "phpcs --standard=vendor/lcobucci/coding-standard",
"cs-fix": "php-cs-fixer fix --rules=@lcobucci"
}
}
Git Hooks: Automate local checks with a pre-commit hook (e.g., using Husky):
composer require --dev husky
npx husky add .husky/pre-commit "composer cs-check"
How can I help you explore Laravel packages today?