instaclick/symfony2-coding-standard
PHP_CodeSniffer ruleset for enforcing the Symfony2 Coding Standard in PHP projects. Helps keep formatting and conventions consistent across teams and CI, with ready-to-use sniffs and easy integration into existing development workflows.
Installation Add the package via Composer in your Laravel project:
composer require --dev instaclick/symfony2-coding-standard
Ensure php-cs-fixer is also installed (required dependency):
composer require --dev friendsofphp/php-cs-fixer
Configure php-cs-fixer
Create/update .php-cs-fixer.dist.php in your project root:
<?php
return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true, // Optional: Enable stricter rules
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->exclude('node_modules')
);
First Use Case: Run Linter Execute the fixer on your codebase:
vendor/bin/php-cs-fixer fix
Integrate into your CI pipeline (e.g., GitHub Actions) to enforce consistency.
Pre-Commit Hook
Use php-cs-fixer as a pre-commit hook to catch formatting issues early:
composer require --dev laravel-pint
vendor/bin/pint --test # Dry run
vendor/bin/pint # Auto-fix
Add to .git/hooks/pre-commit:
#!/bin/sh
vendor/bin/php-cs-fixer fix --dry-run || exit 1
CI/CD Integration Fail builds on violations:
# .github/workflows/lint.yml
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/php-cs-fixer fix --dry-run --diff
Team Onboarding
CONTRIBUTING.md.php-cs-fixer fix --allow-risky=yes for new contributors to auto-format their PRs.use App vs. use aliases). Override rules in .php-cs-fixer.dist.php:
->setRules([
'no_unused_imports' => false, // Laravel often uses unused `use` for IDE hints
])
// app/Console/Commands/FixCodingStandard.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FixCodingStandard extends Command {
protected $signature = 'code:fix';
public function handle() {
$this->call('vendor:publish', ['--tag' => 'php-cs-fixer']);
$this->info('Running Symfony coding standard...');
shell_exec('vendor/bin/php-cs-fixer fix');
}
}
Register in app/Console/Kernel.php and run with:
php artisan code:fix
Rule Conflicts
@Symfony rules may enforce PSR-12 (e.g., return_type_declaration), which Laravel’s pint (PHP-CS-Fixer wrapper) may override. Solution: Align configurations or disable conflicting rules.no_unused_imports if your team relies on IDE-specific use statements.Performance
php-cs-fixer. Solution:
--parallel flag (PHP-CS-Fixer 3.0+):
vendor/bin/php-cs-fixer fix --parallel
--cache-file=.php-cs-fixer.cache.False Positives
no_superfluous_phpdoc_tags may flag Laravel-specific annotations (e.g., @mixin). Solution: Whitelist files or override the rule:
->setRules([
'no_superfluous_phpdoc_tags' => [
'remove_inheritdoc' => false,
'allow_mixed' => true,
],
])
vendor/bin/php-cs-fixer fix --dry-run --diff
vendor/bin/php-cs-fixer fix -v
vendor/bin/php-cs-fixer fix --rules=@Symfony --stop-on-violation
Custom Rules Extend the Symfony standard by adding project-specific rules:
->setRules([
'@Symfony' => true,
'custom_rule' => true, // Hypothetical; requires PHP-CS-Fixer custom rule
])
Integration with Laravel Tools
laravel/pint, merge configs:
composer require laravel/pint --dev
vendor/bin/pint --config=.php-cs-fixer.dist.php
vendor/bin/phpstan analyse --level=max
vendor/bin/php-cs-fixer fix
Visual Studio Code
Add to .vscode/settings.json for real-time feedback:
{
"php-cs-fixer.executablePath": "vendor/bin/php-cs-fixer",
"php-cs-fixer.rules": "@Symfony",
"editor.formatOnSave": true
}
vendor/bin/php-cs-fixer fix app/Http/Controllers/
composer.json to avoid rule changes:
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
}
vendor/bin/php-cs-fixer fix --dry-run --format=diff > CODESTYLE_REPORT.md
How can I help you explore Laravel packages today?