Skip to content

Commit 82b9f82

Browse files
authored
UsedSymbolExtractor: fix incorrect detection of attribute (#121)
1 parent da9eb49 commit 82b9f82

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/UsedSymbolExtractor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function token_get_all;
1212
use const PHP_VERSION_ID;
1313
use const T_AS;
14+
use const T_ATTRIBUTE;
1415
use const T_CLASS;
1516
use const T_COMMENT;
1617
use const T_CONST;
@@ -337,9 +338,13 @@ private function getFqnSymbolKind(int $pointerBeforeName, int $pointerAfterName)
337338
break;
338339
} while ($pointerAfterName < $this->numTokens);
339340

341+
// phpcs:disable Squiz.PHP.CommentedOutCode.Found
340342
if (
341343
$tokenAfterName === '('
342-
&& $tokenBeforeName[0] !== T_NEW // eliminate new \ClassName( syntax
344+
&& !(
345+
$tokenBeforeName[0] === T_NEW // eliminate new \ClassName(
346+
|| (PHP_VERSION_ID > 80000 && $tokenBeforeName[0] === T_ATTRIBUTE) // eliminate #[\AttributeName(
347+
)
343348
) {
344349
return SymbolKind::FUNCTION;
345350
}

tests/UsedSymbolExtractorTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use function file_get_contents;
7+
use const PHP_VERSION_ID;
78

89
class UsedSymbolExtractorTest extends TestCase
910
{
@@ -112,6 +113,17 @@ public function provideVariants(): iterable
112113
__DIR__ . '/data/not-autoloaded/used-symbols/curly-braces.php',
113114
[]
114115
];
116+
117+
yield 'attribute' => [
118+
__DIR__ . '/data/not-autoloaded/used-symbols/attribute.php',
119+
PHP_VERSION_ID >= 80000
120+
? [
121+
SymbolKind::CLASSLIKE => [
122+
'SomeAttribute' => [3],
123+
],
124+
]
125+
: []
126+
];
115127
}
116128

117129
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
#[\SomeAttribute()]
4+
class ClassWithAttribute {}

0 commit comments

Comments
 (0)