- How do I integrate PHP CS Fixer into a Laravel project’s CI/CD pipeline?
- Add a step in your GitHub Actions or GitLab CI file to run `php-cs-fixer fix --dry-run` before merging. For auto-fixing, use `php-cs-fixer fix --allow-risky=yes` in a pre-commit hook or CI job. Exclude `vendor/` and `storage/` directories with `--exclude='vendor|storage'`.
- Which rule set should I use for Laravel projects?
- Start with `@Symfony` for Laravel’s default conventions or `@PhpCsFixer` for stricter rules. Customize further by extending `.php-cs-fixer.dist.php` to include Laravel-specific rules like Blade template formatting. Test with `--dry-run` first to preview changes.
- Can PHP CS Fixer handle Laravel’s dynamic class loading (e.g., Facades, Service Providers)?
- Yes, but some rules like `NoUnusedImports` may flag Facades or dynamic imports. Whitelist exceptions in your config or use `--ignore='.*'` for specific files. Test edge cases with `php-cs-fixer fix --diff` to validate behavior.
- How do I create a custom Artisan command for PHP CS Fixer in Laravel?
- Generate a command with `php artisan make:command CsFixCommand`, then call `php-cs-fixer fix` with Laravel-specific paths (e.g., `--path-mode=intersection --path='app/Http' --path='app/Models'`). Register the command in `app/Console/Kernel.php` for global access.
- What’s the best way to handle third-party vendor code (e.g., node_modules) with PHP CS Fixer?
- Exclude vendor directories entirely with `--exclude='vendor|node_modules'`. For Laravel, focus on `app/`, `config/`, and `routes/` paths. If you must lint third-party code, use `--dry-run` to preview changes before applying fixes.
- How do I configure PHP CS Fixer to auto-fix files on git commit?
- Use `husky` or `pre-commit` hooks to run `php-cs-fixer fix --allow-risky=yes` before commits. Add a `.php-cs-fixer.dist.php` config file to version control and reference it in your hook script. Ensure the hook excludes `vendor/` and generated files.
- Does PHP CS Fixer support PHP 8.5 for Laravel projects?
- Yes, PHP CS Fixer supports PHP 7.4–8.5. For Laravel, ensure your `composer.json` and server PHP version match. Use `--allow-unsupported-php-version=yes` cautiously if testing unreleased PHP versions. Monitor release notes for breaking changes.
- How can I test PHP CS Fixer rules without breaking my Laravel codebase?
- Run `php-cs-fixer fix --dry-run --diff` to preview changes. Use `--path-mode=intersection` to target specific directories (e.g., `app/`) incrementally. For risky rules like `@autoPHPMigration:risky`, test in a staging environment first.
- What are the alternatives to PHP CS Fixer for Laravel?
- Alternatives include `php-cs-fixer/shim` (for dependency conflicts), `psalm` (for static analysis), or `laravel-pint` (a Laravel-specific wrapper). However, PHP CS Fixer offers deeper customization and built-in rule sets like `@Symfony`, making it the most robust choice for Laravel.
- How do I maintain the `.php-cs-fixer.dist.php` config file in a team?
- Commit the config file to version control and document its purpose in your team’s onboarding. Assign a maintainer to update rules when PHP/Laravel versions change. Use `php-cs-fixer fix --config=.php-cs-fixer.dist.php` to ensure consistency across environments.