beste/php-cs-fixer-config
Shared PHP-CS-Fixer configuration used in BESTE projects, extending ergebnis/php-cs-fixer-config. Provides ready-made rulesets for PHP 8.1 and 8.2 to standardize code style across repositories.
Install the package in your Laravel project:
composer require --dev beste/php-cs-fixer-config
Configure PHP-CS-Fixer in your project’s root .php-cs-fixer.dist.php:
<?php
return (new Beste\PhpCsFixer\Config())
->setRules([
'@Beste' => true, // Use the BESTE preset (PHP 8.1/8.2)
'laravel:risky' => true, // Optional: Enable Laravel-specific rules
]);
Run PHP-CS-Fixer to auto-fix files:
vendor/bin/php-cs-fixer fix
Integrate with CI (e.g., GitHub Actions):
- name: Run PHP-CS-Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@Beste
Standardize a Laravel project’s code style by:
use statement ordering (e.g., grouped by type: Framework, Laravel, Third-party, Local).null types last).Project Setup
composer.json under require-dev:
"beste/php-cs-fixer-config": "^3.3"
.php-cs-fixer.dist.php:
return (new Beste\PhpCsFixer\Config())
->setRules([
'@Beste/Php82' => true, // PHP 8.2 preset
'ordered_imports' => true,
'no_unused_imports' => true,
]);
Laravel-Specific Customization
->setFinder(
(new PhpCsFixer\Finder())
->in(__DIR__)
->exclude('vendor')
->name('*.php')
->notName('*.blade.php') // Exclude Blade files
)
->setRules([
'blank_line_after_opening_tag' => true, // For Blade
]);
CI/CD Integration
jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@Beste/Php82
Team Onboarding
Makefile target for new developers:
fix-style:
@echo "Running PHP-CS-Fixer..."
vendor/bin/php-cs-fixer fix --rules=@Beste/Php82
Php82 for PHP 8.2 projects) in CONTRIBUTING.md.laravel:risky preset to handle dynamic use statements (e.g., use App\Commands\{$command})..blade.php files from auto-fixing unless you’ve configured Blade-specific rules.@your-org/php-cs-fixer-config) and require it across projects.php-cs-fixer with husky or pre-commit to block style violations early:
composer require --dev php-cs-fixer
npx husky add .husky/pre-commit "vendor/bin/php-cs-fixer fix --dry-run --diff"
Preset Mismatch
Php81 preset in a PHP 8.2 project may miss rules like match_using_similar_syntax.@Beste/Php82 for PHP 8.2).Deprecated Rules
no_spaces_inside_parenthesis → spaces_inside_parentheses), but some Laravel-specific rules may lag.Blade Template Handling
.blade.php) may not be formatted correctly by default.Finder:
->setFinder(
(new PhpCsFixer\Finder())
->in(__DIR__)
->name('*.php')
->exclude('resources/views') // Or include with Blade rules
)
Performance with Large Codebases
php-cs-fixer on monorepos or large projects can be slow.vendor/bin/php-cs-fixer fix --parallel
Or limit scope:
vendor/bin/php-cs-fixer fix app/ src/
CI Flakiness
- run: git config --global core.autocrlf input
--dry-run --diff to preview changes:
vendor/bin/php-cs-fixer fix --dry-run --diff --rules=@Beste/Php82
vendor/bin/php-cs-fixer fix --rules=ordered_imports
vendor/bin/php-cs-fixer fix -v
Custom Rulesets Extend the base config for project-specific needs:
return (new Beste\PhpCsFixer\Config())
->setRules([
'@Beste/Php82' => true,
'line_ending' => true, // Force LF
'array_syntax' => ['syntax' => 'short'], // Prefer `[]` over `array()`
]);
Laravel-Specific Overrides Target Laravel files (e.g., migrations, commands) with custom rules:
->setRules([
'files' => [
'database/migrations/*' => [
'no_unused_imports' => false, // Disable for migrations
],
],
]);
PHPCS-Fixer Version Pinning
Avoid conflicts by pinning the version in composer.json:
"php-cs-fixer": "^3.10",
"beste/php-cs-fixer-config": "^3.3"
Shared Config for Monorepos
Create a shared package (e.g., @your-org/coding-standards) with:
// packages/coding-standards/config/php-cs-fixer.php
return (new Beste\PhpCsFixer\Config())
->setRules([
'@Beste/Php82' => true,
// Shared rules...
]);
Require it in child projects:
"@your-org/coding-standards": "^1.0"
php artisan make:command FixStyle
// app/Console/Commands/FixStyle.php
public function handle()
{
$this->callSilently('php-cs-fixer', ['fix', '--rules=@Beste/Php82']);
}
php-cs-fixer with pre-commit:
composer require --dev php-cs-fixer
npx pre-commit install
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: php-cs-fixer
name: PHP-CS-Fixer
entry: vendor/bin/php-cs-fixer fix --dry
How can I help you explore Laravel packages today?