ergebnis/php-cs-fixer-config
Factory-style PHP-CS-Fixer config for projects: choose a versioned ruleset (PHP 5.3–8.3), generate a consistent configuration, and keep coding standards aligned across repositories. Install via Composer and use with friendsofphp/php-cs-fixer.
Installation:
composer require --dev ergebnis/php-cs-fixer-config:^6.62.3
Add to composer.json under require-dev if not using --dev.
Create Config File:
Place .php-cs-fixer.php in your project root with the updated minimal setup:
<?php
declare(strict_types=1);
use Ergebnis\PhpCsFixer\Config;
use PhpCsFixer\Finder;
$config = Config\Factory::fromRuleSet(Config\RuleSet\Php83::create());
$config->setFinder(Finder::create()->in(__DIR__));
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
return $config;
First Use Case: Run PHP-CS-Fixer directly:
vendor/bin/php-cs-fixer fix
Or integrate into CI (see GitHub Actions below).
Php83 for PHP 8.3).
$ruleSet = Config\RuleSet\Php83::create();
$finder = Finder::create()
->in(['src', 'tests'])
->exclude(['vendor', 'node_modules']);
->name('*.php')
->notName('*.blade.php') // Exclude Blade templates
Add to composer.json:
"scripts": {
"cs-fix": [
"mkdir -p .build/php-cs-fixer",
"@php-cs-fixer"
],
"php-cs-fixer": "php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose"
}
Run:
composer cs-fix
Add a pre-commit hook in .git/hooks/pre-commit:
#!/bin/sh
vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php
Use in GitHub Actions (updated dependencies in 6.62.3):
- uses: actions/checkout@v7
- uses: zizmorcore/zizmor-action@v0.5.7
with:
php-cs-fixer: true
Enable file headers globally:
$ruleSet = Config\RuleSet\Php83::create()->withHeader($yourHeaderString);
Register third-party fixers (e.g., from erickskrauch/php-cs-fixer-custom-fixers):
$ruleSet = Config\RuleSet\Php83::create()
->withCustomFixers(Config\Fixers::fromFixers(
new \ErickSkrauch\PhpCsFixerCustomFixers\Fixer\FooFixer()
))
->withRules(Config\Rules::fromArray([
'ErickSkrauch\Foo' => true,
]));
Cache Directory:
.build/php-cs-fixer/ to .gitignore can bloat your repo.mkdir -p .build/php-cs-fixer in scripts to auto-create it.Rule Conflicts:
get_called_class in PHP 8.3).Custom Fixer Dependencies:
erickskrauch/php-cs-fixer-custom-fixers) will cause errors.composer require --dev erickskrauch/php-cs-fixer-custom-fixers
Performance:
--parallel for speed:
vendor/bin/php-cs-fixer fix --parallel
vendor/bin/php-cs-fixer fix --dry-run --diff
--verbose to diagnose issues.php-cs-fixer validate to check config syntax.Dynamic Rule Sets:
Load rule sets dynamically based on environment (e.g., Php74 for legacy branches):
$ruleSet = Config\RuleSet\Php74::create();
if (app()->environment('production')) {
$ruleSet = Config\RuleSet\Php83::create();
}
Laravel-Specific:
// In your PackageServiceProvider
$this->publishes([
__DIR__.'/config/php-cs-fixer.php' => config_path('php-cs-fixer.php'),
]);
// app/Console/Commands/FixCs.php
use Symfony\Component\Process\Process;
class FixCs extends Command {
protected $signature = 'cs:fix';
public function handle() {
$process = new Process(['vendor/bin/php-cs-fixer', 'fix']);
$process->run();
$this->output->write($process->getOutput());
}
}
Rule Set Inheritance: Extend existing rule sets in your config:
class CustomRuleSet extends Config\RuleSet\Php83 {
public static function create(): self {
$ruleSet = parent::create();
return $ruleSet->withRules(Config\Rules::fromArray([
'array_syntax' => ['syntax' => 'short'],
]));
}
}
HeaderCommentFixer respects YAML-style headers but may strip existing comments. Use --allow-risky=yes cautiously.->notPath() for complex exclusions (e.g., ->notPath(['*/Resources/*'])).Team Alignment:
composer.json to avoid unexpected rule changes:
"ergebnis/php-cs-fixer-config": "^6.62.3"
Partial Fixes:
Use --path-mode=intersection to fix only files matching both your finder and PHP-CS-Fixer’s rules.
IDE Integration:
Configure PHPStorm to use your .php-cs-fixer.php via:
Settings > Editor > Code Style > PHP > PHP-CS-Fixer > Config File.
Legacy Support:
For PHP 7.4 projects, use Php74 but override risky rules like mb_str_functions:
->withRules(Config\Rules::fromArray(['mb_str_functions' => false]))
friendsofphp/php-cs-fixer updated to 3.95.11 (includes bug fixes and performance improvements).actions/cache, actions/checkout, zizmorcore/zizmor-action).rector/rector and phpstan/phpstan-phpunit for better static analysis compatibility.renew.yaml ignore rule for artipacked (PR #1435). Ensure your .gitignore or CI excludes this file if applicable.How can I help you explore Laravel packages today?