facile-it/facile-coding-standard
PHP coding standard based on PHP-CS-Fixer by Facile.it. Installs via Composer with an interactive setup that generates a .php-cs-fixer.dist.php, auto-detects files from composer autoload (psr-0/psr-4/classmap), and adds cs-check/cs-fix scripts.
Installation:
composer require --dev facile-it/facile-coding-standard
The installer will automatically generate:
.php-cs-fixer.dist.php (default config)composer.json scripts (cs-check, cs-fix)First Use Case: Run a dry check to preview changes:
composer cs-check
Apply fixes automatically:
composer cs-fix
Key Files:
.php-cs-fixer.dist.php: Default config (edit via .php-cs-fixer.php for overrides).composer.json: Contains cs-check/cs-fix scripts.Pre-Commit Hooks:
Integrate with php-cs-fixer via Git hooks (e.g., pre-commit) to enforce standards before commits:
composer cs-check # Fails if violations exist
CI/CD Pipeline: Add to Laravel’s CI (e.g., GitHub Actions) to block non-compliant PRs:
- name: Run Facile Coding Standard
run: composer cs-check
Team Onboarding:
cs-fix command in your CONTRIBUTING.md.composer facile-cs-create-config to regenerate config if rules update.Partial Fixes: Target specific files/directories:
php-cs-fixer fix app/Http/Controllers --dry-run
Risky Rules:
Enable risky rules (e.g., class_keyword) via a custom config:
$config = include __DIR__ . '/.php-cs-fixer.dist.php';
$config->setRules([
'@PER-CS3x0:risky' => true,
'class_keyword' => true,
]);
Laravel-Specific:
vendor/ and storage/ from autoload paths in .php-cs-fixer.dist.php:
$finder->exclude(['vendor', 'storage']);
bootstrap/cache/ to exclude:
$finder->ignoreDotFiles(true)->ignoreVCS(true);
Custom Rules:
Extend the CompositeRulesProvider to add project-specific rules:
$rulesProvider = new Facile\CodingStandards\Rules\CompositeRulesProvider([
new Facile\CodingStandards\Rules\DefaultRulesProvider(),
new Facile\CodingStandards\Rules\ArrayRulesProvider([
'array_push' => true,
'no_whitespace_in_empty_array' => true,
]),
]);
Symfony/Laravel Compatibility:
--no-risky flag with facile-cs-create-config to avoid Symfony-specific rules:
composer facile-cs-create-config --no-risky
PHPStorm Integration:
php-cs-fixer via:
Settings > Editor > Code Style > PHP > PHP-CS-Fixer..php-cs-fixer.dist.php.Risky Rules:
class_keyword or long_to_shorthand_operator may break existing code.PHP Version Mismatches:
php-cs-fixer version and PHP runtime:
composer require php-cs-fixer:^3.88
Autoload Paths:
psr-4, psr-0, or classmap. Custom autoloads (e.g., files) may be missed.$finder in .php-cs-fixer.dist.php:
$finder->in(__DIR__ . '/custom-path');
Caching Issues:
$config->setUsingCache(false)) may slow down checks but ensures up-to-date rules.composer facile-cs-create-config
Heredoc Rules:
heredoc_to_nowdoc or multiline_string_to_heredoc are marked "to be discussed" and may change behavior.$config->setRules([
'heredoc_to_nowdoc' => false,
]);
Dry Runs:
Always use --dry-run first to preview changes:
composer cs-check -- --dry-run --diff
Verbose Output: Enable debug mode for detailed rule explanations:
php-cs-fixer fix --verbose
Rule-Specific Fixes: Target a single rule to debug:
php-cs-fixer fix --rules=phpdoc_summary
Config Validation:
Validate your .php-cs-fixer.php for syntax errors:
php -l .php-cs-fixer.php
Custom Rulesets:
Create a project-specific ruleset by extending CompositeRulesProvider:
class AppRulesProvider extends Facile\CodingStandards\Rules\CompositeRulesProvider {
public function __construct() {
parent::__construct([
new Facile\CodingStandards\Rules\DefaultRulesProvider(),
new Facile\CodingStandards\Rules\ArrayRulesProvider([
'no_whitespace_in_empty_array' => true,
'array_push' => false, // Disable for legacy code
]),
]);
}
}
Rector Integration: Use Rector (added in v1.5.0) for refactoring alongside CS fixes:
composer require --dev rector/rector
Configure in rector.php:
return static function (RectorConfig $config): void {
$config->paths([__DIR__ . '/src']);
$config->rules([
new SetTypeToCast(),
new TernaryToElvisOperator(),
]);
};
GitHub Actions:
Cache php-cs-fixer between runs to speed up CI:
- name: Cache PHP-CS-Fixer
uses: actions/cache@v3
with:
path: ~/.cache/php-cs-fixer
key: ${{ runner.os }}-php-cs-fixer
IDE Shortcuts:
Bind composer cs-fix to a keyboard shortcut in PHPStorm for quick fixes:
Settings > Keymap > Other > Run External Tool.composer cs-fix.How can I help you explore Laravel packages today?