cakephp/cakephp-codesniffer
CakePHP coding standard for PHP_CodeSniffer. Fully PSR-12 compliant plus additional CakePHP-specific sniffs and fixers. Install via Composer and run phpcs with --standard=CakePHP (or use a phpcs.xml) to check and auto-fix your code.
Installation Update to the latest version via Composer (now fully compatible with PHP_CodeSniffer 4.0+):
composer require --dev cakephp/cakephp-codesniffer:^5.3
Ensure PHP_CodeSniffer 4.0+ is installed (no need for separate installation if using Composer):
composer require --dev squizlabs/php_codesniffer:^4.0
First Run
Run PHPCS with the CakePHP standard (remove all installed_paths references from config):
vendor/bin/phpcs --standard=CakePHP app/
--report=full for detailed output.--extensions=php to limit checks to PHP files.Integration with Laravel
Add a phpcs command to composer.json scripts:
"scripts": {
"test:phpcs": "phpcs --standard=CakePHP app/"
}
php artisan phpcs
Pre-Commit Hooks Use PHPCS 4.0+ in a Git pre-commit hook (adjust severity levels as needed):
# .git/hooks/pre-commit
#!/bin/sh
vendor/bin/phpcs --standard=CakePHP --warning-severity=3 --error-severity=5 app/ || exit 1
--warning-severity/--error-severity instead of deprecated flags.CI/CD Pipeline Integrate into GitHub Actions with PHPCS 4.0’s JSON output:
# .github/workflows/phpcs.yml
jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer install
- run: vendor/bin/phpcs --standard=CakePHP --report=json app/ > phpcs-results.json
phpcs-github-pr-comment.Custom Rulesets (PHPCS 4.0+)
Create a .phpcs.xml without installed_paths (fully deprecated in PHPCS 4.0):
<config name="cache" value="true"/>
<rule ref="CakePHP">
<exclude name="CakePHP.NamingConventions.ValidClassNamePrefix.UpperCasePrefix"/>
</rule>
--config-file=.phpcs.xml.Laravel-Specific Adjustments
App namespace:
<rule ref="CakePHP.NamingConventions.ValidClassNamePrefix">
<properties>
<property name="prefix" value="App\\|Illuminate\\"/>
</properties>
</rule>
config/, database/).Critical: installed_paths Deprecation
installed_paths support. Old configs will fail with:
Warning: The 'installed_paths' config option is no longer supported.
installed_paths entries from .phpcs.xml and .phpcs.dist.xml.
PHPCS 4.0 auto-detects the CakePHP standard via Composer autoload.False Positives in Laravel
ValidClassNamePrefix may flag Laravel’s RouteServiceProvider or AppServiceProvider.vendor/bin/phpcs --standard=CakePHP --ignore=app/Console/Kernel.php app/
Or in .phpcs.xml:
<file>app/Console/Kernel.php</file>
Performance with PHPCS 4.0
.phpcs.xml:
<config name="cache" value="true"/>
--parallel=4 (if supported) for parallel processing:
vendor/bin/phpcs --standard=CakePHP --parallel=4 app/
Dynamic Class Names in Laravel
ValidClassNamePrefix may fail on Laravel’s dynamic classes (e.g., Eloquent models)..phpcs.xml:
<rule ref="CakePHP.NamingConventions.ValidClassNamePrefix">
<properties>
<property name="prefix" value="App\\|Generated\\"/>
</properties>
</rule>
--verbose to debug rule application:
vendor/bin/phpcs --standard=CakePHP --verbose app/Models/User.php
vendor/bin/phpcs --standard=CakePHP --list-rules
vendor/bin/phpcs app/Http/Controllers/UserController.php
Custom Rules with PHPCS 4.0
Extend PHPCS by creating a Custom standard in vendor/custom-codesniffer/CakePHP/Sniffs/:
mkdir -p vendor/custom-codesniffer/CakePHP/Sniffs/
.phpcs.xml:
<rule ref="Custom"/>
Autofix with phpcbf
Use PHPCS’s built-in fixer (test in a branch first):
vendor/bin/phpcbf --standard=CakePHP --report=diff app/
IDE Integration
vendor/bin/phpcs and ensure PHPCS 4.0+ is selected.GitHub PR Comments
Use phpcs-github-pr-comment with PHPCS 4.0’s JSON output:
vendor/bin/phpcs --standard=CakePHP --report=json app/ > phpcs.json
vendor/bin/phpcs-github-pr-comment --file=phpcs.json
How can I help you explore Laravel packages today?