Skip to content

Commit 500f55b

Browse files
committed
ReferencedNameHelper: Fixed detection
1 parent 1f7bd4b commit 500f55b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,13 @@ private static function isReferencedName(File $phpcsFile, int $startPointer): bo
270270
if ($constPointer !== null && TokenHelper::findNext($phpcsFile, [T_OPEN_SHORT_ARRAY, T_ARRAY], $constPointer + 1, $startPointer) === null) {
271271
return false;
272272
}
273-
} elseif ($previousToken['code'] === T_BITWISE_AND && TokenHelper::findPreviousLocal($phpcsFile, [T_FUNCTION], $previousPointer - 1) !== null) {
274-
return false;
273+
} elseif ($previousToken['code'] === T_BITWISE_AND) {
274+
$pointerBefore = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
275+
$isFunctionPointerBefore = TokenHelper::findPreviousLocal($phpcsFile, T_FUNCTION, $previousPointer - 1) !== null;
276+
277+
if ($tokens[$pointerBefore]['code'] !== T_VARIABLE && $isFunctionPointerBefore) {
278+
return false;
279+
}
275280
} elseif ($previousToken['code'] === T_GOTO) {
276281
return false;
277282
}

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function testGetAllReferencedNames(): void
5252
['OPENSSL_ALGO_SHA512', false, true],
5353
['SomeTrait', false, false],
5454
['string', true, false],
55+
['doSomething', true, false],
56+
['STREAM_URL_STAT_QUIET', false, true],
5557
];
5658

5759
$names = ReferencedNameHelper::getAllReferencedNames($phpcsFile, 0);

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Doctrine\ORM\Mapping as ORM;
66
use UsedNamespace\UsedNameFooBar as UsedNameFooBarBaz;
77
use function DI\string;
8+
use function doSomething;
9+
use function stream_wrapper_restore;
10+
use const STREAM_URL_STAT_QUIET;
811

912
class FooClass extends \ExtendedClass implements \ImplementedInterface, \SecondImplementedInterface, \ThirdImplementedInterface
1013
{
@@ -110,3 +113,14 @@ class OpenSsl
110113
};
111114

112115
string();
116+
117+
function whatever($flags)
118+
{
119+
foreach ([] as $item) {
120+
doSomething($item);
121+
}
122+
123+
if ($flags & STREAM_URL_STAT_QUIET) {
124+
125+
}
126+
}

0 commit comments

Comments
 (0)