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 (or create one if missing):
<?php
return (\PhpCsFixer\Config)->create()
->setRules([
// Your existing rules...
'@PHP-CS-Fixer:risky' => true,
'@PHP-CS-Fixer:aliases' => true,
// New custom fixer (v3.37.2+)
'PhpdocNoIncorrectVarAnnotationFixer' => true,
])
->setFinder(
(\PhpCsFixer\Finder)::create()
->in(__DIR__.'/../src')
->in(__DIR__.'/../tests')
);
vendor/bin/php-cs-fixer fix
Fixer class for available fixers (e.g., NoUnusedPhpDocTags, NoSuperfluousPhpDocTags, PhpdocNoIncorrectVarAnnotationFixer).php-cs-fixer in composer.json scripts:
"scripts": {
"cs-fix": "php-cs-fixer fix",
"cs-check": "php-cs-fixer fix --dry-run --diff"
}
Pre-commit Hooks
Use php-cs-fixer with tools like roave/security-advisories or Laravel’s pre-commit scripts:
composer require --dev roave/security-advisories
composer require --dev laravel/pint # Optional: For parallel linting
Example composer.json script:
"scripts": {
"test": [
"@cs-check",
"@phpunit"
],
"cs-check": "php-cs-fixer fix --dry-run --diff"
}
CI/CD Pipeline
Add to Laravel’s GitHub Actions (.github/workflows/php-cs-fixer.yml):
name: PHP CS Fixer
on: [push, pull_request]
jobs:
fix:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-php@v3
with:
php-version: '8.2'
- run: composer install
- run: vendor/bin/php-cs-fixer fix --dry-run --diff
Custom Fixer Selection
Enable fixers in .php-cs-fixer.dist.php:
->setRules([
// Laravel-specific custom fixers
'NoUnusedPhpDocTags' => true,
'NoSuperfluousPhpDocTags' => true,
'PhpdocNoIncorrectVarAnnotationFixer' => true, // New in v3.37.2
'OrderedImports' => [
'sort_algorithm' => 'alpha',
'imports_order' => ['class', 'function', 'const'],
],
])
ServiceProvider classes, use:
'NoUnusedUses' => [
'remove_unused_aliases' => true,
'remove_unused_imports' => true,
],
pint for route files:
vendor/bin/pint routes/web.php
vendor/bin/php-cs-fixer fix routes/web.php
Rule Conflicts
PhpdocNoIncorrectVarAnnotationFixer vs. @PSR12).->setRules([
'@PSR12' => true,
'PhpdocNoIncorrectVarAnnotationFixer' => true,
'phpdoc_align' => false, // Disable if conflicting
])
Performance Overhead
--cache-file=.php-cs-fixer.cache.vendor/ and node_modules/:
->setFinder(
(\PhpCsFixer\Finder)::create()
->exclude('vendor')
->exclude('node_modules')
)
False Positives with PhpdocNoIncorrectVarAnnotationFixer
protected $fillable with @var array).'PhpdocNoIncorrectVarAnnotationFixer' => [
'skip_matching' => [
'~protected \\$fillable~',
'~private \\$guarded~',
],
]
vendor/bin/php-cs-fixer fix --dry-run --diff
vendor/bin/php-cs-fixer fix -v
vendor/bin/php-cs-fixer fix --rules=PhpdocNoIncorrectVarAnnotationFixer path/to/Model.php
Custom Fixers for Laravel
Extend the package by creating a new fixer (e.g., for Laravel’s HasFactory trait docs):
// app/CustomFixers/HasFactoryDocFixer.php
namespace App\CustomFixers;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Tokenizer\Tokens;
class HasFactoryDocFixer implements FixerInterface {
public function isRisky(): bool { return false; }
public function getName(): string { return 'has_factory_doc'; }
public function getDescription(): string { return 'Ensures HasFactory trait is documented.'; }
public function fix(\SplFileInfo $file, Tokens $tokens) { /* ... */ }
public function isTokenized(): bool { return true; }
}
Register it in .php-cs-fixer.dist.php:
->registerCustomFixers([
__DIR__.'/../app/CustomFixers/HasFactoryDocFixer.php',
])
Integration with Laravel Valet/PSA
Add to ~/.php-cs-fixer.dist.php for global projects:
->setRules([
'PhpdocNoIncorrectVarAnnotationFixer' => true,
// ...
])
Git Attributes
Enforce fixers via .gitattributes:
*.php diff=php-cs-fixer
Configure in .git/config:
[diff "php-cs-fixer"]
textconv = vendor/bin/php-cs-fixer filter --rules-file=.php-cs-fixer.dist.php
shivammathur/php-cs-fixer-config-laravel for pre-configured Laravel rules.php-cs-fixer with PHPStorm’s "Reformat Code" action via:
// .php-cs-fixer.dist.php
->setUsingCache(true)
CONTRIBUTING.md:
## Code Style
Run `composer cs-fix` to auto-fix style issues. Custom fixers:
- `PhpdocNoIncorrectVarAnnotationFixer`: Fixes incorrect `@var` annotations for asymmetric-visibility properties (e.g., `protected $fillable`).
- `NoUnusedPhpDocTags`: Removes redundant PHPDoc tags (e.g., `@var` for obvious types).
- `OrderedImports`: Sorts imports alphabetically by type.
How can I help you explore Laravel packages today?