chiiya/code-style-php
Reusable code style configs for PHP 8.1+ projects, combining PHP-CS-Fixer, EasyCodingStandard (ECS), and Rector. Install via Composer and import the provided rule sets into your tool config files to standardize formatting and refactors.
composer require --dev chiiya/code-style-php
cp vendor/chiiya/code-style-php/.php-cs-fixer.dist.php .
cp vendor/chiiya/code-style-php/ecs.php .
cp vendor/chiiya/code-style-php/rector.php .
./vendor/bin/php-cs-fixer fix --dry-run
./vendor/bin/ecs check
./vendor/bin/rector process --dry-run
Integrate with GrumPHP (included as a dev dependency) for pre-commit hooks:
composer require --dev phpro/grumphp
.grumphp.yml to use the new configs:
parameters:
tasks:
phpcsfixer:
config: .php-cs-fixer.dist.php
ecs:
config: ecs.php
rector:
config: rector.php
Daily Linting
Add a fix script to package.json or composer.json:
"scripts": {
"lint:fix": "php-cs-fixer fix && ecs check --fix && rector process"
}
Run via:
npm run lint:fix # or `composer lint:fix`
CI/CD Pipeline Use the package in GitHub Actions (example):
- name: Run Code Style Checks
run: |
./vendor/bin/php-cs-fixer fix --dry-run
./vendor/bin/ecs check
./vendor/bin/rector process --dry-run
Laravel-Specific Patterns
php artisan with custom commands for team-wide fixes:
// app/Console/Commands/FixCodeStyle.php
public function handle() {
$this->call('vendor:publish', ['--provider' => 'Chiiya\CodeStyle\CodeStyleServiceProvider']);
$this->info('Published configs. Run `php-cs-fixer fix` next.');
}
Team Onboarding
Include the configs in your README.md under "Development Setup" with:
## Code Style
Ensure your IDE uses these configs:
- PHP-CS-Fixer: [`.php-cs-fixer.dist.php`](.php-cs-fixer.dist.php)
- ECS: [`ecs.php`](ecs.php)
- Rector: [`rector.php`](rector.php)
PHP Version Mismatch
composer.json or use a wrapper script:
alias cs-fixer='php82 ./vendor/bin/php-cs-fixer'
Rector Breaking Changes
InlineArrayReturnAssignRector (added in v1.10.0) may refactor existing logic unexpectedly.--dry-run and --diff to preview changes:
./vendor/bin/rector process --dry-run --diff
ECS vs. PHP-CS-Fixer Overlap
no_unused_imports) exist in both tools and may conflict.ecs.php:
return ECSConfig::configure()
->withRules([
// Explicitly exclude PHP-CS-Fixer’s unused-imports rule
new NoUnusedImports(),
])
->withParallel();
GrumPHP Caching Issues
--no-cache:
./vendor/bin/grumphp run --no-cache
Isolate Rule Failures
Use --rules flags to test individual tools:
./vendor/bin/php-cs-fixer fix --rules=@PSR12 --dry-run
./vendor/bin/ecs check --rules=Symplify\CodingStandard\Rules\Arrays\ArrayPushArrayRule
Custom Rule Exclusions Exclude specific files/directories in configs:
// .php-cs-fixer.dist.php
return PhpCsFixerConfig::create()
->withRules([
'@PSR12' => true,
])
->withPath('src/')
->ignore(['tests/Feature/']);
IDE Integration
.php-cs-fixer.dist.php via:
Settings > Languages & Frameworks > PHP > Code Sniffer > Scheme > Import Configuration..php-cs-fixer.dist.php.Custom Rules
Extend the configs by merging additional rules. Example for rector.php:
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
return static function (RectorConfig $rector): void {
$rector->importNames();
$rector->importShortClasses();
$rector->ruleWithConfiguration(
InlineArrayReturnAssignRector::class,
[Option::PHP_VERSION_FEATURES => PhpVersion::PHP_82]
);
// Add your custom rector here
$rector->rule(CustomRector::class);
};
Dynamic Configs Use Laravel’s Service Provider to load configs dynamically:
// app/Providers/CodeStyleServiceProvider.php
public function boot() {
$this->publishes([
__DIR__.'/../../config/code-style.php' => config_path('code-style.php'),
], 'code-style');
}
Then reference config('code-style.php-cs-fixer') in your configs.
Git Hooks
Auto-fix on git commit via a pre-commit hook:
echo '#!/bin/sh
./vendor/bin/php-cs-fixer fix --dry-run || exit 1' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
How can I help you explore Laravel packages today?