onramplab/onr-phpcs-laravel
Opinionated PHP_CodeSniffer ruleset for Laravel projects. Provides a ready-to-use PHPCS configuration with Laravel-focused coding standards to help keep code style consistent across your app and team.
Installation
composer require --dev onramplab/onr-phpcs-laravel
Add to composer.json under require-dev:
"scripts": {
"test": [
"phpcs --standard=./vendor/onramplab/onr-phpcs-laravel/ruleset.xml"
]
}
First Run Execute in project root:
composer test
Or directly:
phpcs --standard=./vendor/onramplab/onr-phpcs-laravel/ruleset.xml app/
Key Files
ruleset.xml: Core ruleset (extends PSR12 + custom Laravel rules).Sniffs/: Custom sniff classes (e.g., LaravelControllerSniff.php).Pre-Commit Hook
Integrate with phpcs in .git/hooks/pre-commit:
#!/bin/sh
composer test || exit 1
CI/CD Pipeline
Add to GitHub Actions (.github/workflows/lint.yml):
- name: Run PHPCS
run: composer test
Custom Rulesets
Extend the default ruleset by creating a custom ruleset.xml:
<ruleset>
<config name="installedPaths" value="./vendor/onramplab/onr-phpcs-laravel"/>
<config name="extensions" value="php"/>
<rule ref="ruleset.xml">
<exclude name="Laravel.Controller.MethodLength"/>
</rule>
</ruleset>
barryvdh/laravel-ide-helper for consistent docblock standards.Settings > PHP > Quality Tools > PHP_CodeSniffer.Default Sniffer to the package’s ruleset.xml.phpcs --generate-baseline to create a baseline for new contributors.Rule Overrides
phpcs --standard=PSR12 app/ # Compare against PSR12 baseline
ruleset.xml if needed.Performance
vendor/ and node_modules/ from scans to speed up CI:
phpcs --standard=ruleset.xml --ignore=vendor/,node_modules/ app/
False Positives
$fillable) may trigger Generic.Files.LineLength. Adjust Generic.Files.LineLength.MaximumLineLength in ruleset.xml:
<rule ref="Generic.Files.LineLength">
<properties>
<property name="maximumLineLength" value="120"/>
</properties>
</rule>
--verbose to diagnose rule failures:
phpcs --standard=ruleset.xml --verbose app/Http/Controllers/
Sniffs/ directory for rule documentation (e.g., LaravelControllerSniff.php comments).Add Custom Sniffs
Extend the ruleset by creating a new sniff class in app/CodeSniffer/:
class CustomLaravelSniff extends \PHP_CodeSniffer\Standards\AbstractStandard {
public function __construct() {
parent::__construct();
$this->register();
}
}
Update ruleset.xml to include it:
<rule ref="CustomLaravelSniff"/>
Modify Existing Rules
Override sniff classes in your project (e.g., app/CodeSniffer/Sniffs/LaravelControllerSniff.php) and reference them in a custom ruleset.xml.
Configuration
Use phpcs --config-set to tweak settings dynamically:
phpcs --config-set installed_paths ./app/CodeSniffer
How can I help you explore Laravel packages today?