Skip to content

Commit

Permalink
UsedSymbolExtractor: fix incorrect detection of attribute (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 11, 2024
1 parent da9eb49 commit 82b9f82
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/UsedSymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function token_get_all;
use const PHP_VERSION_ID;
use const T_AS;
use const T_ATTRIBUTE;
use const T_CLASS;
use const T_COMMENT;
use const T_CONST;
Expand Down Expand Up @@ -337,9 +338,13 @@ private function getFqnSymbolKind(int $pointerBeforeName, int $pointerAfterName)
break;
} while ($pointerAfterName < $this->numTokens);

// phpcs:disable Squiz.PHP.CommentedOutCode.Found
if (
$tokenAfterName === '('
&& $tokenBeforeName[0] !== T_NEW // eliminate new \ClassName( syntax
&& !(
$tokenBeforeName[0] === T_NEW // eliminate new \ClassName(
|| (PHP_VERSION_ID > 80000 && $tokenBeforeName[0] === T_ATTRIBUTE) // eliminate #[\AttributeName(
)
) {
return SymbolKind::FUNCTION;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/UsedSymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use function file_get_contents;
use const PHP_VERSION_ID;

class UsedSymbolExtractorTest extends TestCase
{
Expand Down Expand Up @@ -112,6 +113,17 @@ public function provideVariants(): iterable
__DIR__ . '/data/not-autoloaded/used-symbols/curly-braces.php',
[]
];

yield 'attribute' => [
__DIR__ . '/data/not-autoloaded/used-symbols/attribute.php',
PHP_VERSION_ID >= 80000
? [
SymbolKind::CLASSLIKE => [
'SomeAttribute' => [3],
],
]
: []
];
}

}
4 changes: 4 additions & 0 deletions tests/data/not-autoloaded/used-symbols/attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

#[\SomeAttribute()]
class ClassWithAttribute {}

0 comments on commit 82b9f82

Please sign in to comment.