pccomponentes/coding-standard
PcComponentes Coding Standard adds PHP_CodeSniffer sniffs to enforce consistent PHP style. Install via Composer as a dev dependency and reference vendor/pccomponentes/coding-standard/src/ruleset.xml in your phpcs.xml(.dist) to apply the rules.
Install the Package
Add the package to your project’s composer.json under require-dev:
composer require --dev pccomponentes/coding-standard
Configure PHPCS
Create or update phpcs.xml.dist in your project root with:
<?xml version="1.0"?>
<ruleset name="Project Coding Standard">
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml"/>
</ruleset>
.dist suffix ensures it’s copied to phpcs.xml during deployment but ignored in Git.Run PHPCS Lint your codebase:
vendor/bin/phpcs --standard=PcComponentes app/
--report=full for detailed output or --report=summary for a quick overview.First Use Case: Laravel Controller Validation Enforce consistent method naming and docblock formatting in controllers:
vendor/bin/phpcs --standard=PcComponentes app/Http/Controllers/
RequireSingleLineMethod and docblock consistency.Pre-Commit Hook
Use a script (e.g., pre-commit.sh) to run PHPCS before commits:
#!/bin/bash
vendor/bin/phpcs --standard=PcComponentes --colors --report=emacs app/ || exit 1
CI/CD Pipeline (GitHub Actions Example) Add PHPCS to your workflow to block non-compliant code:
name: PHPCS
on: [push, pull_request]
jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- run: composer install
- run: vendor/bin/phpcs --standard=PcComponentes --warning-severity=3 app/
--warning-severity=3 to fail builds on warnings.IDE Integration Configure PHPStorm/VSCode to use PHPCS for real-time feedback:
Settings > Languages & Frameworks > PHP > Code Sniffer and set the standard to PcComponentes.phpcs.executablePath to vendor/bin/phpcs.Custom Ruleset for Laravel Extend the standard to enforce Laravel-specific conventions:
<ruleset>
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml"/>
<rule ref="vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PSR2/PSR2.xml">
<exclude name="PSR2.Methods.MethodDeclaration"/>
</rule>
<arg name="tab-width" value="4"/>
<arg name="encoding" value="utf-8"/>
</ruleset>
Service Container Sniffs Enforce consistent dependency injection patterns:
vendor/bin/phpcs --standard=PcComponentes app/Providers/
bind()/singleton() method naming and docblock consistency.Migration File Validation Validate migration file structure and naming:
vendor/bin/phpcs --standard=PcComponentes database/migrations/
Artisan Command Checks Audit command classes for proper naming and method organization:
vendor/bin/phpcs --standard=PcComponentes app/Console/Commands/
handle() method placement and docblock requirements.Facade and Helper Sniffs Detect misuse of facades or static helpers:
vendor/bin/phpcs --standard=PcComponentes --extensions=php app/ --ignore=tests/
Cache:: or Auth:: usage where dependency injection is preferred.Rule Conflicts
PcComponentes and PSR2 (e.g., method declaration spacing).phpcs.xml:
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml">
<exclude name="PcComponentes.Sniffs.Methods.RequireSingleLineMethod"/>
</rule>
Performance Overhead
--ignore=tests/ to skip test files.False Positives
do-while loops).phpcs.xml:
<file>app/OldLegacyCode/Controller.php</file>
<exclude name="PcComponentes.Sniffs.ControlStructures.DoWhileSniff"/>
IDE Misconfiguration
vendor/bin/phpcs.Verbose Output
Use --verbose to debug rule application:
vendor/bin/phpcs --standard=PcComponentes --verbose app/
Rule-Specific Debugging Isolate which rule triggers a violation:
vendor/bin/phpcs --standard=PcComponentes --report=checkstyle app/ | grep -A5 "error"
Dry Run with Custom Rules Test a subset of rules before full adoption:
vendor/bin/phpcs --standard=PcComponentes --ruleset=PcComponentes.NamingConventions app/
Custom Sniffs Add project-specific sniffs by extending the ruleset:
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml"/>
<rule ref="path/to/your/custom-sniff.xml"/>
Overriding Default Rules
Modify or disable rules in phpcs.xml:
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml">
<config name="single_line_comment_spacing" value="1"/>
</rule>
Dynamic Rulesets Use environment variables to switch rulesets:
RULESET=PcComponentes vendor/bin/phpcs app/
$RULESET.Blade Template Sniffs
composer require --dev alexpeattie/phpcs-blade
Update phpcs.xml:
<file>resources/views/*.blade.php</file>
<rule ref="vendor/alexpeattie/phpcs-blade/ruleset.xml"/>
Artisan Command Autoloading
app/Console/Kernel.php.vendor/bin/phpcs --standard=PcComponentes app/Console/Kernel.php
Service Provider Sniffs
register()/boot() methods may violate line-length rules.phpcs.xml:
<arg name="line_ending" value="LF"/>
<arg name="line_length" value="180"/>
composer require --dev friendsofphp/php-cs-fixer
vendor/bin/php-cs-fixer fix --rules=@PcComponentes --dry-run
How can I help you explore Laravel packages today?