Skip to content

Commit a108576

Browse files
committed
UselessParenthesesSniff: Fixed false positive
1 parent 113c268 commit a108576

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

SlevomatCodingStandard/Sniffs/PHP/UselessParenthesesSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ private function checkParenthesesAroundConditionInTernaryOperator(File $phpcsFil
7373
{
7474
$tokens = $phpcsFile->getTokens();
7575

76-
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
76+
$pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1);
77+
if ($tokens[$pointerBeforeParenthesisOpener]['code'] === T_BOOLEAN_NOT) {
78+
return;
79+
}
7780

81+
$ternaryOperatorPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] + 1);
7882
if ($tokens[$ternaryOperatorPointer]['code'] !== T_INLINE_THEN) {
7983
return;
8084
}

tests/Sniffs/PHP/data/uselessParenthesesNoErrors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function doSomething($parameter) {
4747

4848
$z = ($a ? '0' : '1') ? '2' : '3';
4949
$zz = ($a + $b);
50+
$zzz = !($a) ? true : false;
5051

5152
exit($a);
5253
die($a);

0 commit comments

Comments
 (0)