yoast/yoastcs
Yoast Coding Standards (YoastCS) provides Composer-installable rulesets for PHP_CodeSniffer plus PHP Parallel Lint, bundling Yoast sniffs and selected external standards (including WordPress). Use it to enforce consistent code style and quality in Yoast projects.
Installation:
composer require --dev yoast/yoastcs:"^3.0"
Ensure allow-plugins is configured in composer.json:
{
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
First Run (PHP Parallel Lint):
Add to composer.json:
"scripts": {
"lint": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --show-deprecated --exclude vendor --exclude .git"
]
}
Run:
composer lint
First Run (PHP_CodeSniffer): Run basic check:
vendor/bin/phpcs --standard=Yoast src/
View all sniffs:
vendor/bin/phpcs -e --standard=Yoast
Pre-Commit Hooks:
Integrate with pre-commit (e.g., Git Hooks or Husky) to run:
composer lint && vendor/bin/phpcs --standard=Yoast --report=full src/
CI/CD Integration:
Use composer check-cs-warnings (if using Yoast repo template) or:
# Example GitHub Actions
- name: Run PHPCS
run: vendor/bin/phpcs --standard=Yoast --warning-severity=1 src/ || exit 0
Threshold-Based Checks: Set thresholds in CI:
YOASTCS_THRESHOLD_ERRORS=5 YOASTCS_THRESHOLD_WARNINGS=20 vendor/bin/phpcs --standard=Yoast --report=YoastCS\Yoast\Reports\Threshold src/
Check result in script:
if (defined('YOASTCS_ABOVE_THRESHOLD') && YOASTCS_ABOVE_THRESHOLD) {
exit(1); // Fail build
}
PhpStorm Integration:
YoastWarning for warnings, Error for errors.Custom Sniff Exclusions:
Extend .phpcs.xml.dist:
<rule ref="Yoast">
<exclude name="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall"/>
</rule>
Parallel Lint for Large Codebases: Optimize for speed:
composer lint -- --jobs=8 # 8 parallel processes
Dynamic Thresholds: Use environment variables in CI:
YOASTCS_THRESHOLD_ERRORS=$(git diff --name-only | wc -l) vendor/bin/phpcs --report=YoastCS\Yoast\Reports\Threshold src/
Custom Reports: Generate a custom report for PRs:
vendor/bin/phpcs --standard=Yoast --report=json src/ > phpcs-report.json
PHP Version Mismatches:
minimum-stability in composer.json is set to dev or alpha if using PHPCompatibilityWP ^3.0.0@alpha.WordPress Version Sniffs:
minimum_wp_version defaults to 6.8 (v3.3.0). Update in .phpcs.xml.dist if targeting older WP:
<config name="minimum_wp_version" value="6.3"/>
Slevomat Sniffs:
RequireTrailingCommaInCall enforces trailing commas in multi-line calls, while DisallowTrailingCommaInCall applies to single-line calls.RequireNullCoalesceOperator (??) and RequireNullCoalesceEqualOperator (??=) are mandatory in v3.3.0+.Threshold Report Quirks:
vendor/bin/phpcs --report="YoastCS\\Yoast\\Reports\\Threshold"
YOASTCS_THRESHOLD_EXACT_MATCH to detect if thresholds were exactly met (not just under).Performance:
vendor/ and .git/ explicitly to avoid slow scans:
vendor/bin/phpcs --standard=Yoast --exclude=vendor,node_modules src/
Isolate Sniffs: Run a single sniff for debugging:
vendor/bin/phpcs --standard=Yoast --sniffs=SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall src/
Verbose Output: Enable debug mode:
vendor/bin/phpcs -n --standard=Yoast src/
Parallel Lint Errors:
--show-deprecated flags deprecated calls (e.g., mysql_*).Parse error in output.PhpStorm False Positives:
VariableAnalysis sniffs.Custom Sniffs:
Add to Yoast ruleset in .phpcs.xml.dist:
<rule ref="Yoast">
<file>CustomSniff.php</file>
</rule>
Override Defaults: Extend the ruleset dynamically:
// In a Composer script (e.g., post-autoload-dump)
$ruleset = new \PHP_CodeSniffer\Ruleset();
$ruleset->importRuleset('Yoast');
$ruleset->addExcludedFile('path/to/excluded-file.php');
Custom Reports:
Extend the Threshold report or create a new one by subclassing PHP_CodeSniffer\Reports\Report.
PHPCompatibilityWP: Adjust PHP version checks:
<config name="php_version" value="8.1"/>
How can I help you explore Laravel packages today?