Skip to content

Commit

Permalink
Merge pull request #361 from Yoast/JRF/yoastcs-variable-analysis
Browse files Browse the repository at this point in the history
YoastCS rules: flag unused/undefined variables
  • Loading branch information
jrfnl authored Dec 14, 2023
2 parents 340b3a2 + 106b5e3 commit 12e1d05
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/basics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
phpcsstandards/phpcsextra:"dev-develop"
wp-coding-standards/wpcs:"dev-develop"
slevomat/coding-standard:"dev-master"
sirbrillig/phpcs-variable-analysis:"2.x"
# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Severity levels:
The `Yoast` standard for PHP_CodeSniffer is comprised of the following:
* The `WordPress` ruleset from the [WordPress Coding Standards](https://github.com/WordPress/WordPress-Coding-Standards) implementing the official [WordPress PHP Coding Standards](https://make.wordpress.org/core/handbook/coding-standards/php/), with some [select exclusions](https://github.com/Yoast/yoastcs/blob/develop/Yoast/ruleset.xml#L29-L75).
* The [`PHPCompatibilityWP`](https://github.com/PHPCompatibility/PHPCompatibilityWP) ruleset which checks code for PHP cross-version compatibility while preventing false positives for functionality polyfilled within WordPress.
* The [`VariableAnalysis`](https://github.com/sirbrillig/phpcs-variable-analysis/) ruleset.
* Select additional sniffs taken from [`PHP_CodeSniffer`](https://github.com/PHPCSStandards/PHP_CodeSniffer).
* Select additional sniffs taken from [`PHPCSExtra`](https://github.com/PHPCSStandards/PHPCSExtra).
* Select additional sniffs taken from [`SlevomatCodingStandard`](https://github.com/slevomat/coding-standard).
Expand Down
2 changes: 2 additions & 0 deletions Yoast/Reports/Threshold.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* available which can be used in calling scripts.
*
* @since 2.2.0
*
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Flags unused params which are required via the interface. Invalid.
*/
final class Threshold implements Report {

Expand Down
19 changes: 19 additions & 0 deletions Yoast/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@
</rule>


<!--
#############################################################################
SNIFF FOR USE OF UNDEFINED/UNUSED VARIABLES
#############################################################################
-->
<rule ref="VariableAnalysis">
<properties>
<property name="allowUnusedCaughtExceptions" value="true"/>
<property name="allowUnusedParametersBeforeUsed" value="true"/>
<property name="allowUnusedForeachVariables" value="true"/>
<property name="allowWordPressPassByRefFunctions" value="true"/>

<!-- These two properties (attempt to) prevent false positives for interaction with views. -->
<property name="allowUnusedVariablesBeforeRequire" value="true"/>
<property name="allowUndefinedVariablesInFileScope" value="true"/>
</properties>
</rule>


<!--
#############################################################################
ADD SOME SPECIFIC EXTRA SNIFFS
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"phpcompatibility/phpcompatibility-wp": "^2.1.4",
"phpcsstandards/phpcsextra": "^1.2.1",
"phpcsstandards/phpcsutils": "^1.0.9",
"sirbrillig/phpcs-variable-analysis": "^2.11.17",
"slevomat/coding-standard": "^8.14.0",
"squizlabs/php_codesniffer": "^3.8.0",
"wp-coding-standards/wpcs": "^3.0.1"
Expand Down

0 comments on commit 12e1d05

Please sign in to comment.