Skip to content

Commit 001066e

Browse files
committed
UselessParenthesesSniff: Fixed false positives
1 parent 0d1329f commit 001066e

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,50 @@ private function containsOperators(File $phpcsFile, int $parenthesisOpenerPointe
124124
{
125125
$tokens = $phpcsFile->getTokens();
126126

127-
if (TokenHelper::findNext($phpcsFile, [T_PLUS, T_MINUS, T_STRING_CONCAT], $parenthesisOpenerPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']) === null) {
127+
$operators = [T_PLUS, T_MINUS, T_MULTIPLY, T_DIVIDE, T_STRING_CONCAT];
128+
129+
$operatorsPointers = TokenHelper::findNextAll($phpcsFile, $operators, $parenthesisOpenerPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']);
130+
if (count($operatorsPointers) === 0) {
128131
return false;
129132
}
130133

134+
$containsPlusOrMinus = false;
135+
$containsMultiplyOrDivide = false;
136+
$containsStringConcat = false;
137+
foreach ($operatorsPointers as $operatorsPointer) {
138+
if (in_array($tokens[$operatorsPointer]['code'], [T_PLUS, T_MINUS], true)) {
139+
$containsPlusOrMinus = true;
140+
} elseif (in_array($tokens[$operatorsPointer]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
141+
$containsMultiplyOrDivide = true;
142+
} else {
143+
$containsStringConcat = true;
144+
}
145+
}
146+
131147
$pointerAfterParenthesis = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
132-
if (in_array($tokens[$pointerAfterParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_STRING_CONCAT], true)) {
133-
return true;
148+
if (in_array($tokens[$pointerAfterParenthesis]['code'], $operators, true)) {
149+
if ($containsPlusOrMinus && in_array($tokens[$pointerAfterParenthesis]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
150+
return true;
151+
}
152+
if ($containsMultiplyOrDivide && in_array($tokens[$pointerAfterParenthesis]['code'], [T_PLUS, T_MINUS], true)) {
153+
return true;
154+
}
155+
if ($containsStringConcat || $tokens[$pointerAfterParenthesis]['code'] === T_STRING_CONCAT) {
156+
return true;
157+
}
134158
}
135159

136160
$pointerBeforeParenthesis = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
137-
if (in_array($tokens[$pointerBeforeParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_STRING_CONCAT], true)) {
138-
return true;
161+
if (in_array($tokens[$pointerBeforeParenthesis]['code'], $operators, true)) {
162+
if ($containsPlusOrMinus && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
163+
return true;
164+
}
165+
if ($containsMultiplyOrDivide && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_PLUS, T_MINUS], true)) {
166+
return true;
167+
}
168+
if ($containsStringConcat || $tokens[$pointerBeforeParenthesis]['code'] === T_STRING_CONCAT) {
169+
return true;
170+
}
139171
}
140172

141173
return false;

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ function () {
106106
&& (null === $b || null === $c)) {
107107
}
108108

109+
$a = ($b * $c) - $d;
110+
$a = ($b * 60 * 60) + ($c * 60) + $d;
111+
109112
// Must be last
110113
return true
111114
? 100

0 commit comments

Comments
 (0)