Install the package in your Laravel project:
composer require --dev innmind/coding-standard
Create .php_cs.dist at your project root with the basic config:
<?php
return \Innmind\CodingStandard\CodingStandard::config(['app', 'routes', 'src', 'tests']);
(Adjust paths to match your Laravel structure.)
Run the fixer once to auto-correct:
vendor/bin/php-cs-fixer fix
.git/hooks/pre-commit:
#!/bin/sh
vendor/bin/php-cs-fixer fix --dry-run --diff
(Fails if changes are needed, enforcing consistency before commits.)Team Onboarding
.php_cs.dist via Git (or template it in vendor/).composer install + php-cs-fixer fix to align their IDE.CI Integration
# .github/workflows/php-cs.yml
jobs:
coding-standard:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/php-cs-fixer fix --dry-run --diff --allow-risky=yes
(Fail builds if fixes are needed, but allow risky changes in CI.)
Partial Fixes
vendor/bin/php-cs-fixer fix app/Http/Controllers --rules=@Innmind
CodingStandard::config([
'app', 'routes', 'src',
'tests',
], [
'vendor/*',
'bootstrap/cache/*',
'storage/framework/*',
]);
php-cs-fixer as a PHPStan/PSR-12 rule provider in PHPStorm:
<!-- php-cs-fixer.xml.dist -->
<config cacheFile=".php_cs.cache">
<rule ref="Innmind"/>
</config>
Rule Conflicts
return spacing)..php_cs.dist or use --rules to override:
vendor/bin/php-cs-fixer fix --rules=@PSR12,@Innmind
Performance
php-cs-fixer.--cache-file=.php_cs.cache and exclude heavy directories.False Positives
Route::group nesting)..php_cs.dist:
CodingStandard::config([], [
'rules' => [
'@Innmind' => true,
'no_unused_imports' => false, // Disable for Laravel's Facades
],
]);
--dry-run first to preview changes:
vendor/bin/php-cs-fixer fix --dry-run --diff
vendor/bin/php-cs-fixer fix -v
// .php_cs.dist
return CodingStandard::config(['app'], [
'rules' => [
'Innmind\\Rules\\ClassAlignment' => true,
'custom_rule' => MyCustomRule::class,
],
]);
--parallel (requires PHP 8.1+):
vendor/bin/php-cs-fixer fix --parallel=4
use statements for Facades.
Solution: Whitelist Laravel’s Facades in .php_cs.dist:
CodingStandard::config([], [
'exclude' => [
'rules' => ['no_unused_imports' => ['App\\Providers\\RouteServiceProvider' => true]],
],
]);
How can I help you explore Laravel packages today?