jakub-onderka/php-code-style
A PHP coding style toolkit for consistent formatting and style enforcement across projects. Helps standardize code layout and conventions, making reviews easier and reducing style-related noise in diffs for teams and CI pipelines.
Installation Add the package via Composer:
composer require --dev jakub-onderka/php-code-style
Register the service provider in config/app.php under providers:
JakubOnderka\PHPCodeStyle\PHPCodeStyleServiceProvider::class,
Basic Usage Run the fixer directly via Artisan:
php artisan code-style:fix
This will process files in app/ by default (check config/code-style.php for paths).
First Use Case Integrate into your CI pipeline (e.g., GitHub Actions) to enforce style consistency before merges:
- name: Run PHP Code Style Fixer
run: php artisan code-style:fix --dry-run
Pre-Commit Hook
Use husky or Laravel’s pre-commit to run the fixer automatically:
php artisan code-style:fix --path=app/Http/Controllers
Tip: Cache results with --cache for faster repeated runs.
Custom Rules
Extend the default rules (e.g., PSR-2) by publishing the config:
php artisan vendor:publish --provider="JakubOnderka\PHPCodeStyle\PHPCodeStyleServiceProvider" --tag="config"
Modify config/code-style.php to include additional rules (e.g., SquizLabs_PHP_CodeSniffer).
Parallel Processing For large codebases, leverage Laravel’s queues to process files in parallel:
// In a custom Artisan command
$files = File::allFiles(app_path());
foreach ($files as $file) {
FixFiles::dispatch($file->getPathname());
}
IDE Integration
Use php-cs-fixer (a modern fork) as a fallback if this package lacks features. Sync configs between tools:
php artisan code-style:config:export --format=php-cs-fixer
Outdated Rules The package is unmaintained (last release: 2014). Prioritize:
php-cs-fixer (active maintenance).php artisan code-style:config:export --format=php-cs-fixer > .php-cs-fixer.dist.php
Performance Issues
--diff and --verbose for faster runs.memory_limit if processing many files:
memory_limit = 2G
Config Overrides
.php-codestyle files override global settings. Document this in your team’s style guide.Dry Runs
Always use --dry-run first to preview changes:
php artisan code-style:fix --dry-run --diff
Excluded Files
Use .php-codestyle-exclude to skip tests or generated files:
/tests/
/storage/
Custom Rules Debugging Enable verbose output to trace rule failures:
php artisan code-style:fix --verbose
Custom Rulesets
Create a Ruleset class to combine multiple standards:
use JakubOnderka\PHPCodeStyle\Ruleset;
class CustomRuleset extends Ruleset {
protected $rules = [
'@PSR2',
'SquizLabs_PHP_CodeSniffer.Sniffs.WhiteSpace.SpaceAfterCastSniff',
];
}
Event Listeners
Hook into the code-style.fixed event to log or notify:
// In EventServiceProvider
protected $listen = [
'code-style.fixed' => [
\App\Listeners\LogCodeStyleFix::class,
],
];
Parallel Processing
Override the FixFiles command to implement chunked processing:
public function handle() {
$files = File::allFiles(app_path())->chunk(100);
foreach ($files as $chunk) {
FixFiles::dispatch($chunk);
}
}
How can I help you explore Laravel packages today?