phpyh/coding-standard
Opinionated PHP coding standard for Laravel projects, built on PHP-CS-Fixer with ready-to-use rules, presets, and a simple CLI/workflow to automatically format code and keep style consistent across your team and CI.
Installation Add the package via Composer:
composer require --dev phpyh/coding-standard
Register the provider in config/app.php (if not auto-discovered):
'providers' => [
Phpyh\CodingStandard\CodingStandardServiceProvider::class,
],
Basic Usage Run the coding standard checker via Artisan:
php artisan coding-standard:check
This scans your project for violations against predefined rules (e.g., PSR-12, custom Laravel conventions).
First Use Case Integrate into your CI pipeline (e.g., GitHub Actions) to enforce consistency:
- name: Run Coding Standard
run: php artisan coding-standard:check --fix
Pre-Commit Hooks
Use husky or Laravel’s pre-commit to run checks before commits:
php artisan coding-standard:check --format=json > coding-standard.json
Add a Git hook to fail if violations exist.
IDE Integration Configure PHPStorm to use the coding standard:
Settings > Editor > Code Style > PHP..php_cs or .php_cs.dist file (if provided).Custom Rules Extend the default rules by publishing the config:
php artisan vendor:publish --provider="Phpyh\CodingStandard\CodingStandardServiceProvider" --tag="config"
Modify config/coding-standard.php to add/override rules (e.g., line length, namespace prefixes).
Automated Fixes
Use the --fix flag to auto-correct fixable issues:
php artisan coding-standard:check --fix
Parallel Processing
For large codebases, leverage parallel checks with --parallel (if supported):
php artisan coding-standard:check --parallel=4
False Positives
App\Services\) may trigger unnecessary warnings. Override in config:
'rules' => [
'namespaces' => [
'prefix' => 'App\\',
'validate_root_namespace' => false,
],
],
Performance
vendor/, storage/) improves speed:
php artisan coding-standard:check --exclude="vendor,storage"
Config Overrides
PHP Version Compatibility
8.1). Some rules may behave differently across versions.Verbose Output
Use --verbose to debug rule application:
php artisan coding-standard:check --verbose
JSON Output For CI integration, parse JSON output:
php artisan coding-standard:check --format=json > report.json
Custom Rulesets
Create a ruleset.xml (if supported) or extend the Rule classes in app/Rules:
namespace App\Rules;
use Phpyh\CodingStandard\Rules\AbstractRule;
class CustomRule extends AbstractRule {
protected function getPattern() { return '...'; }
protected function getFix() { return '...'; }
}
Event Listeners
Hook into coding-standard.checked events to log or notify:
CodingStandard::addListener(function ($violations) {
Log::info('Coding standard violations:', $violations);
});
Integration with PHP-CS-Fixer
If the package uses UnderlyingPHP/PhpCsFixer, leverage its .php-cs-fixer.dist.php for advanced configs.
Dry Runs
Use --dry-run to preview changes before applying fixes:
php artisan coding-standard:check --fix --dry-run
Team Onboarding
Include a CODING_STANDARD.md in your repo with:
Partial Checks Target specific files/directories:
php artisan coding-standard:check app/Http/Controllers
How can I help you explore Laravel packages today?