Install the package in your Laravel project:
composer require whatwedo/php-coding-standard --dev
Run a basic check against your Laravel app (e.g., app/ directory) using the Symfony preset:
vendor/bin/ecs check app/ --config vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php
whatwedo-wordpress.php.whatwedo-common.php.Fix automatically (if using ecs ≥ v9.0):
vendor/bin/ecs fix app/ --config vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php
Integrate into your workflow via a Git pre-commit hook (e.g., using pre-commit):
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: php-coding-standard
name: PHP Coding Standard
entry: vendor/bin/ecs check app/ --config vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php
language: system
pass_filenames: false
stages: [commit]
Laravel-Specific Customization
Extend the Symfony preset (whatwedo-symfony.php) in your project’s ecs.php to:
$ecsConfig->skip([
__DIR__.'/../database/migrations/*.php',
__DIR__.'/../bootstrap/cache/*',
]);
use sorting for Laravel classes):
$ecsConfig->ruleWithConfiguration(
\Symplify\EasyCodingStandard\ValueObject\Option\SetList::FIXER,
\NunoMaduro\Collision\Rule\OrderedUseStatementRule::class,
[
'order' => \NunoMaduro\Collision\ValueObject\OrderedUseStatement::LARAVEL_ORDER,
]
);
Team Onboarding
CONTRIBUTING.md:
## Coding Standards
Run `vendor/bin/ecs check app/` to validate your changes.
# .github/workflows/ecs.yml
name: PHP Coding Standard
on: [push, pull_request]
jobs:
ecs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/ecs check app/ --config vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php
Incremental Adoption
ecs check --no-cache).ecs fix for low-impact rules (e.g., spacing, line breaks).False Positives in Laravel
UnusedPrivateMethod may flag Laravel-generated methods (e.g., handle() in controllers).$ecsConfig->skip([
\SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateMethodSniff::class => [
__DIR__.'/../app/Http/Controllers/*',
],
]);
Performance with Large Codebases
ecs check on app/ can be slow for monorepos or large apps.--parallel (requires ecs ≥ v9.0):
vendor/bin/ecs check app/ --parallel --config=...
$ecsConfig->paths([__DIR__.'/../app', '!app/Tests']);
Configuration Overrides
whatwedo-symfony.php with custom rules may cause conflicts.ECSConfig::import() to merge presets:
$ecsConfig->import(__DIR__.'/vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php');
$ecsConfig->ruleWithConfiguration(\Symplify\EasyCodingStandard\ValueObject\Option\SetList::FIXER, ...);
--verbose to diagnose skipped files/rules:
vendor/bin/ecs check app/ --verbose
vendor/bin/ecs check app/ --dry-run
Custom Rules
Add project-specific rules (e.g., enforce Illuminate\Support\Facades\ aliases):
$ecsConfig->ruleWithConfiguration(
\Symplify\EasyCodingStandard\ValueObject\Option\SetList::FIXER,
\WhatWedo\CustomRule\LaravelFacadeRule::class,
['alias' => 'Auth::user()']
);
Dynamic Paths Use Laravel’s service container to define paths dynamically:
$ecsConfig->paths(
array_merge(
[__DIR__.'/../app'],
app()->make(\Illuminate\Filesystem\Filesystem::class)
->glob(__DIR__.'/../modules/*/src')
)
);
CI-Specific Configs Override rules for CI (e.g., stricter checks):
if (getenv('CI')) {
$ecsConfig->ruleWithConfiguration(\Symplify\EasyCodingStandard\ValueObject\Option\SetList::PSR_12, ...);
}
How can I help you explore Laravel packages today?