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 your composer.json:
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
First Use Case: Run a basic PHPCS check on your project:
vendor/bin/phpcs --standard=Yoast src/
--standard=Yoast applies Yoast’s custom ruleset (WordPress + PHPCompatibilityWP + Slevomat + custom sniffs).vendor/ and .git by default (configured in .phpcs.xml.dist).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 with:
composer lint
Pre-Commit Hooks:
Integrate PHPCS into Git hooks (e.g., pre-commit) to block violations:
vendor/bin/phpcs --standard=Yoast --report=full src/ || exit 1
--report=full for detailed output.parallel-lint for faster feedback.CI/CD Integration:
- name: Run PHPCS
run: vendor/bin/phpcs --standard=Yoast --report=threshold src/
export YOASTCS_THRESHOLD_ERRORS=0
export YOASTCS_THRESHOLD_WARNINGS=5
Run with:
vendor/bin/phpcs --standard=Yoast --report=YoastCS\Yoast\Reports\Threshold src/
Exit with error if YOASTCS_ABOVE_THRESHOLD is true.PhpStorm Integration:
Settings > Languages & Frameworks > PHP > Code Sniffer.Yoast.Custom Rulesets:
Extend .phpcs.xml.dist to override defaults:
<rule ref="Yoast">
<exclude name="WordPress.WP.CapitalPDangler"/>
<arg name="minimum_wp_version" value="6.4"/>
</rule>
<exclude-pattern> in .phpcs.xml.dist to skip tests or legacy code:
<exclude-pattern>*/tests/*</exclude-pattern>
parallel-lint src/ app/ --exclude vendor --jobs 4
YOASTCS_THRESHOLD_EXACT_MATCH in scripts to auto-update thresholds:
if (defined('YOASTCS_THRESHOLD_EXACT_MATCH') && !YOASTCS_THRESHOLD_EXACT_MATCH) {
echo "Update thresholds in CI config.";
}
PHP Version Mismatches:
<arg name="minimum_php_version" value="7.2"/>
<arg name="minimum_wp_version" value="6.4"/>
minimum-stability isn’t set to dev (for alpha packages).SlevomatCodingStandard Errors:
Property "checkIfConditions" does not exist. Update or pin:
"yoast/yoastcs": "^3.4.1"
False Positives:
get_post_meta() calls. Exclude or suppress:
<exclude name="WordPress.WP.GetMetaSingle"/>
.phpcs.xml.dist:
<rule ref="VariableAnalysis">
<exclude name="VariableAnalysis.UnusedVariable.UnusedVariable"/>
Threshold Report Quirks:
--report=YoastCS\Yoast\Reports\Threshold may need escaping on Windows:
vendor/bin/phpcs --report="YoastCS\\Yoast\\Reports\\Threshold"
vendor/bin/phpcs -e --standard=Yoast
vendor/bin/phpcs --standard=Yoast --generator=Text | grep "WordPress.WP"
--dry-run to test PHPCS without fixing:
vendor/bin/phpcs --standard=Yoast --dry-run src/
Custom Sniffs:
Add to .phpcs.xml.dist:
<rule ref="Yoast">
<file>app/Path/To/CustomSniff.php</file>
</rule>
Ensure the sniff follows PHPCS standards.
Override Rules: Disable or modify sniffs globally:
<rule ref="Yoast">
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator"/>
<arg name="line_ending" value="LF"/>
</rule>
Parallel Lint Customization:
Extend the composer.json script for project-specific paths:
"lint": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint src/ app/ --exclude tests/ --jobs 8"
]
--fix for auto-correctable issues (e.g., whitespace, trailing commas):
vendor/bin/phpcs --standard=Yoast --fix src/
vendor/bin/phpcs --standard=Yoast app/Models/
phpcs --cache (requires phpcs-cache extension).<!-- .phpstorm.meta.php -->
<option name="phpCodeSnifferSettings">
<option name="standard" value="Yoast"/>
<option name="onSave" value="true"/>
</option>
How can I help you explore Laravel packages today?