iodigital-com/php-code-sniffer-standard
Extends PHP_CodeSniffer with iO Digital coding standards. Install via Composer, add an IO ruleset to your phpcs.xml, configure excluded paths and PHP version ranges, and run vendor/bin/phpcs. Includes guidance for properly ignoring sniff violations.
Installation
composer require --dev iodigital-com/php-code-sniffer-standard:^29.5
Add the standard to your phpcs.xml or phpcs.xml.dist:
<config name="standard" value="IODigitalCom\PHPCodeSnifferStandard"/>
First Run
vendor/bin/phpcs --standard=IODigitalCom\PHPCodeSnifferStandard app/
Key Files to Review
ruleset.xml (if provided) – Defines custom rules or overrides.tests/ (if included) – Example sniffs or edge cases.README.md – May outline specific conventions (e.g., naming, spacing).Enforce Laravel-Specific Coding Standards with PHP 8.5 Support Run PHPCS on a Laravel project to catch:
@return/@throws in controllers (now validated against PHP 8.5 type system).Route:: vs. route() helper.getUser() vs. fetchUser()).array_unpack syntax) are now properly linted.Pre-Commit Hook
Integrate with phpcs in a Git hook to block non-compliant code:
composer require --dev dealerdirect/phpcodesniffer-composer-installer
Add to composer.json:
"scripts": {
"test": "phpcs --standard=IODigitalCom\PHPCodeSnifferStandard --report=full"
},
"require-dev": {
"php": "^8.2" // Enforce PHP 8.2+ compatibility
}
CI/CD Pipeline Use in GitHub Actions/GitLab CI with PHP 8.5:
- name: Run PHPCS (PHP 8.5)
run: vendor/bin/phpcs --standard=IODigitalCom\PHPCodeSnifferStandard --warning-severity=0
env:
PHP_VERSION: "8.5"
Custom Rules for PHP 8.5
Extend the standard to handle new PHP 8.5 features (e.g., array_unpack):
// app/CodeSniffer/CustomSniffs/PHP85ArrayUnpackSniff.php
class PHP85ArrayUnpackSniff implements Sniff {
public function register() {
return [
T_ARRAY_UNPACK => [$this, 'processArrayUnpack']
];
}
}
barryvdh/laravel-ide-helper to ensure PHPDoc compliance with PHP 8.5 types.--severity flags to focus on critical issues:
phpcs --standard=IODigitalCom\PHPCodeSnifferStandard --severity=5
phpcs app/Http/Controllers/ --standard=IODigitalCom\PHPCodeSnifferStandard
<rule ref="IODigitalCom.PHP85.Arrays.DisallowLegacyArrayUnpack"/>
PHP Version Mismatch
composer.json:
"require": {
"php": "^8.2"
}
False Positives with PHP 8.5
array_unpack) may trigger false positives.phpcs.xml:
<exclude name="IODigitalCom.PHP85.Arrays.DisallowLegacyArrayUnpack" />
Performance with Large Codebases
phpcs --cache=./phpcs.cache --standard=IODigitalCom\PHPCodeSnifferStandard --parallel=4
Composer Autoloader Issues
composer.lock is up-to-date and run:
composer dump-autoload
--verbose to diagnose rule application:
phpcs --standard=IODigitalCom\PHPCodeSnifferStandard --verbose
phpcs --standard=IODigitalCom\PHPCodeSnifferStandard --sniffs=IODigitalCom.PHP85.Arrays
php -v # Ensure PHP 8.2+ is used
Custom Standards for PHP 8.5
Merge with other standards (e.g., PSR12) in phpcs.xml:
<config name="standard" value="IODigitalCom\PHPCodeSnifferStandard,PSR12"/>
Dynamic Rules for New PHP Features
Use Sniff classes to enforce Laravel-specific logic (e.g., validate HasFactory trait usage with PHP 8.5 attributes):
public function process(SimpleToken $token) {
if ($token->getContent() === '#[HasFactory]') {
$this->recordWarning('Use "use Illuminate\Database\Eloquent\HasFactory;"', $token->getPosition());
}
}
Configuration Overrides for PHP 8.5 Override default values per file/directory:
<arg name="extensions" value="php,blade" />
<arg name="tab-width" value="4" />
<arg name="php-version" value="8.5" /> <!-- Explicitly set PHP version -->
GitHub Actions Best Practices
Update your workflow to use the latest composer-install action:
- uses: ramsey/composer-install@v2
with:
dependency-versions: locked
How can I help you explore Laravel packages today?