Skip to content

Commit 4fedfc8

Browse files
committed
Fix handling of square brackets
1 parent 3826567 commit 4fedfc8

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

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

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,50 +51,52 @@ public function process(File $phpcsFile, $stackPtr)
5151
$token = $tokens[$stackPtr];
5252

5353
$start = $phpcsFile->findStartOfStatement($stackPtr);
54-
$end = $phpcsFile->findEndOfStatement($stackPtr);
55-
56-
$nearestParens = null;
57-
if (isset($token['nested_parenthesis']) === true) {
58-
$parens = $token['nested_parenthesis'];
59-
end($parens);
60-
$nearestParens = key($parens);
61-
62-
$start = $nearestParens;
63-
$end = $parens[$start];
64-
}
65-
66-
$nearestBracket = $phpcsFile->findPrevious(T_OPEN_SQUARE_BRACKET, $stackPtr);
6754

6855
if ($token['code'] === T_BOOLEAN_AND) {
69-
$search = [T_BOOLEAN_OR];
56+
$search = T_BOOLEAN_OR;
7057
} else if ($token['code'] === T_BOOLEAN_OR) {
71-
$search = [T_BOOLEAN_AND];
58+
$search = T_BOOLEAN_AND;
7259
} else {
7360
throw new \LogicException('Unreachable');
7461
}
7562

76-
do {
77-
$found = $phpcsFile->findNext($search, $start, $end);
78-
if ($found === false) {
63+
while (true) {
64+
$previous = $phpcsFile->findPrevious(
65+
[
66+
$search,
67+
T_OPEN_PARENTHESIS,
68+
T_OPEN_SQUARE_BRACKET,
69+
T_CLOSE_PARENTHESIS,
70+
T_CLOSE_SQUARE_BRACKET,
71+
],
72+
$stackPtr,
73+
$start
74+
);
75+
76+
if (!$previous) {
7977
break;
8078
}
8179

82-
$foundParens = null;
83-
if (isset($tokens[$found]['nested_parenthesis']) === true) {
84-
$parens = $tokens[$found]['nested_parenthesis'];
85-
end($parens);
86-
$foundParens = key($parens);
87-
}
88-
89-
$foundBracket = $phpcsFile->findPrevious(T_OPEN_SQUARE_BRACKET, $found);
90-
91-
if ($nearestParens === $foundParens && $nearestBracket === $foundBracket) {
80+
if ($tokens[$previous]['code'] === T_OPEN_PARENTHESIS
81+
|| $tokens[$previous]['code'] === T_OPEN_SQUARE_BRACKET
82+
) {
83+
// We halt if we reach the opening parens / bracket of the boolean operator.
84+
return;
85+
} else if ($tokens[$previous]['code'] === T_CLOSE_PARENTHESIS) {
86+
// We skip the content of nested parens.
87+
$stackPtr = ($tokens[$previous]['parenthesis_opener'] - 1);
88+
} else if ($tokens[$previous]['code'] === T_CLOSE_SQUARE_BRACKET) {
89+
// We skip the content of nested brackets.
90+
$stackPtr = ($tokens[$previous]['bracket_opener'] - 1);
91+
} else if ($tokens[$previous]['code'] === $search) {
92+
// We reached a mismatching operator, thus we must report the error.
9293
$error = "Mixed '&&' and '||' within an expression without using parentheses.";
9394
$phpcsFile->addError($error, $stackPtr, 'MissingParentheses', []);
95+
return;
96+
} else {
97+
throw new \LogicException('Unreachable');
9498
}
95-
96-
$start = ($found + 1);
97-
} while ($start !== false);
99+
}//end while
98100

99101
}//end process()
100102

src/Standards/Generic/Tests/CodeAnalysis/MixedBooleanOperatorUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ if (true && foo(true || true));
2929
if (true && foo(true && true || true));
3030
if (true && $foo[true || true]);
3131
if (true && $foo[true && true || true]);
32+
33+
if (true && foo(true) || true);
34+
if (true && $foo[true] || true);

src/Standards/Generic/Tests/CodeAnalysis/MixedBooleanOperatorUnitTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ class MixedBooleanOperatorUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
3 => 2,
30-
7 => 2,
31-
12 => 2,
32-
16 => 1,
29+
3 => 1,
30+
7 => 1,
31+
12 => 1,
3332
17 => 1,
34-
29 => 2,
35-
31 => 2,
33+
29 => 1,
34+
31 => 1,
35+
33 => 1,
36+
34 => 1,
3637
];
3738

3839
}//end getErrorList()

0 commit comments

Comments
 (0)