m6web/php-cs-fixer-config
Reusable PHP CS Fixer configuration from M6Web/Bedrock Streaming. Install via Composer and use the provided BedrockStreaming config in .php-cs-fixer.dist.php, with options to extend/override rules for your project or CI.
composer require --dev m6web/php-cs-fixer-config
touch .php-cs-fixer.dist.php
src/ and tests/ by default):
<?php
$finder = PhpCsFixer\Finder::create()
->in([__DIR__.'/src', __DIR__.'/tests']);
return (new M6Web\CS\Config\BedrockStreaming())->setFinder($finder);
.gitignore:
.php-cs-fixer.cache
./vendor/bin/php-cs-fixer fix --dry-run --diff
Integrate with Laravel’s ecosystem by adding a pre-commit hook in .git/hooks/pre-commit:
#!/bin/sh
./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation
Make it executable:
chmod +x .git/hooks/pre-commit
Why? Blocks non-compliant code before it’s committed, reducing merge conflicts and CI failures.
./vendor/bin/php-cs-fixer fix
make cs # Uses the Makefile target (see below)
./vendor/bin/php-cs-fixer fix src/Http/Controllers/
Add this to your Laravel project’s Makefile (place in project root):
# PHP-CS-Fixer targets
cs:
@echo "Checking PHP-CS-Fixer compliance..."
./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --diff
cs-fix:
@echo "Fixing PHP-CS-Fixer issues..."
./vendor/bin/php-cs-fixer fix
cs-ci:
@echo "Running PHP-CS-Fixer in CI mode..."
./vendor/bin/php-cs-fixer fix --dry-run --using-cache=no --verbose --rules=@BedrockStreaming
Usage:
make cs # Check for violations (dry run)
make cs-fix # Auto-fix all issues
make cs-ci # CI-friendly check (no cache, verbose)
Add to .github/workflows/php-cs-fixer.yml:
name: PHP-CS-Fixer
on: [push, pull_request]
jobs:
fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer install
- run: make cs-ci
Why? Ensures compliance before merging, even if local hooks are bypassed.
Extend the base config to add Laravel-specific rules (e.g., ordered imports):
<?php
$finder = PhpCsFixer\Finder::create()
->in([__DIR__.'/src', __DIR__.'/tests']);
$config = new class() extends M6Web\CS\Config\BedrockStreaming {
public function getRules(): array {
$rules = parent::getRules();
$rules['ordered_imports'] = ['sort_algorithm' => 'alpha'];
$rules['no_unused_imports'] = true;
return $rules;
}
};
return $config->setFinder($finder);
make cs-ci in deployment scripts to catch style issues early.--verbose flag to log violations to Telescope for team-wide visibility.cs target to your docker-compose.yml services for containerized checks.Rule Conflicts with Legacy Code
native_function_invocation or declare_strict_types may break older PHP versions.$config->setRiskyAllowed(false);
--allow-risky=yes in CLI for one-off fixes:
./vendor/bin/php-cs-fixer fix --allow-risky=yes
Cache Issues
make cs-ci # Uses `--using-cache=no`
.php-cs-fixer.cache manually if corruption is suspected.Performance in Large Repos
$finder->exclude(['node_modules', 'vendor', 'storage']);
--parallel (PHP-CS-Fixer 3.57+):
./vendor/bin/php-cs-fixer fix --parallel
IDE Integration Quirks
.php-cs-fixer.dist.php.Settings > Tools > PHP > PHP Code Sniffer > Configuration File → Point to .php-cs-fixer.dist.php.php-cs-fixer.executablePath to ./vendor/bin/php-cs-fixer.Git Hooks and Permissions
composer:
composer require --dev dealerdirect/phpcodesniffer-composer-installer
Then add to composer.json:
"extra": {
"installer-paths": {
"scripts/pre-commit.php": ["type:php-cs-fixer-hook"]
}
}
Laravel-Specific Rule Overrides Add these to your custom config for Laravel projects:
$rules['concat_space'] = ['spacing' => 'one']; // Laravel's `{{ }}` syntax
$rules['blank_line_after_opening_tag'] = true; // PSR-12 for Blade files
Exclude Vendor Files Avoid scanning Laravel’s vendor files (already compliant):
$finder->exclude(['vendor']);
Team-Specific Customizations
.blade.php files:
$finder->in([__DIR__.'/resources/views']);
database/migrations to the finder:
$finder->in([__DIR__.'/database/migrations']);
Debugging Rule Violations
Use --verbose to see which rule triggered a violation:
./vendor/bin/php-cs-fixer fix --verbose --dry-run
Output example:
FIXER APPLIED: [native_function_invocation][src/ServiceProvider.php:42] Found native function call 'count' without parentheses.
PHP 8.4-Specific Rules Leverage PHP 8.4 features with these rules:
$rules['php_unit_method_casing'] = ['case' => 'snake_case']; // For Pest/Laravel tests
$rules['php_unit_test_class_requires_covers'] = true; // Enforce test coverage
CI-Specific Optimizations
- uses: actions/cache@v3
with:
path: |
vendor
.php-cs-fixer.cache
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
artifacts to cache results between jobs.Pairing with Other Tools
How can I help you explore Laravel packages today?