Skip to content

Commit 9e6619c

Browse files
committed
RequireNullSafeObjectOperatorSniff: Fixed false positie
1 parent 1f46614 commit 9e6619c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullSafeObjectOperatorSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use const T_NULLSAFE_OBJECT_OPERATOR;
3030
use const T_OBJECT_OPERATOR;
3131
use const T_OPEN_PARENTHESIS;
32+
use const T_SEMICOLON;
3233
use const T_STRING;
3334

3435
class RequireNullSafeObjectOperatorSniff implements Sniff
@@ -242,6 +243,17 @@ private function checkNextCondition(
242243
return;
243244
}
244245

246+
$pointerAfterNexIdentificator = TokenHelper::findNextEffective($phpcsFile, $nextIdentificatorEndPointer + 1);
247+
248+
$tokens = $phpcsFile->getTokens();
249+
250+
if (
251+
$tokens[$pointerAfterNexIdentificator]['code'] !== $tokens[$identicalPointer]['code']
252+
&& !in_array($tokens[$pointerAfterNexIdentificator]['code'], [T_INLINE_THEN, T_SEMICOLON], true)
253+
) {
254+
return;
255+
}
256+
245257
$identificatorDifference = $this->getIdentificatorDifference(
246258
$phpcsFile,
247259
$identificator,

tests/Sniffs/ControlStructures/data/requireNullSafeObjectOperatorNoErrors.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
if ($a !== null && $a === 0) {
3333
// Something
3434
}
35+
36+
if ($a === null || $a->property) {
37+
// Something
38+
}

0 commit comments

Comments
 (0)