Skip to content

Commit 975e587

Browse files
authored
Detect unqualified static access in global scope (#243)
1 parent 735ee14 commit 975e587

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/UsedSymbolExtractor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ public function parseUsedSymbols(
169169
}
170170

171171
$usedSymbols[$kind][$symbolName][] = $token[2];
172+
173+
} elseif (
174+
$inGlobalScope
175+
&& $this->getTokenAfter($pointerAfterName)[0] === T_DOUBLE_COLON
176+
) {
177+
// unqualified static access (e.g., Foo::class, Foo::method(), Foo::CONSTANT) in global scope
178+
// register to allow detection of classes not in $knownSymbols
179+
$usedSymbols[SymbolKind::CLASSLIKE][$name][] = $token[2];
172180
}
173181

174182
break;
@@ -235,6 +243,15 @@ public function parseUsedSymbols(
235243
$symbolName = $name;
236244
$kind = $this->getFqnSymbolKind($pointerBeforeName, $pointerAfterName, false);
237245
$usedSymbols[$kind][$symbolName][] = $token[2];
246+
247+
} elseif (
248+
strpos($name, '\\') === false
249+
&& $inGlobalScope
250+
&& $this->getTokenAfter($pointerAfterName)[0] === T_DOUBLE_COLON
251+
) {
252+
// unqualified static access (e.g., Foo::class, Foo::method(), Foo::CONSTANT) in global scope
253+
// register to allow detection of classes not in $knownSymbols
254+
$usedSymbols[SymbolKind::CLASSLIKE][$name][] = $token[2];
238255
}
239256
}
240257

tests/UsedSymbolExtractorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ public function provideVariants(): iterable
129129
SymbolKind::CLASSLIKE => [
130130
'DateTimeImmutable' => [3],
131131
'PHPUnit\Framework\Error' => [5],
132+
'UnknownClass' => [17, 18, 19], // issue #224: unqualified static access in global scope
133+
'self' => [22], // filtered by Analyser via ignoredSymbols
134+
'parent' => [24], // filtered by Analyser via ignoredSymbols
132135
],
133136
SymbolKind::FUNCTION => [
134137
'PHPUnit\Framework\assertSame' => [7],
@@ -188,6 +191,7 @@ public function provideVariants(): iterable
188191
'PDO' => [11],
189192
'My\App\XMLReader' => [15],
190193
'CURLOPT_SSL_VERIFYHOST' => [19],
194+
'ZipArchive' => [22], // issue #224: now detected via unqualified static access
191195
],
192196
],
193197
self::extensionSymbolsForExtensionsTestCases(),

tests/data/not-autoloaded/used-symbols/global-namespace.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ public function someFunction(string $foo): void
1212
user_defined_function();
1313
}
1414
}
15+
16+
// Test for issue #224: unqualified static access in global scope
17+
$class = UnknownClass::class;
18+
UnknownClass::staticMethod();
19+
UnknownClass::CONSTANT;
20+
21+
// These should NOT be detected as class usages
22+
self::FOO;
23+
static::bar();
24+
parent::__construct();

0 commit comments

Comments
 (0)