drupol/phpcsfixer-configs-php
Ready-made PHP-CS-Fixer config objects for common standards and PHP versions (PSR-12, PHP 5.6–7.3). Implements ConfigInterface and supports combining rules via withRulesFrom() for easy reuse across projects.
composer require --dev drupol/phpcsfixer-configs-php
phpcs.xml or phpcsfixer.dist.php:
use Drupol\PhpCsFixerConfigsPhp\Config\PSR12;
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->name('*.php');
return (new PhpCsFixer\Config())
->setRules(new PSR12())
->setFinder($finder);
./vendor/bin/php-cs-fixer fix
Use the PSR12 config for modern Laravel projects (recommended for Laravel 8+). For legacy PHP 5.6 projects, use Php56.
Laravel CI/CD Pipelines Add PHP-CS-Fixer to your GitHub Actions workflow:
- name: Run PHP-CS-Fixer
run: ./vendor/bin/php-cs-fixer fix --config=phpcsfixer.dist.php --dry-run --diff
Use --dry-run for pre-commit checks.
Pre-Commit Hooks Integrate with Laravel Pint (if using) or Husky:
composer require --dev laravel/pint
./vendor/bin/pint --test # Test before commit
Custom Rule Overrides
Extend base configs (e.g., PSR12) with project-specific rules:
$config = (new PSR12())
->withRulesFrom([
'@PSR12',
'no_unused_imports' => true,
'ordered_imports' => ['sort_algorithm' => 'alpha'],
]);
Multi-PHP Version Projects
Use version-specific configs (e.g., Php71 for legacy code):
$config = (new Php71())
->withRulesFrom(new PSR12()); // Merge PSR12 rules
postcss or webpack.mix.js for build-time checks.task('phpcsfixer', function () {
run('./vendor/bin/php-cs-fixer fix');
});
Rule Conflicts
Php56 and PSR12 rules directly; use withRulesFrom() to merge.array_push is enabled in Php56 but disabled in PSR12.Deprecated Rules
escape_implicit_backslashes → string_implicit_backslashes).Performance
vendor/ and node_modules/ from PHP-CS-Fixer scans:
$finder->exclude(['vendor', 'node_modules']);
PHP-CS-Fixer Version Locking
^3.0 but excludes 3.0.1–3.0.3).composer.json constraints if needed:
"require-dev": {
"php-cs-fixer": "^3.10"
}
--dry-run in CI to preview changes:
./vendor/bin/php-cs-fixer fix --dry-run
./vendor/bin/php-cs-fixer fix --rules=@PSR12 --path-mode=intersection
$config->setCacheFile(__DIR__.'/phpcsfixer.cache');
Custom Configs
Create a Config class extending Drupol\PhpCsFixerConfigsPhp\Config\BaseConfig for reusable rules:
namespace App\Configs;
use Drupol\PhpCsFixerConfigsPhp\Config\PSR12;
class LaravelConfig extends PSR12 {
public function __construct() {
$this->withRules([
'no_unused_imports' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized']],
]);
}
}
Dynamic Rule Loading
Load rules from environment variables (e.g., .env):
$rules = explode(',', env('PHP_CS_FIXER_RULES', ''));
$config = (new PSR12())->withRulesFrom($rules);
Symfony Integration
For Symfony projects, use the symfony:risky preset as a base:
$config = (new PSR12())->withRulesFrom(['@Symfony', '@Symfony:risky']);
How can I help you explore Laravel packages today?