respect/coding-standard
Respect Coding Standard provides a set of PHP_CodeSniffer rules used across Respect components, helping enforce consistent code style and best practices in PHP projects.
Install the Package
composer require --dev respect/coding-standard
This installs the Respect Coding Standard ruleset as a dependency.
Run PHP_CodeSniffer Execute the standard against your Laravel project:
vendor/bin/phpcs --standard=Respect src/
src/ with your target directory (e.g., app/ for Laravel-specific checks).--report=summary for a quick overview or --report=full for detailed output.First Use Case
CI/CD Pipeline Integration Add to your GitHub Actions or GitLab CI:
- name: Enforce Respect Coding Standard
run: vendor/bin/phpcs --standard=Respect --warning-severity=0 src/ || exit 1
vendor/, storage/, and bootstrap/cache/:
vendor/bin/phpcs --standard=Respect --ignore="vendor/,storage/,bootstrap/cache/" src/
Pre-Commit Hook
Use husky or a custom script to block commits with violations:
# .husky/pre-commit
#!/bin/sh
vendor/bin/phpcs --standard=Respect --report=summary src/ || exit 1
IDE Integration Configure PHPStorm or VSCode to use the Respect standard:
phpcs.xml path in Settings > Editor > Inspections > PHP > PHP_CodeSniffer.PHP Intelephense extension with phpcs.xml configured.Hybrid Standards Combine with Laravel’s PSR-12 or custom rules:
<!-- phpcs.xml -->
<ruleset>
<rule ref="Respect"/>
<rule ref="PSR12">
<exclude name="PSR12.Files.SideEffects"/>
</rule>
</ruleset>
Exclude Blade Files Respect’s standard doesn’t handle Blade templates. Exclude them:
vendor/bin/phpcs --standard=Respect --ignore="*.blade.php" src/
Facade and Helper Methods
Respect may flag Laravel’s app() or facades. Override in phpcs.xml:
<exclude name="Respect.NamingConventions.ValidMethodName" from="app/Helpers/*.php"/>
Artisan Command Naming
Respect enforces SnakeCase for methods. Laravel’s handle() may conflict. Adjust severity:
<arg name="severity" value="2" name="Respect.NamingConventions.ValidMethodName"/>
Strict Naming Conventions
snake_case for methods (e.g., get_user()), conflicting with Laravel’s camelCase (e.g., getUser()).<exclude name="Respect.NamingConventions.SnakeCaseMethods"/>
False Positives in Legacy Code
AppServiceProvider class names).--ignore or patch violations incrementally:
vendor/bin/phpcs --standard=Respect --ignore="app/Providers/AppServiceProvider.php" src/
Performance Overhead
phpcs.vendor/bin/phpcs --standard=Respect --parallel=4 src/
Dependency Conflicts
doctrine/coding-standard (a transitive dependency) may introduce rule conflicts.composer.json.Detailed Error Output
Use --report=full to diagnose specific violations:
vendor/bin/phpcs --standard=Respect --report=full src/ | grep "ERROR"
Rule-Specific Help List available rules:
vendor/bin/phpcs --standard=Respect --list-rules
Dry Run with Fixes Test auto-fixes (where supported):
vendor/bin/phpcs --standard=Respect --fix --dry-run src/
Custom Rules
Extend the standard by adding your own rules to phpcs.xml:
<rule ref="Respect">
<arg name="extensions" value="php,blade.php"/>
</rule>
Sniff Integration
Create custom sniffs for Laravel-specific patterns (e.g., Route::resource naming):
// CustomSniff.php
class LaravelRouteSniff implements Sniff {
public function register() {
return [T_WHITESPACE, T_DOC_COMMENT_OPEN_TAG];
}
}
Configuration Overrides
Use environment-specific configs (e.g., phpcs.dev.xml vs. phpcs.prod.xml) to adjust severity:
<arg name="severity" value="5" name="Respect.NamingConventions.ValidClassName"/>
Service Provider Naming
Respect may flag AppServiceProvider as invalid. Exclude:
<exclude-pattern>*/Providers/AppServiceProvider.php</exclude-pattern>
Facade Usage
Respect’s rules may target Facade::class. Suppress with:
<exclude name="Respect.NamingConventions.ValidClassName" from="app/Facades/*.php"/>
Migration Files
Exclude or adjust rules for database/migrations:
vendor/bin/phpcs --standard=Respect --ignore="database/migrations/" src/
How can I help you explore Laravel packages today?