Skip to content

Commit a343cd7

Browse files
committed
SlevomatCodingStandard.PHP.UselessParentheses: Fixed false positive
1 parent 2ed4132 commit a343cd7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
use const T_REQUIRE_ONCE;
6161
use const T_SELF;
6262
use const T_SEMICOLON;
63+
use const T_SL;
6364
use const T_SR;
6465
use const T_STATIC;
6566
use const T_STRING_CAST;
@@ -472,14 +473,17 @@ private function checkParenthesesAroundOperators(File $phpcsFile, int $parenthes
472473
return;
473474
}
474475

476+
$complicatedOperators = [T_INLINE_THEN, T_COALESCE, T_BITWISE_AND, T_BITWISE_OR, T_BITWISE_XOR, T_SL, T_SR];
477+
475478
$operatorsPointers = [];
476479
$actualStartPointer = $parenthesisOpenerPointer + 1;
477480
while (true) {
478481
$pointer = TokenHelper::findNext(
479482
$phpcsFile,
480483
array_merge(
481484
self::OPERATORS,
482-
[T_OPEN_PARENTHESIS, T_INLINE_THEN, T_COALESCE, T_BITWISE_AND, T_BITWISE_OR, T_BITWISE_XOR],
485+
[T_OPEN_PARENTHESIS],
486+
$complicatedOperators,
483487
Tokens::$comparisonTokens
484488
),
485489
$actualStartPointer,
@@ -490,7 +494,7 @@ private function checkParenthesesAroundOperators(File $phpcsFile, int $parenthes
490494
break;
491495
}
492496

493-
if (in_array($tokens[$pointer]['code'], [T_INLINE_THEN, T_COALESCE, T_BITWISE_AND, T_BITWISE_OR, T_BITWISE_XOR], true)) {
497+
if (in_array($tokens[$pointer]['code'], $complicatedOperators, true)) {
494498
return;
495499
}
496500

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ function ($value) {
172172

173173
$char = (0xF0 & ord('1')) >> 4;
174174

175+
$char = ($ord - 0xc0 << 6) + ord($char[1]) - 0x80;
176+
$char = ($ord - 0xc0 >> 6) + ord($char[1]) - 0x80;
177+
175178
$anotherObject = new ($object->getClassName());
176179

177180
// Must be last

0 commit comments

Comments
 (0)