shlinkio/php-coding-standard
PHP_CodeSniffer ruleset used by Shlink projects. Extends PSR-12 with strict comparisons, no long arrays, spaced concatenation, no unused imports, ordered uses, required type hints/return types, trailing commas in multiline, and other modern PHP formatting rules.
Installation Add the package to your Laravel project via Composer:
composer require --dev shlinkio/php-coding-standard
Configure PHPCS
Create or update phpcs.xml in your project root with:
<?xml version="1.0"?>
<ruleset name="Shlink Laravel Standard">
<rule ref="Shlinkio" />
<arg name="colors" value="true"/>
<arg name="report" value="summary"/>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>storage/</exclude-pattern>
<exclude-pattern>bootstrap/cache/</exclude-pattern>
</ruleset>
First Run Execute PHPCS on your Laravel app directory:
vendor/bin/phpcs app/
Focus on fixing errors in app/Http/Controllers/ or app/Models/ first.
Local Development Run PHPCS before committing:
vendor/bin/phpcs --standard=Shlinkio app/
Use --diff to check changes:
vendor/bin/phpcs --diff --standard=Shlinkio app/
CI/CD Integration
Add to your GitHub Actions workflow (.github/workflows/phpcs.yml):
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 --dev
- run: vendor/bin/phpcs --standard=Shlinkio --warning-severity=3
IDE Integration Configure PHPStorm to use this standard:
Settings > Editor > Code Style > PHP... > Import Scheme and select phpcs.xmlExcluding Blade Files
Add Blade exclusions to phpcs.xml:
<exclude-pattern>resources/views/*.blade.php</exclude-pattern>
Handling Laravel Generators Ignore auto-generated files (e.g., migrations, factories):
<exclude-pattern>database/migrations/*.php</exclude-pattern>
<exclude-pattern>database/factories/*.php</exclude-pattern>
Custom Rules Overrides
Override specific Shlink rules in phpcs.xml:
<rule ref="Shlinkio.ArrayNotation">
<severity>ignore</severity>
</rule>
False Positives in Blade Templates
array() syntax in Blade files.{{ @php }} wrappers for complex logic.Strict Comparisons in Laravel Core
== in some core files (e.g., Illuminate\Support\Arr).vendor/ and focus on app/ directory.Trailing Commas in JSON Configs
config/app.php.<rule ref="Shlinkio.TrailingComma">
<exclude-pattern>config/*.php</exclude-pattern>
</rule>
Detailed Reports
Use --report=full for verbose output:
vendor/bin/phpcs --standard=Shlinkio --report=full app/
Rule-Specific Checks Test individual rules:
vendor/bin/phpcs --standard=Shlinkio --rules=StrictComparisons app/
Ignoring Specific Files Temporarily ignore files during development:
vendor/bin/phpcs --standard=Shlinkio --ignore=app/OldController.php
Parallel Execution
Speed up CI runs with --parallel:
vendor/bin/phpcs --standard=Shlinkio --parallel=4
Cache Results
Use --cache to avoid re-scanning unchanged files:
vendor/bin/phpcs --standard=Shlinkio --cache=/tmp/phpcs.cache
Custom Rules
Add project-specific rules after <rule ref="Shlinkio" />:
<rule ref="Shlinkio">
<exclude name="StrictComparisons"/>
</rule>
<rule ref="CustomRuleSet">
<file>app/Rules/</file>
</rule>
Integration with PHP-CS-Fixer Combine with PHP-CS-Fixer for auto-fixing:
composer require --dev friendsofphp/php-cs-fixer
./vendor/bin/php-cs-fixer fix --rules=@Shlinkio
Visual Studio Code
Add to .vscode/settings.json:
{
"php.validate.executablePath": "vendor/bin/phpcs",
"php.validate.run": "onType",
"php.validate.standard": "Shlinkio"
}
How can I help you explore Laravel packages today?