kubawerlos/php-cs-fixer-custom-fixers
Custom fixers for FriendsOfPHP PHP-CS-Fixer. Install via Composer, register the Fixers set, then enable individual rules to enforce additional style conventions (e.g., prefer class constants, remove leading global namespace slashes, tidy PHPDoc params).
composer require --dev kubawerlos/php-cs-fixer-custom-fixers
.php-cs-fixer.dist.php to register the custom fixers:
<?php
return (new PhpCsFixer\Config())
->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers())
->setRules([
'@PSR2' => true,
// Add your custom fixers here
\PhpCsFixerCustomFixers\Fixer\NoLeadingSlashInGlobalNamespaceFixer::name() => true,
]);
vendor/bin/php-cs-fixer fix
Fix global namespace slashes (e.g., \Foo → Foo):
-$x = new \Foo();
+$x = new Foo();
Run:
vendor/bin/php-cs-fixer fix --rules=NoLeadingSlashInGlobalNamespaceFixer
Laravel CI/CD Pipeline:
- name: Run PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --diff --dry-run
Pre-Commit Hook:
php-cs-fixer with husky or pre-commit to auto-fix files before commit:
composer require --dev php-cs-fixer
npx husky add .husky/pre-commit "vendor/bin/php-cs-fixer fix --dry-run"
Team Onboarding:
CONTRIBUTING.md)."Use
NoLeadingSlashInGlobalNamespaceFixerto avoid slashes in global namespace classes."
Group Related Fixers: Combine fixers for similar concerns (e.g., PHPDoc, PHPUnit, or performance):
->setRules([
// PHPDoc fixers
\PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true,
\PhpCsFixerCustomFixers\Fixer\PhpDocPropertySorterFixer::name() => true,
// PHPUnit fixers
\PhpCsFixerCustomFixers\Fixer\PhpUnitAssertArgumentsOrderFixer::name() => true,
]);
Conditional Fixers:
Use fixers selectively based on project needs (e.g., disable NoNullableBooleanTypeFixer for legacy code):
->setRules([
\PhpCsFixerCustomFixers\Fixer\NoNullableBooleanTypeFixer::name() => [
'risky' => false, // Skip if null checks are critical
],
]);
Custom Config for Teams:
Share a base config (e.g., php-cs-fixer-team.php) and allow overrides:
// php-cs-fixer-team.php
return (new PhpCsFixer\Config())
->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers())
->setRules([
'@PSR12' => true,
\PhpCsFixerCustomFixers\Fixer\NoCommentedOutCodeFixer::name() => true,
]);
Bootstrap Requirement:
php-cs-fixer/shim, manually require the bootstrap:
require __DIR__ . '/vendor/kubawerlos/php-cs-fixer-custom-fixers/bootstrap.php';
ClassNotFound errors.Risky Fixers:
IssetToArrayKeyExistsFixer, NoNullableBooleanTypeFixer) may break logic.\PhpCsFixerCustomFixers\Fixer\IssetToArrayKeyExistsFixer::name() => [
'risky' => false,
],
Deprecated Fixers:
DataProviderNameFixer) are deprecated in favor of PHP CS Fixer’s built-ins.php_unit_data_provider_name) to avoid future conflicts.Performance Impact:
NoDuplicatedArrayKeyFixer or NoDuplicatedImportsFixer can slow down large codebases.vendor/bin/php-cs-fixer fix app/Http/Controllers/
Dry Run:
Always use --diff or --dry-run first to preview changes:
vendor/bin/php-cs-fixer fix --diff
Fix Specific Files: Target problematic files directly:
vendor/bin/php-cs-fixer fix app/Models/User.php --rules=NoUselessCommentFixer
Ignore Files: Exclude files/directories from fixing:
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('storage')
)
Custom Fixer Logic:
class LaravelRouteNameFixer extends AbstractFixer
{
public function getName(): string { return 'laravel_route_name'; }
// Implement fix logic...
}
->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers())
->registerCustomFixers(new \App\Fixers\LaravelFixers())
Override Default Config:
CommentedOutFunctionFixer to allow dd()):
\PhpCsFixerCustomFixers\Fixer\CommentedOutFunctionFixer::name() => [
'functions' => ['print_r', 'var_dump'], // Exclude 'dd'
],
Combine with Other Tools:
psalm or phpstan to catch logical errors before applying fixers.composer test:static
vendor/bin/php-cs-fixer fix
Laravel-Specific Fixers:
Add fixers for Laravel idioms (e.g., Route::name() formatting):
\PhpCsFixerCustomFixers\Fixer\NoUselessStrlenFixer::name() => true,
// Custom: Ensure Route names use snake_case
Version Pinning:
Pin the package version in composer.json to avoid unexpected updates:
"require-dev": {
"kubawerlos/php-cs-fixer-custom-fixers": "^1.0"
}
CI Feedback:
Use GitHub’s php-cs-fixer action to enforce standards:
- name: PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: "--diff --dry-run"
How can I help you explore Laravel packages today?