Skip to content

Commit bf123f1

Browse files
committed
UselessParenthesesSniff: Fixed false positive
1 parent d51f32c commit bf123f1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private function isPartOfArithmeticOperation(File $phpcsFile, int $parenthesisOp
132132
{
133133
$tokens = $phpcsFile->getTokens();
134134

135-
$operators = [T_PLUS, T_MINUS, T_MULTIPLY, T_DIVIDE, T_STRING_CONCAT];
135+
$operators = [T_PLUS, T_MINUS, T_MULTIPLY, T_DIVIDE, T_STRING_CONCAT, T_MODULUS];
136136

137137
$operatorsPointers = TokenHelper::findNextAll($phpcsFile, $operators, $parenthesisOpenerPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']);
138138
if (count($operatorsPointers) === 0) {
@@ -141,23 +141,29 @@ private function isPartOfArithmeticOperation(File $phpcsFile, int $parenthesisOp
141141

142142
$containsPlusOrMinus = false;
143143
$containsMultiplyOrDivide = false;
144+
$containsModulus = false;
144145
$containsStringConcat = false;
145146
foreach ($operatorsPointers as $operatorsPointer) {
146147
if (in_array($tokens[$operatorsPointer]['code'], [T_PLUS, T_MINUS], true)) {
147148
$containsPlusOrMinus = true;
148149
} elseif (in_array($tokens[$operatorsPointer]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
149150
$containsMultiplyOrDivide = true;
151+
} elseif ($tokens[$operatorsPointer]['code'] === T_MODULUS) {
152+
$containsModulus = true;
150153
} else {
151154
$containsStringConcat = true;
152155
}
153156
}
154157

155158
$pointerAfterParenthesis = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
156159
if (in_array($tokens[$pointerAfterParenthesis]['code'], $operators, true)) {
157-
if ($containsPlusOrMinus && in_array($tokens[$pointerAfterParenthesis]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
160+
if ($containsPlusOrMinus && in_array($tokens[$pointerAfterParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_MODULUS], true)) {
158161
return true;
159162
}
160-
if ($containsMultiplyOrDivide && in_array($tokens[$pointerAfterParenthesis]['code'], [T_PLUS, T_MINUS], true)) {
163+
if ($containsMultiplyOrDivide && in_array($tokens[$pointerAfterParenthesis]['code'], [T_PLUS, T_MINUS, T_MODULUS], true)) {
164+
return true;
165+
}
166+
if ($containsModulus && in_array($tokens[$pointerAfterParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_PLUS, T_MINUS], true)) {
161167
return true;
162168
}
163169
if ($containsStringConcat || $tokens[$pointerAfterParenthesis]['code'] === T_STRING_CONCAT) {
@@ -167,10 +173,13 @@ private function isPartOfArithmeticOperation(File $phpcsFile, int $parenthesisOp
167173

168174
$pointerBeforeParenthesis = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
169175
if (in_array($tokens[$pointerBeforeParenthesis]['code'], $operators, true)) {
170-
if ($containsPlusOrMinus && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_MULTIPLY, T_DIVIDE], true)) {
176+
if ($containsPlusOrMinus && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_MODULUS], true)) {
177+
return true;
178+
}
179+
if ($containsMultiplyOrDivide && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_PLUS, T_MINUS, T_MODULUS], true)) {
171180
return true;
172181
}
173-
if ($containsMultiplyOrDivide && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_PLUS, T_MINUS], true)) {
182+
if ($containsModulus && in_array($tokens[$pointerBeforeParenthesis]['code'], [T_MULTIPLY, T_DIVIDE, T_PLUS, T_MINUS], true)) {
174183
return true;
175184
}
176185
if ($containsStringConcat || $tokens[$pointerBeforeParenthesis]['code'] === T_STRING_CONCAT) {

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ function () {
112112

113113
$c = (clone $a->foo())->bar($b);
114114

115+
$d = (10 - ($x % 10)) % 10;
116+
$e = (($x % 10) - 10) % 10;
117+
115118
// Must be last
116119
return true
117120
? 100

0 commit comments

Comments
 (0)