lookyman/coding-standard
Opinionated PHP coding standard package for your projects. Install via Composer and reference the bundled ruleset.xml in PHP_CodeSniffer to enforce consistent formatting and style, with room to add your own project-specific rules.
Installation Add the package via Composer:
composer require --dev lookyman/coding-standard
Register the provider in config/app.php (if not auto-discovered):
'providers' => [
Lookyman\CodingStandard\CodingStandardServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Lookyman\CodingStandard\CodingStandardServiceProvider"
Edit config/coding-standard.php to define your ruleset (e.g., PSR-12, custom rules).
First Use Case Run the coding standard checker on a single file:
php artisan coding-standard:check app/Http/Controllers/ExampleController.php
Or check an entire directory (e.g., app/):
php artisan coding-standard:check app/
Integrate with CI
Add to your .gitlab-ci.yml or Travis.yml:
script:
- php artisan coding-standard:check app/
Pre-Commit Hooks
Use php artisan coding-standard:check in a pre-commit hook (e.g., via husky or pre-commit) to catch violations early:
# .git/hooks/pre-commit
#!/bin/sh
php artisan coding-standard:check --fix
Fixing Issues Automatically Enable auto-fixing where possible:
php artisan coding-standard:check --fix app/
(Note: Only applies to rules supporting fixes, e.g., indentation, spacing.)
Custom Rulesets Extend the default ruleset by creating a custom config:
// config/coding-standard.php
'rules' => [
'PSR12' => true,
'custom' => [
'Lookyman\CodingStandard\Rules\NoHardTabs' => true,
],
],
Integration with PHPStan/PHPMD Combine with other tools for deeper analysis:
php artisan coding-standard:check app/ && vendor/bin/phpstan analyse app/
Parallel Processing For large codebases, split checks across files/directories:
find app/ -name "*.php" | xargs -P 4 php artisan coding-standard:check
Laravel IDE Helper Run checks before generating IDE helpers:
php artisan coding-standard:check app/ && vendor/bin/ide-helper:generate
Custom Artisan Commands
Extend the package’s commands in app/Console/Commands:
use Lookyman\CodingStandard\Commands\CheckCommand;
class CustomCheckCommand extends CheckCommand {
protected $signature = 'custom:check {path}';
// Override logic as needed
}
GitHub Actions Cache Composer dependencies for faster CI runs:
jobs:
coding-standard:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-php@v2
- run: composer install
- run: php artisan coding-standard:check app/
Visual Studio Code Integration
Use the PHP Intelephense extension to highlight violations in real-time (requires manual rule mapping).
Outdated Package
php-cs-fixer to v3.x).No Auto-Discovery
config/app.php or use a package like laravel-package-discovery.Limited Rule Support
// config/coding-standard.php
'rules' => [
'PSR12' => [
'rules' => [
'@PSR12' => true,
'no_unused_imports' => true,
],
],
],
php-cs-fixer directly if advanced rules are needed.Performance on Large Codebases
vendor/ and node_modules/ in .gitignore.False Positives
'exclude' => [
'app/Legacy/',
'config/custom.php',
],
Verbose Output Enable debug mode for detailed errors:
php artisan coding-standard:check --verbose app/
Dry Run Test changes without fixing:
php artisan coding-standard:check --dry-run app/
Rule-Specific Checks Isolate rule violations:
php artisan coding-standard:check --rule=PSR12 app/
Custom Rules
Create a new rule by extending Lookyman\CodingStandard\AbstractRule:
namespace App\Rules;
use Lookyman\CodingStandard\AbstractRule;
class CustomRule extends AbstractRule {
public function getName() { return 'custom_rule'; }
public function process(\PhpParser\Node $node) { /* ... */ }
}
Register in config/coding-standard.php:
'rules' => [
'custom_rule' => App\Rules\CustomRule::class,
],
Override Default Rules Disable/enable rules dynamically:
// In a command or service
$checker = app('coding-standard');
$checker->disableRule('PSR12');
$checker->enableRule('no_hard_tabs');
Event Listeners Listen for check events (e.g., to log violations):
// app/Providers/EventServiceProvider.php
protected $listen = [
'coding-standard.violations' => [
\App\Listeners\LogViolations::class,
],
];
Path Handling
config/coding-standard.php for reliability:
'paths' => [
base_path('app'),
base_path('config'),
],
Case Sensitivity
Caching
vendor/ to speed up runs:
# .gitlab-ci.yml
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/
How can I help you explore Laravel packages today?