Skip to content

Commit 6e58e31

Browse files
pepakrizkukulich
authored andcommitted
UselessParenthesesSniff: Fixed false positive
1 parent bb8be43 commit 6e58e31

File tree

5 files changed

+10
-2
lines changed

5 files changed

+10
-2
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ private function checkParenthesesAroundOperators(File $phpcsFile, int $parenthes
461461
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_DIVIDE && $tokens[$firstOperatorPointer]['code'] === T_DIVIDE) {
462462
return;
463463
}
464+
465+
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_DIVIDE && $tokens[$firstOperatorPointer]['code'] === T_MULTIPLY) {
466+
return;
467+
}
464468
}
465469

466470
$lastOperatorPointer = $operatorsPointers[count($operatorsPointers) - 1];

tests/Sniffs/PHP/UselessParenthesesSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/uselessParenthesesErrors.php');
1919

20-
self::assertSame(40, $report->getErrorCount());
20+
self::assertSame(41, $report->getErrorCount());
2121

22-
foreach ([8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38, 41, 42, 43, 44, 49, 53, 55, 56, 57, 58, 63] as $line) {
22+
foreach ([8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38, 41, 42, 43, 44, 49, 53, 55, 56, 57, 58, 64] as $line) {
2323
self::assertSniffError($report, $line, UselessParenthesesSniff::CODE_USELESS_PARENTHESES);
2424
}
2525

tests/Sniffs/PHP/data/uselessParenthesesErrors.fixed.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function () {
5757
$x = 100 / 50 * 100;
5858
$x = $a + $b * 3;
5959
$x = $b + 100 - $c;
60+
$x = $b * 100 / $c;
6061

6162
function () {
6263
return [

tests/Sniffs/PHP/data/uselessParenthesesErrors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function () {
5757
$x = ((100 / 50) * 100);
5858
$x = ($a + $b * 3);
5959
$x = $b + (100 - $c);
60+
$x = $b * (100 / $c);
6061

6162
function () {
6263
return [

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function () {
129129

130130
$a = 10 / (5 / 2);
131131

132+
$a = 10 / (5 * 2);
133+
132134
pow(10, -($x + 1));
133135

134136
if (! ($i === $j - 1)) {

0 commit comments

Comments
 (0)