diff --git a/src/UsedSymbolExtractor.php b/src/UsedSymbolExtractor.php index 7242cca..57135a0 100644 --- a/src/UsedSymbolExtractor.php +++ b/src/UsedSymbolExtractor.php @@ -169,6 +169,14 @@ public function parseUsedSymbols( } $usedSymbols[$kind][$symbolName][] = $token[2]; + + } elseif ( + $inGlobalScope + && $this->getTokenAfter($pointerAfterName)[0] === T_DOUBLE_COLON + ) { + // unqualified static access (e.g., Foo::class, Foo::method(), Foo::CONSTANT) in global scope + // register to allow detection of classes not in $knownSymbols + $usedSymbols[SymbolKind::CLASSLIKE][$name][] = $token[2]; } break; @@ -235,6 +243,15 @@ public function parseUsedSymbols( $symbolName = $name; $kind = $this->getFqnSymbolKind($pointerBeforeName, $pointerAfterName, false); $usedSymbols[$kind][$symbolName][] = $token[2]; + + } elseif ( + strpos($name, '\\') === false + && $inGlobalScope + && $this->getTokenAfter($pointerAfterName)[0] === T_DOUBLE_COLON + ) { + // unqualified static access (e.g., Foo::class, Foo::method(), Foo::CONSTANT) in global scope + // register to allow detection of classes not in $knownSymbols + $usedSymbols[SymbolKind::CLASSLIKE][$name][] = $token[2]; } } diff --git a/tests/UsedSymbolExtractorTest.php b/tests/UsedSymbolExtractorTest.php index d555525..c3b706f 100644 --- a/tests/UsedSymbolExtractorTest.php +++ b/tests/UsedSymbolExtractorTest.php @@ -129,6 +129,9 @@ public function provideVariants(): iterable SymbolKind::CLASSLIKE => [ 'DateTimeImmutable' => [3], 'PHPUnit\Framework\Error' => [5], + 'UnknownClass' => [17, 18, 19], // issue #224: unqualified static access in global scope + 'self' => [22], // filtered by Analyser via ignoredSymbols + 'parent' => [24], // filtered by Analyser via ignoredSymbols ], SymbolKind::FUNCTION => [ 'PHPUnit\Framework\assertSame' => [7], @@ -188,6 +191,7 @@ public function provideVariants(): iterable 'PDO' => [11], 'My\App\XMLReader' => [15], 'CURLOPT_SSL_VERIFYHOST' => [19], + 'ZipArchive' => [22], // issue #224: now detected via unqualified static access ], ], self::extensionSymbolsForExtensionsTestCases(), diff --git a/tests/data/not-autoloaded/used-symbols/global-namespace.php b/tests/data/not-autoloaded/used-symbols/global-namespace.php index 737966a..a4df4e0 100644 --- a/tests/data/not-autoloaded/used-symbols/global-namespace.php +++ b/tests/data/not-autoloaded/used-symbols/global-namespace.php @@ -12,3 +12,13 @@ public function someFunction(string $foo): void user_defined_function(); } } + +// Test for issue #224: unqualified static access in global scope +$class = UnknownClass::class; +UnknownClass::staticMethod(); +UnknownClass::CONSTANT; + +// These should NOT be detected as class usages +self::FOO; +static::bar(); +parent::__construct();