brandembassy/coding-standard
Opinionated PHP coding standard ruleset for enforcing consistent code style across projects. Built on common tooling (e.g., PHP_CodeSniffer) to simplify linting in CI and local development, helping teams keep formatting and conventions uniform.
Install via Composer
composer require --dev brandembassy/coding-standard
Add to your project’s composer.json under require-dev if not auto-detected.
Integrate with PHP-CS-Fixer
Create/update php-cs-fixer.dist.php:
<?php
return (new PhpCsFixer\Config())
->setRules(include __DIR__.'/vendor/brandembassy/coding-standard/ruleset.php')
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
);
First Use Case: Local Fixes Run in your project root:
vendor/bin/php-cs-fixer fix
Verify changes with:
vendor/bin/php-cs-fixer fix --dry-run
CI Integration
Add to .github/workflows/lint.yml (example):
- name: Run PHP-CS-Fixer
run: vendor/bin/php-cs-fixer fix --diff --dry-run
Team Onboarding
composer.json (or private repo).CONTRIBUTING.md.Laravel-Specific Tweaks
use App\ imports):
// php-cs-fixer.dist.php
->setRules(array_merge(
include __DIR__.'/vendor/brandembassy/coding-standard/ruleset.php',
['ordered_imports' => ['sort_algorithm' => 'alpha']]
))
Static Analysis (PHPStan/Psalm)
brandembassy/coding-standard’s phpstan.neon (if provided):
vendor/bin/phpstan analyse --level=max src/
Pre-Commit Hooks
Use php-cs-fixer via husky or pre-commit:
# .pre-commit-config.yaml
- repo: local
hooks:
- id: php-cs-fixer
name: PHP-CS-Fixer
entry: vendor/bin/php-cs-fixer fix
language: system
types: [php]
php-cs-fixer.dist.php:
->setRules([
'@brandembassy' => true,
'array_syntax' => ['syntax' => 'short'],
])
jobs:
lint-frontend:
run: vendor/bin/php-cs-fixer fix app/Http/Controllers/
lint-backend:
run: vendor/bin/php-cs-fixer fix app/Models/
.vscode/settings.json:
{
"php-cs-fixer.executablePath": "vendor/bin/php-cs-fixer",
"editor.formatOnSave": true,
"editor.defaultFormatter": "bmewburn.vscode-intelephense"
}
Rule Conflicts
php-cs-fixer fails with Rule "X" is not defined.@brandembassy is loaded first in your ruleset. Example:
->setRules([
'@brandembassy' => true,
'no_unused_imports' => true, // Override specific rules
])
PHP 8.4 Compatibility
Rector proposals or PHP 8.4 features in your codebase.php-cs-fixer and rector dependencies to ensure compatibility:
composer require --dev php-cs-fixer:^3.15 rector/rector:^0.16
8.4 in php-cs-fixer.dist.php:
->setRiskyAllowed(true) // If needed for PHP 8.4 features
Performance in Large Projects
php-cs-fixer fix.vendor/bin/php-cs-fixer fix --parallel
--dry-run in CI for speed, then run full fix in a separate job.Ignoring Files
vendor/ or node_modules/.Finder:
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude(['vendor', 'node_modules', 'storage'])
)
Rule Version Drift
brandembassy/coding-standard releases break existing code.composer.json or test upgrades in a branch:
composer require brandembassy/coding-standard:^14.8
Dry-Run with Verbosity:
vendor/bin/php-cs-fixer fix --dry-run -v
Outputs diffs and skipped files.
Rule-Specific Debugging:
vendor/bin/php-cs-fixer fix --rules=@brandembassy --dry-run
PHP 8.4 Debugging:
vendor/bin/php-cs-fixer fix --allow-risky=yes
Custom Rules
Extend the ruleset by creating a local ruleset.php:
// custom-ruleset.php
return [
'@brandembassy' => true,
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => false],
];
Then reference it in php-cs-fixer.dist.php:
->setRules(include __DIR__.'/custom-ruleset.php')
CI-Specific Configs Use environment variables to toggle strictness:
// php-cs-fixer.dist.php
$rules = include __DIR__.'/vendor/brandembassy/coding-standard/ruleset.php';
if (getenv('CI')) {
$rules['risky_nullable_type_declaration'] = true;
}
return (new PhpCsFixer\Config())->setRules($rules);
Visual Studio Code Snippets
Combine with php-cs-fixer to enforce snippets:
// .vscode/settings.json
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.php-cs-fixer": true
}
}
Laravel Artisan Command: Create a custom command for quick fixes:
// app/Console/Commands/FixCode.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FixCode extends Command {
protected $signature = 'code:fix';
public function handle() {
$this->call('php-cs-fixer', ['--dry-run' => null]);
}
}
Register in app/Console/Kernel.php:
protected $commands = [
Commands\FixCode::class,
];
GitHub Actions Caching:
- name: Cache PHP-CS-Fixer
uses: actions/cache@v3
with:
path: ~/.cache/php-cs-fixer
key: ${{ runner.os }}-php-cs-fixer-${{ hashFiles('**/composer.lock') }}
PHP 8.4 Migration Check:
Use the updated brandembassy/coding-standard to validate PHP 8.4 compatibility:
vendor/bin/php-cs-fixer fix --rules=@brandembassy --allow-risky=yes
How can I help you explore Laravel packages today?