fabpot/php-cs-fixer
Automatically fix PHP coding standards issues and unify style across your codebase. Includes rule sets like PER-CS, Symfony, and PhpCsFixer, plus configurable rules and migrations to modern PHP and PHPUnit. Supports PHP 7.4–8.5.
phpunit, pint, psalm) without requiring architectural changes.@PSR12, @Symfony, @PhpCsFixer) and custom configurations, allowing alignment with Laravel’s existing standards (e.g., Taylor Otwell’s conventions).composer.json workflow../vendor/bin/php-cs-fixer fix src tests --rules=@PSR12 --dry-run
pre-commit hooks (via husky or laravel-git) to enforce fixes before commits.NoUnusedImports) may over-optimize or break legacy code. Requires testing in a staging environment before enforcing in CI..php-cs-fixer.cache).--parallel flag.vendor/ and generated files (e.g., bootstrap/cache).@property-read) may conflict with PHP-CS-Fixer’s defaults. Solution: Extend the config to override specific rules.--allow-unsupported-php-version=yes (risky).@Symfony rules be used, or a custom Laravel-specific ruleset?- name: PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@PSR12
array_syntax, class_attributes) in the team wiki.pint (PHP formatter) or psalm (static analysis)?pint for formatting and PHP-CS-Fixer for semantic fixes.composer require --dev friendsofphp/php-cs-fixer.php artisan cs-fix) for consistency.pint: Use pint for formatting (whitespace, indentation) and PHP-CS-Fixer for semantic fixes (e.g., @var to array type hints).psalm or phpstan to catch logical errors while PHP-CS-Fixer handles style.phpunit via --test flag to ensure fixes don’t break tests.feature/cs-fixer)../vendor/bin/php-cs-fixer init to generate a baseline config (php-cs-fixer.php).@property-read support if needed).check mode in CI to identify violations without auto-fixing.# Step 1: Check only (no auto-fix)
- run: ./vendor/bin/php-cs-fixer check src --rules=@PSR12 --diff
# Step 2: After 2 weeks, enable auto-fix
- run: ./vendor/bin/php-cs-fixer fix src --rules=@PSR12
husky).CONTRIBUTING.md.--allow-unsupported-php-version=yes cautiously.rector). Use composer why-not to resolve.php-cs-fixer/shim if conflicts arise.laravel-valet, docker, and homestead.| Phase | Action | Tools/Commands |
|---|---|---|
| Discovery | Audit current codebase for violations. | php-cs-fixer check --diff --rules=@PSR12 |
| Configuration | Customize php-cs-fixer.php to match Laravel’s standards. |
Edit config file; test with php-cs-fixer fix --dry-run |
| CI Integration | Add to GitHub Actions/GitLab CI. | Example: .github/workflows/php-cs-fixer.yml |
| Local Setup | Configure IDEs and pre-commit hooks. | PhpStorm/VS Code plugins + husky |
| Enforcement | Block merges on failures; auto-fix in CI. | php-cs-fixer fix --allow-risky=yes (for risky rules) |
| Maintenance | Update rulesets annually (e.g., for PHP 8.3+ features). | composer update friendsofphp/php-cs-fixer |
.php-cs-fixer.php locally, leading to inconsistencies..php-cs-fixer.dist.php).php-cs-fixer fix --config=php-cs-fixer.php to enforce central config.How can I help you explore Laravel packages today?