dragon-code/codestyler
Codestyler by The Dragon Code automatically fixes coding style issues in PHP/Laravel projects using Rector and Laravel Pint. Installs via Composer and can publish pint.json and .editorconfig via CLI for consistent formatting and linting.
## Getting Started
### Minimal Setup
1. **Install the package** in your Laravel project:
```bash
composer require dragon-code/codestyler --dev
composer.json under scripts.post-update-cmd:
"scripts": {
"post-update-cmd": [
"vendor/bin/codestyle pint 8.5",
"vendor/bin/codestyle rector laravel",
"vendor/bin/codestyle editorconfig"
]
}
composer update
Run the style command to auto-fix PHP, JS, CSS, and JSON:
composer style
This executes:
For PHP-only projects, simplify composer.json:
"scripts": {
"style": [
"vendor/bin/rector",
"vendor/bin/pint --parallel"
]
}
Run with:
composer style
Override default Rector rules by publishing its config:
vendor/bin/codestyle rector laravel --publish-config
Edit config/rector.php to add custom rules (e.g., SetListSyntax::TO_SHORT).
Skip specific paths in .editorconfig or pint.json:
// pint.json
{
"paths": [
"app/**/*.php",
"!app/Excluded/**/*.php"
]
}
Add to .git/hooks/pre-commit:
#!/bin/sh
composer style
Ensure it runs only on staged PHP/JS files for performance.
Use the provided GitHub Actions template:
# .github/workflows/code-style.yml
jobs:
Checks:
uses: TheDragonCode/.github/.github/workflows/code-style.yml@main
with:
php: true
node: false # Disable if not needed
composer update
.editorconfig, pint.json, and biome.json via Git.composer style command in your CONTRIBUTING.md.PHP Version Mismatch
vendor/bin/codestyle pint 8.2
composer.json scripts to match your PHP version.Biome Not Running
package.json is missing, Biome commands fail silently.CODESTYLER_SKIP_NODE=true in CI or ensure package.json exists.Rector Breaking Changes
laravel preset includes risky rules (e.g., modernize_strpos).rector.php:
return [
'rules' => [
// Disable risky rules
\Rector\Modernize\Rector\String_\ModernizeStrposRector::class => false,
],
];
EditorConfig Conflicts
.editorconfig if not properly configured.File | Settings | Editor | Code Style | Scheme: Project).Composer Normalize Overwriting
composer normalize may reformat composer.json unexpectedly.post-update-cmd if your team prefers manual formatting.vendor/bin/pint --dry-run
vendor/bin/rector --dry-run
vendor/bin/codestyle --verbose
vendor/bin/pint app/Http/Controllers/ExampleController.php
Custom Pint Presets
Extend pint.json with project-specific rules:
{
"preset": "laravel",
"rules": {
"@PHPUnit": true,
"no_unused_imports": true
}
}
Rector Custom Rules Add project-specific Rector rules:
// config/rector.php
return [
'rules' => [
\Rector\CodeQuality\Rector\Class_\CompletePhpdocByShortVarRector::class,
\App\Custom\Rector\MyCustomRector::class, // Your custom rector
],
];
Biome Custom Config
Override Biome’s biome.json to exclude analytics/metrics files:
{
"linter": {
"ignores": ["analytics/**", "metrics/**"]
}
}
Post-Style Hooks
Add cleanup tasks after composer style:
"scripts": {
"style": [
"vendor/bin/rector",
"vendor/bin/pint --parallel",
"@post-style" // Custom script
],
"post-style": [
"php artisan optimize:clear"
]
}
vendor/bin/pint --parallel
pint.json:
"paths": ["app/**/*.php", "!tests/**"]
vendor/bin/rector --cache-dir=var/rector_cache
symfony/* packages are updated to avoid conflicts.composer global require), replace vendor/bin/codestyle with codestyle in scripts.vendor/bin/codestyle --skip-self
---
How can I help you explore Laravel packages today?