Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/UsedSymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/UsedSymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 10 additions & 0 deletions tests/data/not-autoloaded/used-symbols/global-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Loading