consistence/coding-standard
PHP coding standard for Consistence projects: a ready-to-use PHP_CodeSniffer ruleset plus configuration to enforce consistent style, naming, and best practices across codebases. Easy to adopt in CI and local development to keep code clean and uniform.
Install via Composer:
composer require --dev consistence/coding-standard
Run PHPCS with the ruleset:
vendor/bin/phpcs --standard=Consistence src/
src/ with your target directory (e.g., app/ for Laravel).First Use Case:
phpcs to block style violations before commits.
Example (Git Hook):
# .git/hooks/pre-commit
#!/bin/sh
vendor/bin/phpcs --standard=Consistence --warning-severity=0 --error-severity=5 $@
phpunit.xml or a CI step (e.g., GitHub Actions):
<php>
<file>vendor/bin/phpcs</file>
<arg>--standard=Consistence</arg>
<arg>--warning-severity=0</arg>
<arg>--error-severity=5</arg>
<arg>src/</arg>
</php>
Quick Check:
vendor/bin/phpcs --standard=Consistence app/Http/Controllers/ExampleController.php
Daily Development:
phpcs before committing or pushing:
alias phpcs="vendor/bin/phpcs --standard=Consistence --warning-severity=0"
Consistence standard for real-time feedback.Team Onboarding:
CONTRIBUTING.md section:
## Code Style
Run `composer phpcs` to check your changes against the Consistence standard.
php-cs-fixer for auto-fixing common issues:
composer require --dev friendsofphp/php-cs-fixer
vendor/bin/php-cs-fixer fix --rules=@Consistence
CI/CD Integration:
- name: Run PHPCS
run: vendor/bin/phpcs --standard=Consistence --warning-severity=0 --error-severity=5 src/
test:phpcs:
script:
- vendor/bin/phpcs --standard=Consistence --warning-severity=0 --error-severity=5 src/
Laravel-Specific Patterns:
// app/Console/Commands/CheckStyle.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CheckStyle extends Command
{
protected $signature = 'code:check';
public function handle()
{
$exitCode = shell_exec('vendor/bin/phpcs --standard=Consistence --warning-severity=0 src/ 2>&1', $exitCode);
if ($exitCode !== 0) {
$this->error('Code style violations found!');
exit(1);
}
$this->info('Code style checks passed!');
}
}
Register in app/Console/Kernel.php:
protected $commands = [
Commands\CheckStyle::class,
];
Run with:
php artisan code:check
Custom Rulesets:
.phpcs.xml:
<?xml version="1.0"?>
<ruleset name="App">
<config name="installedPaths" value="vendor/consistence/coding-standard"/>
<rule ref="Consistence"/>
<!-- Override specific rules -->
<rule ref="Consistence.Exceptions.ExceptionDeclaration">
<properties>
<property name="exceptionDirectory" value="Exceptions"/>
</properties>
</rule>
</ruleset>
Run with:
vendor/bin/phpcs --standard=app/.phpcs.xml src/
Combine with Other Tools:
phpstan/extension-installer for static analysis:
composer require --dev phpstan/extension-installer
vendor/bin/phpstan analyse --level=5 src/
pint (Laravel’s PHP-CS-Fixer wrapper) for auto-fixing:
composer require laravel/pint --dev
vendor/bin/pint --test
Partial Adoption:
app/Http/ or app/Models/) before full project adoption.Documentation:
Add a STYLE_GUIDE.md with examples of compliant code:
## PHPDoc Example
```php
/**
* @return array<int, string>
*/
public function getItems(): array
{
return ['a', 'b'];
}
class ValidationException extends \Exception {}
$array = [
'key' => 'value',
];
Excluding Files:
.phpcsignore to exclude tests or generated files:
tests/
bootstrap/cache/
vendor/
False Positives:
Squiz.Arrays.ArrayDeclaration may flag valid multi-line arrays. Exclude with:
<rule ref="Consistence">
<exclude name="Squiz.Arrays.ArrayDeclaration"/>
</rule>
new Class). Disable with:
<rule ref="Consistence">
<exclude name="Consistence.Functions.ConstructorCall"/>
</rule>
PHP Version Mismatches:
Rule Conflicts:
vendor/bin/phpcs --standard=PSR12 src/ 2>&1 | grep -v "OK"
vendor/bin/phpcs --standard=Consistence src/ 2>&1 | grep -v "OK"
pint, ensure rules are compatible:
vendor/bin/php-cs-fixer fix --dry-run --rules=@Consistence
Performance:
--cache flag or tools like php-parallel-lint.vendor/bin/phpcs --standard=Consistence app/Http/ app/Models/ &
vendor/bin/phpcs --standard=Consistence app/Console/ app/Providers/ &
DocBlock Quirks:
@var for Constants: The rule disallows @var for constants. Update docblocks:
// Before (flagged)
/** @var string */
const VERSION = '1.0';
// After (compliant)
const VERSION = '1.0';
@inheritDoc: Avoid standalone @inheritDoc blocks. Merge with parent docblocks.Exception Rules:
ExceptionDeclaration rule requires exceptions to be in a specific directory (default: Exceptions/). Configure in .phpcs.xml:
<rule ref="Consistence.Exceptions.ExceptionDeclaration">
<properties>
<property name="exceptionDirectory" value="App/Exceptions"/>
</properties>
</rule>
Verbose Output:
--report=full for detailed error messages:
vendor/bin/phpcs --standard=Consistence --report=full src/
Rule-Specific Help:
vendor/bin/phpcs --standard=Consistence --list-rules
vendor/bin/phpcs --standard=Consistence --help Consistence.Functions.ConstructorCall
Ignore Specific Errors:
--ignore=ERROR_CODE to skip known issues:
vendor/bin/phpcs --standard=Consistence --ignore=Consistence.Arrays.ArrayDeclaration src/
php-cs-fixer to automate fixes for spacingHow can I help you explore Laravel packages today?