Skip to content

Commit 0f5680a

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/RequireExplicitBooleanOperatorPrecedence: remove
unreachable condition This commit reverts the changes to the `Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence` sniff introduced in PHPCSStandards/PHPCSExtra@7b38efb. The sniff's tests remain relevant, so they were preserved. The original commit was added to fix false positives that the sniff was triggering when handling boolean operators inside a match (see PHPCSStandards/PHPCSExtra#271 (review) -1634348864 and PHPCSStandards/PHPCSExtra#271 (comment) ). Example: ```php match (true) { $a || ($b && $c) => true, }; ``` I believe the false positive was actually caused by a bug in `File::findStartOfStatement()`. This bug was then fixed in b82438f which rendered the changes to the sniff itself unnecessary and the removed condition unreachable. Before this fix, when processing the code example above, `File::findStartOfStatement()` returned the variable `$a` as the start of the statement for the `&&` boolean operator. This meant that `$previous` would be set to `||` and the removed condition would be needed to ensure the sniff would bail instead of triggering an error. After this fix, `File::findStartOfStatement()` returns `$b` as the start of the statement and then `$previous` is set to `false` and the sniff bails before reaching the removed condition. Including `Tokens::$blockOpeners` in `RequireExplicitBooleanOperatorPrecedenceSniff::$searchTargets` was necessary only for the removed condition, so it was removed as well.
1 parent 7185370 commit 0f5680a

File tree

1 file changed

+1
-8
lines changed

1 file changed

+1
-8
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/RequireExplicitBooleanOperatorPrecedenceSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class RequireExplicitBooleanOperatorPrecedenceSniff implements Sniff
5353
*/
5454
public function register()
5555
{
56-
$this->searchTargets = Tokens::$booleanOperators;
57-
$this->searchTargets += Tokens::$blockOpeners;
56+
$this->searchTargets = Tokens::$booleanOperators;
5857
$this->searchTargets[T_INLINE_THEN] = T_INLINE_THEN;
5958
$this->searchTargets[T_INLINE_ELSE] = T_INLINE_ELSE;
6059

@@ -102,12 +101,6 @@ public function process(File $phpcsFile, $stackPtr)
102101
return;
103102
}
104103

105-
if (isset(Tokens::$blockOpeners[$tokens[$previous]['code']]) === true) {
106-
// Beginning of the expression found for a block opener. Needed to
107-
// correctly handle match arms.
108-
return;
109-
}
110-
111104
// We found a mismatching operator, thus we must report the error.
112105
$error = 'Mixing different binary boolean operators within an expression';
113106
$error .= ' without using parentheses to clarify precedence is not allowed.';

0 commit comments

Comments
 (0)