mfn/php-cs-fixer-config
Opinionated PHP-CS-Fixer rule set for v3.11+ from mfn. Designed to be dropped into your own fixer config via Mfn\PhpCsFixer\Config::getRules(). Requires setRiskyAllowed(true). PRs welcome; issues disabled.
composer require --dev mfn/php-cs-fixer-config
php-cs-fixer rules with the package’s config in php-cs-fixer.dist.php:
<?php
require 'vendor/autoload.php';
return (new PhpCsFixer\Config())
->setRules(\Mfn\PhpCsFixer\Config::getRules())
->setRiskyAllowed(true)
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/../src')
->exclude('vendor')
);
./vendor/bin/php-cs-fixer fix --dry-run --diff
Use this package to standardize code formatting across your team without manual rule curation. Ideal for:
Replace Existing Config:
Delete custom php-cs-fixer rules and replace them with:
$rules = \Mfn\PhpCsFixer\Config::getRules();
This ensures consistency across all projects using the package.
Laravel-Specific Adjustments: Override rules for Laravel patterns (e.g., facades, Blade) by merging with the package’s rules:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['no_unused_imports'] = 'false'; // Disable for facades
return (new PhpCsFixer\Config())->setRules($rules)->setRiskyAllowed(true);
CI/CD Integration:
Add to your GitHub Actions workflow (.github/workflows/php-cs-fixer.yml):
- name: PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --diff --allow-risky=yes
| Pattern | Implementation |
|---|---|
| Pre-commit Hook | Use php-cs-fixer with husky or pre-commit: |
| ```bash | |
| composer require --dev php-cs-fixer/mfn-config | |
| echo 'vendor/bin/php-cs-fixer fix' >> .git/hooks/pre-commit | |
| ``` | |
| Laravel Facades | Disable no_unused_imports for facade files: |
| ```php | |
| $rules['no_unused_imports'] = ['false', 'App\Facades.*']; | |
| ``` | |
| Blade Templates | Exclude .blade.php files from checks: |
| ```php | |
| ->setFinder(Finder::create()->exclude(['*.blade.php'])) | |
| ``` | |
| Custom Rules | Extend the package’s ruleset: |
| ```php | |
| $rules = \Mfn\PhpCsFixer\Config::getRules(); | |
| $rules['php_unit_method_casing'] = ['snake_case', 'skip_closing_tag']; | |
| ``` |
ordered_imports may conflict with Laravel’s use App\Facades;. Override:
$rules['ordered_imports'] = ['case_sensitive' => false, 'sort_algorithm' => 'alpha'];
->setFinder(Finder::create()->in(['src', 'app'])->exclude(['resources/views']))
property_type for dynamic properties:
$rules['property_type'] = ['skip_missing_properties' => true];
setRiskyAllowed(true):
native_function_invocation (e.g., str_replace → str_replace()).--dry-run first. Override risky rules:
$rules['native_function_invocation'] = ['include' => ['str_replace']];
Laravel Facade Conflicts:
no_unused_imports may flag use App\Facades; as unused.$rules['no_unused_imports'] = ['false', 'App\Facades.*'];
Blade Template False Positives:
@foreach).->setFinder(Finder::create()->exclude(['*.blade.php']))
PHP 8.4 Nullable Types:
nullable_type_declaration may break PHP <8.4 code.if (version_compare(PHP_VERSION, '8.4.0') < 0) {
$rules['nullable_type_declaration'] = false;
}
Deprecated Rules:
no_trailing_comma_in_list_call → no_trailing_comma_in_singleline).php-cs-fixer is v3.11+ to avoid conflicts../vendor/bin/php-cs-fixer fix --dry-run --diff
./vendor/bin/php-cs-fixer fix --rules=ordered_imports --dry-run
php-cs-fixer.custom.php to override rules:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['array_indentation'] = 'true';
return (new PhpCsFixer\Config())->setRules($rules);
Override Rules: Merge with the package’s rules to customize:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['phpdoc_align'] = ['align' => 'left'];
Add Custom Rules: Extend the ruleset dynamically:
$rules = \Mfn\PhpCsFixer\Config::getRules();
$rules['custom_rule'] = ['value' => 'config'];
Conditional Rules: Use PHP logic to enable/disable rules based on environment:
$rules = \Mfn\PhpCsFixer\Config::getRules();
if (app()->environment('local')) {
$rules['no_unused_imports'] = false;
}
composer.json and run php-cs-fixer in deployment scripts.--minimal flag for faster tests:
./vendor/bin/pest --minimal && ./vendor/bin/php-cs-fixer fix
CONTRIBUTING.md to reduce ramp-up time. Example:
## Code Style
We use [mfn/php-cs-fixer-config](https://github.com/mfn/php-cs-fixer-config) for consistent formatting.
Run `composer fix` to auto-format your code.
How can I help you explore Laravel packages today?