Skip to content

Commit 72d9148

Browse files
committed
RequireNullCoalesceOperatorSniff: Fixed false positive
1 parent ef438f8 commit 72d9148

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceOperatorSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
89
use SlevomatCodingStandard\Helpers\TokenHelper;
910
use const T_BOOLEAN_NOT;
@@ -136,6 +137,12 @@ public function checkIdenticalOperator(File $phpcsFile, int $identicalOperator):
136137
return;
137138
}
138139

140+
$pointerBeforeCondition = TokenHelper::findPreviousEffective($phpcsFile, ($isYodaCondition ? $pointerBeforeIdenticalOperator : $variableStartPointer) - 1);
141+
142+
if (in_array($tokens[$pointerBeforeCondition]['code'], Tokens::$booleanOperators, true)) {
143+
return;
144+
}
145+
139146
/** @var int $inlineThenPointer */
140147
$inlineThenPointer = TokenHelper::findNextEffective($phpcsFile, ($isYodaCondition ? $variableEndPointer : $pointerAfterIdenticalOperator) + 1);
141148
if ($tokens[$inlineThenPointer]['code'] !== T_INLINE_THEN) {

tests/Sniffs/ControlStructures/data/requireNullCoalesceOperatorNoErrors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@
3535
$o = $oo !== null ? $oo->property : null;
3636

3737
$p = $pp === null ? null : $pp + 1;
38+
39+
$q = $r !== null && $s !== null
40+
? $s
41+
: '';

0 commit comments

Comments
 (0)