mfn/php-cs-fixer-config
Opinionated PHP-CS-Fixer ruleset for v3.11+ via a simple \Mfn\PhpCsFixer\Config::getRules() helper. Requires setRiskyAllowed(true). Minimal repo with issues disabled—PRs welcome to discuss technical changes.
composer require --dev mfn/php-cs-fixer-config
.php-cs-fixer.php with the minimal config:
<?php
require 'vendor/autoload.php';
return (new PhpCsFixer\Config())
->setFinder(PhpCsFixer\Finder::create()->in(__DIR__.'/../src'))
->setRiskyAllowed(true)
->setRules(\Mfn\PhpCsFixer\Config::getRules());
./vendor/bin/php-cs-fixer fix
For a new Laravel developer joining your team:
php-cs-fixer fix to align their code with the team’s style.src/Config.php: Review the ruleset to understand the package’s opinionated choices (e.g., ordered_imports, nullable_type_declaration).- name: Run PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff
Replace your current .php-cs-fixer.php with the package’s rules:
return (new PhpCsFixer\Config())
->setFinder($finder) // Your existing finder logic
->setRiskyAllowed(true)
->setRules(\Mfn\PhpCsFixer\Config::getRules());
Extend the base rules while overriding specific ones:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['phpdoc_summary'] = false; // Disable if needed
$rules['strict_param'] = true; // Override risky rules
return (new PhpCsFixer\Config())
->setRules($rules)
->setRiskyAllowed(true);
native_function_invocation for Facade calls:
$rules['native_function_invocation']['include'] = [
'compiler_optimized',
'!Auth::user()', // Exclude Facade calls
];
->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__.'/../src')
->exclude(['resources/views'])
)
name: PHP-CS-Fixer
on: [push, pull_request]
jobs:
fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: ./vendor/bin/php-cs-fixer fix --dry-run --diff
php-cs-fixer with tools like Husky or Laravel Pint for local enforcement.->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__.'/../src')
->exclude(['resources/views', '*.blade.php'])
)
->setFinder(PhpCsFixer\Finder::create()
->in(['src', 'app/Http/Controllers'])
->name('*.php')
->notName('*.blade.php')
)
--dry-run --diff in CI to avoid unexpected changes:
./vendor/bin/php-cs-fixer fix --dry-run --diff
Risky Rules Breaking Laravel Patterns:
native_function_invocation: May break Facade calls (e.g., Auth::user()). Exclude them explicitly:
$rules['native_function_invocation']['include'] = ['compiler_optimized'];
no_unused_imports: Could flag Laravel’s dynamic imports (e.g., use Illuminate\Support\Facades\Auth; even if Auth isn’t used directly). Disable if needed:
$rules['no_unused_imports'] = false;
strict_param: May conflict with Laravel’s type-hinting conventions. Test thoroughly.PHP Version Mismatches:
nullable_type_declaration_for_default_null_value require PHP 8.4+. Ensure your CI and local environments match:
php -v # Verify PHP version
Blade Template Conflicts:
@foreach). Exclude Blade files:
->setFinder(PhpCsFixer\Finder::create()->exclude(['resources/views']))
Provenance Risks:
src/Config.php before adoption, especially:
Future-Dated Releases:
--dry-run --diff to preview changes:
./vendor/bin/php-cs-fixer fix --dry-run --diff
./vendor/bin/php-cs-fixer fix -v
$rules['rule_name'] = false;
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['array_syntax'] = ['syntax' => 'short']; // Override to 'short'
$rules['strict_types'] = getenv('CS_FIXER_STRICT_TYPES') === 'true';
git clone https://github.com/mfn/php-cs-fixer-config.git
composer require your-fork/php-cs-fixer-config
Facade Calls:
The native_function_invocation rule may misfire on Facade methods. Exclude them:
$rules['native_function_invocation']['exclude'] = [
'Auth::*',
'Cache::*',
'Route::*',
];
Dependency Injection:
Rules like ordered_imports may reorder Laravel’s Facade imports. Test with:
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
Ensure the order doesn’t break autoloading or IDE features (e.g., PHPStorm’s "Go to Implementation").
Blade Compatibility: PHP-CS-Fixer doesn’t handle Blade syntax well. Pair with Laravel Pint for Blade files:
./vendor/bin/pint --test
./vendor/bin/php-cs-fixer fix --cache-file=.php-cs-fixer.cache
./vendor/bin/php-cs-fixer fix --parallel
->setFinder
How can I help you explore Laravel packages today?