chaplean/coding-standard
Chaplean Coding Standard provides a ready-to-use PHP_CodeSniffer ruleset for consistent PHP and Laravel code style. Drop-in configuration to enforce formatting, naming conventions, and best practices across your project and CI.
Installation Add the package as a dev dependency:
composer require --dev chaplean/coding-standard
Configure PHPCS
Create or update .phpcs.xml in your project root:
<?xml version="1.0"?>
<ruleset name="Project Standards">
<config name="installedPackages" value="chaplean/coding-standard"/>
<rule ref="Chaplean"/>
</ruleset>
First Execution Run PHPCS on your Laravel app directory:
vendor/bin/phpcs --standard=Chaplean app/
Auto-Fix Common Issues
Use phpcbf for quick fixes:
vendor/bin/phpcbf --standard=Chaplean app/
vendor/chaplean/coding-standard/ruleset.xml for custom rules.CI/CD Integration Add to your GitHub Actions workflow:
- name: PHP Coding Standards
run: |
vendor/bin/phpcs --standard=Chaplean \
--warning-severity=3 \
--errors-on-warning \
--extensions=php \
app/
Pre-Commit Hook Use with Laravel Pint or Pre-Commit:
# .pre-commit-config.yaml
- repo: local
hooks:
- id: phpcs
name: Chaplean Coding Standards
entry: vendor/bin/phpcs --standard=Chaplean
language: system
types: [php]
args: ["--extensions=php", "app/"]
IDE Setup
Settings > Editor > Code Style > PHP > PHP Code Sniffer and add the standard.phpcs.executablePath in settings.json:
{
"phpcs.executablePath": "vendor/bin/phpcs",
"phpcs.standard": "Chaplean"
}
Custom Rule Overrides
Extend or disable rules in .phpcs.xml:
<rule ref="Chaplean">
<exclude name="Generic.Files.LineEndings"/>
<arg name="extensions" value="php,blade"/>
</rule>
Exclude Blade Templates
Add to .phpcs.xml:
<file>./resources/views/</file>
<exclude-pattern>.*\.blade\.php$</exclude-pattern>
Focus on Critical Paths
Run PHPCS only on app/ and config/ directories:
vendor/bin/phpcs --standard=Chaplean --extensions=php app/ config/
Diff Mode for PRs Check only changed files in a PR:
vendor/bin/phpcs --standard=Chaplean --diff app/
Rule Conflicts with PSR-12
vendor/bin/phpcs --standard=PSR12 app/ | head -n 20
vendor/bin/phpcs --standard=Chaplean app/ | head -n 20
<rule ref="PSR12"/> alongside Chaplean and resolve overlaps.Blade Template Issues
<exclude-pattern>.*/resources/views/.*</exclude-pattern>
Performance in Large Projects
vendor/bin/phpcs --standard=Chaplean --cache=~/.phpcs_cache app/
False Positives in Facades
Cache::get()) may trigger generic rules. Override:
<exclude name="Generic.Files.LineLength.TooLong"/>
Verbose Output
Run with -v for detailed errors:
vendor/bin/phpcs -v --standard=Chaplean app/
Rule-Specific Debugging Isolate a rule:
vendor/bin/phpcs --standard=Chaplean --ruleset=Chaplean.Generic app/
IDE Debugging For VSCode/PHPStorm, enable PHPCS logging:
{
"phpcs.logLevel": "debug"
}
Case Sensitivity
Ensure .phpcs.xml paths use forward slashes (/), even on Windows.
Composer Autoload If rules aren’t loading, run:
composer dump-autoload
3. **PHPCS Version Mismatch**
Pin `phpcs` version in `composer.json`:
```json
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
}
Custom Rules Add project-specific rules by extending the ruleset:
<rule ref="Chaplean">
<config name="customRule" value="your_rule_path"/>
</rule>
Sniff Development
Create a custom sniff in app/CodeSniffer/ and reference it:
<rule ref="app/CodeSniffer/YourSniff"/>
CI-Specific Adjustments Use environment variables to toggle strictness:
- run: vendor/bin/phpcs --standard=Chaplean --${{ env.STRICT_MODE == 'true' && 'error-severity=3' || 'warning-severity=3' }} app/
Start with Warnings
Begin with --warning-severity=3 in CI to avoid blocking PRs.
Document Exceptions
Use @codingStandardsIgnoreStart in files for justified violations:
// @codingStandardsIgnoreStart
$legacyCode = "..."; // Ignore for legacy reasons
// @codingStandardsIgnoreEnd
Pair with Laravel Pint
Use pint for formatting and Chaplean for standards:
composer require --dev laravel/pint
vendor/bin/pint --test
vendor/bin/phpcs --standard=Chaplean app/
How can I help you explore Laravel packages today?