Install via Composer:
composer require braincrafted/symfony2cs-bundle:dev-master
(Note: Use dev-master as the package is outdated but functional.)
Enable the Bundle:
Add to app/AppKernel.php:
new Braincrafted\Bundle\Symfony2CSBundle\BraincraftedSymfony2CSBundle(),
First Use Case: Run PHP_CodeSniffer with Symfony2 standard:
php vendor/bin/phpcs --standard=Symfony2 src/
(The bundle auto-installs the Symfony2 coding standard for PHP_CodeSniffer.)
Integrate into CI/CD:
Add to .gitlab-ci.yml or .github/workflows/:
test:phpcs:
script:
- php vendor/bin/phpcs --standard=Symfony2 --warning-severity=3 src/
Customize Rules:
Override default rules via phpcs.xml:
<config name="installed_paths" value="vendor/braincrafted/symfony2cs-bundle/Symfony2"/>
<rule ref="Symfony2">
<exclude name="Symfony2.Arrays.ArrayPush"/>
</rule>
Laravel-Specific Use:
// app/Console/Commands/RunPHPCS.php
public function handle()
{
$this->call('phpcs', [
'command' => 'run',
'--standard' => 'Symfony2',
'--extensions' => 'php',
'path' => 'app/'
]);
}
app/Console/Kernel.php:
protected $commands = [
Commands\RunPHPCS::class,
];
Pre-Commit Hook:
Use husky or pre-commit to auto-run PHPCS:
# .husky/pre-commit
#!/bin/sh
php vendor/bin/phpcs --standard=Symfony2 --colors --report=full src/ || exit 1
Composer Scripts: Add to composer.json:
"scripts": {
"phpcs": "php vendor/bin/phpcs --standard=Symfony2 src/"
}
Run with:
composer phpcs
IDE Integration: Configure PHPStorm to use the Symfony2 standard:
Settings > Languages & Frameworks > PHP > Code Sniffer.Default severity level to warning.vendor/braincrafted/symfony2cs-bundle/Symfony2 to Installed paths.Outdated Package:
Path Conflicts:
phpcs.xml:
<config name="installed_paths" value="vendor/braincrafted/symfony2cs-bundle/Symfony2"/>
php vendor/bin/phpcs -i
Rule Overrides:
Symfony2.Arrays.ArrayPush) may be too strict for Laravel. Exclude them in phpcs.xml or create a custom standard.Composer Dependency:
composer.json:
"require-dev": {
"squizlabs/php_codesniffer": "^3.6"
}
php vendor/bin/phpcs --standard=Symfony2 --verbose src/
php vendor/bin/phpcs -i
php vendor/bin/phpcs --standard=Symfony2 app/Http/Controllers/ExampleController.php
Custom Standards:
Fork the Symfony2-coding-standard repo and extend it for Laravel-specific needs (e.g., relax rules for use statements in controllers).
Parallel Processing:
Speed up PHPCS with parallel:
php vendor/bin/phpcs --standard=Symfony2 --parallel=8 src/
Git Hooks:
Use pre-push to catch issues before merging:
# .git/hooks/pre-push
#!/bin/sh
php vendor/bin/phpcs --standard=Symfony2 --warning-severity=3 src/ || exit 1
Visual Studio Code:
Add to .vscode/settings.json:
"php.validate.run": "onType",
"php.cs.fixer.rules": "@Symfony",
"php.cs.snifferConfigPath": "vendor/braincrafted/symfony2cs-bundle/Symfony2/phpcs.xml"
How can I help you explore Laravel packages today?