drupol/php-conventions
Opinionated PHP conventions toolkit with presets for code style, static analysis, and QA tooling. Helps standardize projects quickly by providing shared configuration and automation-friendly defaults for common PHP workflows.
Installation Add the package via Composer:
composer require drupol/php-conventions
No publisher or service provider is required—it’s a standalone utility.
First Use Case Validate a PHP file against PSR-12 conventions:
use Drupol\PhpConventions\Validator;
$validator = new Validator();
$result = $validator->validateFile('/path/to/YourClass.php');
if ($result->isValid()) {
echo "File adheres to PSR-12 conventions!";
} else {
foreach ($result->getErrors() as $error) {
echo "Line {$error['line']}: {$error['message']}\n";
}
}
Where to Look First
src/Rules/ contains individual PSR-12 checks (e.g., ClassDeclarationRule, MethodDeclarationRule).tests/ demonstrates usage patterns and edge cases.File Validation Validate a single file:
$validator = new Validator();
$result = $validator->validateFile('app/Models/User.php');
Directory Validation
Recursively validate all .php files in a directory:
$result = $validator->validateDirectory('app');
Custom Rule Integration
Extend validation with custom rules (e.g., enforce returnType for methods):
use Drupol\PhpConventions\Rules\AbstractRule;
class CustomReturnTypeRule extends AbstractRule {
public function check($node) {
if (!$node->returnType && $node->type === 'method') {
return $this->fail('Method must declare a return type.');
}
return true;
}
}
$validator = new Validator();
$validator->addRule(new CustomReturnTypeRule());
Integration with Laravel Use in a Service Provider or Artisan Command for pre-commit hooks:
// app/Providers/AppServiceProvider.php
use Drupol\PhpConventions\Validator;
public function boot() {
$validator = new Validator();
$validator->validateDirectory(base_path('app'));
}
CI/CD Pipeline Fail builds on convention violations (e.g., GitHub Actions):
- name: Run PHP Conventions Check
run: |
php vendor/bin/php-conventions validate app --fail-on-error
| Use Case | Implementation |
|---|---|
| Pre-commit Hook | Run via php artisan conventions:check |
| CI Linting | Fail pipeline if violations exist |
| IDE Integration | Use as a background checker (e.g., PHPStorm plugin) |
| Code Reviews | Automate PSR-12 checks in PR templates |
False Positives
fillable) may trigger unnecessary errors.$validator->ignoreFiles(['app/Helpers/legacy_helper.php']);
Performance
vendor/) can be slow.vendor/ and focus on app/:
$validator->validateDirectory(app_path());
Rule Conflicts
$validator->setPriority([new CustomRule(), new ClassDeclarationRule()]);
PhpParser Dependency
php-parser (installed automatically via Composer).ext-dom is enabled in php.ini for AST parsing.Verbose Output Enable detailed error messages:
$validator->setVerbose(true);
Log Violations Save results to a file for auditing:
$result = $validator->validateDirectory(app_path());
file_put_contents(storage_path('logs/conventions.log'), $result->toString());
Test Rules Isolated Unit test custom rules:
$this->assertTrue((new CustomRule())->check($astNode));
Custom Rules
Extend AbstractRule to add new checks (e.g., enforce @throws annotations):
class ThrowsAnnotationRule extends AbstractRule {
public function check($node) {
if ($node->type === 'method' && empty($node->docs)) {
return $this->fail('Method must have PHPDoc.');
}
return true;
}
}
Reporters
Implement ReporterInterface to format output (e.g., JSON for APIs):
class JsonReporter implements ReporterInterface {
public function report(Result $result) {
return json_encode($result->getErrors());
}
}
Configuration
Load rules from a config file (e.g., conventions.php):
$validator = new Validator();
$validator->loadRulesFromConfig(config('conventions.rules'));
php-cs-fixer: Use this package for static analysis and php-cs-fixer for automated fixes.// app/Console/Commands/ConventionsCheck.php
public function handle() {
$result = (new Validator())->validateDirectory(app_path());
if (!$result->isValid()) {
$this->error($result->toString());
exit(1);
}
}
pre-commit via:
composer require --dev laravel-zero/framework
vendor/bin/laravel new ConventionsCheckCommand
How can I help you explore Laravel packages today?