Installation:
composer require --dev wayofdev/cs-fixer-config
Create Config File:
Place .php-cs-fixer.dist.php in your project root with this minimal setup:
<?php
declare(strict_types=1);
use WayOfDev\PhpCsFixer\Config\ConfigBuilder;
use WayOfDev\PhpCsFixer\Config\RuleSets\DefaultSet;
require_once 'vendor/autoload.php';
return ConfigBuilder::createFromRuleSet(new DefaultSet())
->inDir(__DIR__ . '/src')
->inDir(__DIR__ . '/tests')
->getConfig();
First Use Case: Run a dry-run to preview changes:
composer cs:diff
WayOfDev\PhpCsFixer\Config\RuleSets\ for predefined configurations (DefaultSet, ExtendedPERSet).ConfigBuilder methods for customization (e.g., inDir(), addFiles()).scripts section in composer.json for predefined commands.Project Setup:
DefaultSet for Symfony-like standards or ExtendedPERSet for PER-CS2.0 compliance.->inDir(__DIR__ . '/src')
->inDir(__DIR__ . '/tests')
File Inclusion:
->addFiles([__FILE__])
Cache Optimization:
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/cache');
CI/CD Integration:
composer cs:fix in GitHub Actions to auto-fix and commit changes (see example workflow).Makefile targets for lint-php and lint-diff (see template).composer cs:diff and block non-compliant code:
composer cs:diff || exit 1
DefaultSet by overriding rules:
use WayOfDev\PhpCsFixer\Config\RuleSets\DefaultSet as BaseSet;
class CustomSet extends BaseSet {
protected function getRules(): array {
return array_merge(parent::getRules(), [
'@PSR12' => true,
'no_unused_imports' => true,
]);
}
}
Cache Directory:
.build/ to .gitignore may bloat your repo with cached files..build/php-cs-fixer/ in .gitignore.Rule Conflicts:
DefaultSet and ExtendedPERSet may cause unexpected rule overlaps.Dry-Run Misuse:
composer cs:diff shows changes but doesn’t apply them. Always review the diff before committing auto-fixes.PHP Version Mismatch:
composer.json.-v flag for detailed logs:
composer cs:fix -v
rm -rf .build/php-cs-fixer/cache
Custom RuleSets:
Extend WayOfDev\PhpCsFixer\Config\RuleSets\AbstractRuleSet to create reusable configurations:
class MyRuleSet extends AbstractRuleSet {
protected function getRules(): array {
return [
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
];
}
}
Dynamic Configs:
Use ConfigBuilder methods like setFinder() to dynamically include/exclude files based on patterns:
->setFinder(
(new \PhpCsFixer\Finder())
->in(__DIR__)
->exclude(['vendor', 'node_modules'])
)
Environment-Specific Rules: Override rules in CI vs. local development:
if (getenv('CI')) {
$config->setRiskyAllowed(true); // Enable riskier fixes in CI
}
composer cs:fix locally before committing to avoid large, noisy PRs.pull_request events (see workflow template).DefaultSet) in CONTRIBUTING.md to align contributors.How can I help you explore Laravel packages today?