mfn/php-cs-fixer-config
Opinionated php-cs-fixer ruleset for v3.11+ from MFN. Provides a ready-to-use rule array via Mfn\PhpCsFixer\Config::getRules(); enable setRiskyAllowed(true) in your php-cs-fixer config and apply the rules.
composer require --dev mfn/php-cs-fixer-config
php-cs-fixer.neon or PHP config:
require 'vendor/autoload.php';
$rules = \Mfn\PhpCsFixer\Config::getRules();
return (new PhpCsFixer\Config())
->setFinder(\PhpCsFixer\Finder::create()->in(__DIR__.'/src'))
->setRiskyAllowed(true)
->setRules($rules);
vendor/bin/php-cs-fixer fix --dry-run
Use this package to standardize your team’s PHP code style without reinventing the wheel. Ideal for:
php-cs-fixer for the first time but wanting a pre-configured, battle-tested setup.Team Onboarding:
composer.json under require-dev.php-cs-fixer on every PR (e.g., GitHub Actions):
- name: Run PHP-CS-Fixer
run: vendor/bin/php-cs-fixer fix --diff --dry-run
CONTRIBUTING.md as a mandatory step.Project-Specific Overrides:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['phpdoc_align'] = ['align' => 'left']; // Override a specific rule
setFinder() to target specific paths (e.g., exclude tests):
->setFinder(\PhpCsFixer\Finder::create()->notPath(['tests']))
Laravel-Specific Integration:
laravel-pint (if using Pint) by aliasing php-cs-fixer in composer.json:
"scripts": {
"cs-fix": "php-cs-fixer fix",
"pint": "pint"
}
php-cs-fixer for legacy codebases and Pint for new projects (or vice versa).php-cs-fixer via a tool like pre-commit to auto-fix files:
repos:
- repo: local
hooks:
- id: php-cs-fixer
name: PHP-CS-Fixer
entry: vendor/bin/php-cs-fixer fix --allow-risky=yes
language: system
types: [php]
php-cs-fixer as the code style tool in Settings > Tools > PHP > Code Sniffer.PHP CS Fixer extension and point it to your config file.Risky Rules:
setRiskyAllowed(true). Be cautious with risky rules (e.g., no_unused_imports) as they may break functionality. Test thoroughly in a staging environment.--dry-run first to preview changes:
vendor/bin/php-cs-fixer fix --dry-run --diff
Rule Conflicts:
array_syntax, concat_space) may conflict with Laravel conventions. Override them explicitly:
$rules['array_syntax'] = ['syntax' => 'short']; // Force short syntax
Performance:
vendor/, node_modules/) from the finder to speed up runs:
->setFinder(\PhpCsFixer\Finder::create()->exclude(['vendor', 'node_modules']))
--verbose to diagnose issues:
vendor/bin/php-cs-fixer fix --verbose
$rules['no_unused_imports'] = false; // Disable to debug
symfony):
$rules = array_merge(
\Mfn\PhpCsFixer\Config::getRules(),
\Symfony\CS\Config::getRules()
);
main branch):
$rules = \Mfn\PhpCsFixer\Config::getRules();
if (app()->environment('production')) {
$rules['strict_param'] = true;
}
php-cs-fixer in PHPUnit tests to validate style consistency:
$this->assertFileEquals(
__DIR__.'/expected_file.php',
__DIR__.'/actual_file.php',
'Files should match after CS fixing'
);
How can I help you explore Laravel packages today?