Tim MacDonald shares a way to sharing PHP-CS-Fixer rules across your projects. It involves in setting up on repo/package that contains the rules and a little helper class that takes in a PhpCsFixer\Finder
instance (along with optional extra rules).
<?php
namespace TiMacDonald;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
function styles(Finder $finder, array $rules = []): Config {
$rules = array_merge(require __DIR__.'/rules.php', $rules);
return Config::create()
->setFinder($finder)
->setRiskyAllowed(true)
->setRules($rules);
}
Once required in your actual project – using Composer – you have that project’s .php_cs.dist
call the styles
function:
<?php
$finder = PhpCsFixer\Finder::create()
->in([
__DIR__.'/app',
__DIR__.'/config',
__DIR__.'/database',
__DIR__.'/routes',
__DIR__.'/tests',
]);
return TiMacDonald\styles($finder);
Nice!