Skip to content

Commit

Permalink
Extra: do not allow ambiguous conditions
Browse files Browse the repository at this point in the history
This new PHPCS 3.9.0 sniff will flag code which contains a `||` and a `&&` operator in the same condition and their precedence is not clarified via parenthesis.

This should allow for users of WPCS to find/prevent some bugs in code.

I'd personally would **_highly_** recommend for WP Core to also use this sniff, but I suspect that will need due discussion/Make post/handbook change.

Note: running this sniff on WP Core as-is, would (to my surprise) not actually trigger any new errors.

Includes updating a code snippet in the Ruleset test to comply.
  • Loading branch information
jrfnl committed Mar 4, 2024
1 parent 9d29ddc commit 92751a1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Tests/RulesetCheck/class-ruleset-test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Ruleset_Test {
if ( preg_match( '`' . preg_quote( $param_a, '`' ) . '`i', $param_b ) === 1 ) {
echo esc_html( "doublequoted $string" );
} elseif ( $this->a ) {
$ab = $a % $b + $c / $d && $param_a || $param_b >> $bitshift;
$ab = $a % $b + $c / $d && ( $param_a || $param_b >> $bitshift );
}
};

Expand Down
4 changes: 4 additions & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
https://github.com/WordPress/WordPress-Coding-Standards/pull/809 -->
<rule ref="Squiz.PHP.DisallowSizeFunctionsInLoops"/>

<!-- Do not allow ambiguous conditions.
https://github.com/WordPress/WordPress-Coding-Standards/issues/2429 -->
<rule ref="Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence"/>

<!-- Check that functions use all parameters passed.
https://github.com/WordPress/WordPress-Coding-Standards/issues/1510 -->
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter">
Expand Down

0 comments on commit 92751a1

Please sign in to comment.