friendsofphp/php-cs-fixer
PHP CS Fixer automatically fixes PHP code to match coding standards. Use built-in rule sets like @PER-CS, @Symfony, or @PhpCsFixer, or define your own config. Helps modernize code for newer PHP and PHPUnit. Supports PHP 7.4–8.5.
Installation:
composer require --dev friendsofphp/php-cs-fixer
For dependency conflicts, use the shim:
composer require --dev php-cs-fixer/shim
Initialize Config:
Generate a basic .php-cs-fixer.dist.php config file:
./vendor/bin/php-cs-fixer init
This creates a Symfony-style config (recommended for Laravel projects).
First Run: Fix all files in your project:
./vendor/bin/php-cs-fixer fix src tests config
Or dry-run with:
./vendor/bin/php-cs-fixer check src
laravel-pint or Git hooks to auto-fix before commits.# .github/workflows/ci.yml
- name: PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff
Project-Wide Standardization:
Use the @Symfony rule set (Laravel’s default) or @PhpCsFixer for stricter rules:
// .php-cs-fixer.dist.php
return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'no_unused_imports' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
]);
Laravel-Specific Customizations:
composer.json scripts:
"scripts": {
"cs-fix": "php-cs-fixer fix src app config --allow-risky=yes"
}
use statements:
'fully_qualified_strict_types' => true,
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => true],
Parallel Processing: Leverage the default parallel runner for large codebases (e.g., Laravel monorepos):
./vendor/bin/php-cs-fixer fix --parallel
--stop-on-violation to fail fast:
./vendor/bin/php-cs-fixer fix --stop-on-violation --parallel
Rule Sets for Migrations:
Use @autoPHPMigration to modernize legacy PHP (e.g., mt_rand → random_int):
return PhpCsFixer\Config::create()
->setRules([
'@autoPHPMigration' => true,
'random_api_migration' => true,
]);
laravel/pint with PHP-CS-Fixer for granular control:
composer require --dev friendsofphp/php-cs-fixer --dev
composer remove laravel/pint
'no_unused_imports' => true,
'ordered_imports' => ['sort_algorithm' => 'custom', 'imports_order' => ['const', 'class', 'function']],
Risky Rules:
@auto:risky in CI initially—test locally first. Example:
./vendor/bin/php-cs-fixer fix --rules=@auto:risky --dry-run
static_lambda may break legacy closures in service containers.False Positives:
no_unused_imports: Exclude Laravel’s dynamic Facades:
'no_unused_imports' => true,
'exclude' => ['app/Providers/AppServiceProvider.php'],
ordered_imports: Conflicts with Laravel’s use Illuminate\Support\Facades\* patterns. Use:
'ordered_imports' => ['sort_algorithm' => 'alpha', 'case_sensitive' => false],
Performance:
./vendor/bin/php-cs-fixer fix --parallel --processes=2
php -d memory_limit=2G ./vendor/bin/php-cs-fixer fix
Config Conflicts:
.php-cs-fixer.dist.php vs .php-cs-fixer.php: The latter overrides the former. Commit .dist.php to version control.--diff to debug:
./vendor/bin/php-cs-fixer fix --diff --dry-run
./vendor/bin/php-cs-fixer fix -v
'no_unused_imports' => false,
git diff to review changes:
git diff --name-only | xargs ./vendor/bin/php-cs-fixer fix --dry-run
Custom Rule Sets:
Create a project-specific set in .php-cs-fixer.dist.php:
return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'laravel_rules' => true, // Custom rule (see below)
]);
Define in composer.json:
"extra": {
"php-cs-fixer": {
"rules": {
"laravel_rules": {
"no_unused_imports": true,
"fully_qualified_strict_types": true,
"ordered_class_elements": ["order" => ["use_trait", "case"]]
}
}
}
}
PHPStan Integration: Combine with PHPStan for static analysis:
composer require --dev phpstan/phpstan
./vendor/bin/phpstan analyse --level=5
./vendor/bin/php-cs-fixer fix
Editor Plugins:
junstyle/vscode-php-cs-fixer for on-save fixing.'no_unused_imports' => true,
'exclude' => [
'app/Providers/FacadeServiceProvider.php',
'routes/web.php',
],
'risky_rules' => ['@autoPHPMigration:risky' => true],
'exclude' => ['database/migrations/*.php'],
--path-mode=overlay to skip:
./vendor/bin/php-cs-fixer fix --path-mode=overlay --exclude=resources/views
How can I help you explore Laravel packages today?