Skip to content

Commit 6bbe1d0

Browse files
authored
Support qualified names in global scope (#148)
1 parent b63a9ac commit 6bbe1d0

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ Another approach for DIC-only usages is to scan the generated php file, but that
161161

162162
## Limitations:
163163
- Extension dependencies are not analysed (e.g. `ext-json`)
164-
- Files without namespace has limited support
165-
- Only symbols with use statements and FQNs are detected
166164

167165
## Contributing:
168166
- Check your code by `composer check`

src/UsedSymbolExtractor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use function is_array;
88
use function ltrim;
99
use function strlen;
10+
use function strpos;
1011
use function substr;
1112
use function token_get_all;
1213
use const PHP_VERSION_ID;
@@ -72,6 +73,7 @@ public function parseUsedSymbols(): array
7273

7374
$level = 0; // {, }, {$, ${
7475
$squareLevel = 0; // [, ], #[
76+
$inGlobalScope = true;
7577
$inClassLevel = null;
7678
$inAttributeSquareLevel = null;
7779

@@ -105,6 +107,7 @@ public function parseUsedSymbols(): array
105107
break;
106108

107109
case PHP_VERSION_ID >= 80000 ? T_NAMESPACE : -1:
110+
$inGlobalScope = false;
108111
$useStatements = []; // reset use statements on namespace change
109112
break;
110113

@@ -121,6 +124,11 @@ public function parseUsedSymbols(): array
121124
$symbolName = $useStatements[$neededAlias] . substr($token[1], strlen($neededAlias));
122125
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
123126
$usedSymbols[$kind][$symbolName][] = $token[2];
127+
128+
} elseif ($inGlobalScope) {
129+
$symbolName = $token[1];
130+
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
131+
$usedSymbols[$kind][$symbolName][] = $token[2];
124132
}
125133

126134
break;
@@ -142,6 +150,7 @@ public function parseUsedSymbols(): array
142150

143151
if (substr($nextName, 0, 1) !== '\\') { // not a namespace-relative name, but a new namespace declaration
144152
$useStatements = []; // reset use statements on namespace change
153+
$inGlobalScope = false;
145154
}
146155

147156
break;
@@ -173,6 +182,11 @@ public function parseUsedSymbols(): array
173182
$symbolName = $useStatements[$neededAlias] . substr($name, strlen($neededAlias));
174183
$kind = $this->getFqnSymbolKind($pointerBeforeName, $this->pointer - 1, false);
175184
$usedSymbols[$kind][$symbolName][] = $token[2];
185+
186+
} elseif ($inGlobalScope && strpos($name, '\\') !== false) {
187+
$symbolName = $name;
188+
$kind = $this->getFqnSymbolKind($pointerBeforeName, $this->pointer - 1, false);
189+
$usedSymbols[$kind][$symbolName][] = $token[2];
176190
}
177191
}
178192

tests/UsedSymbolExtractorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public function provideVariants(): iterable
105105
[
106106
SymbolKind::CLASSLIKE => [
107107
'DateTimeImmutable' => [3],
108+
'PHPUnit\Framework\Error' => [5],
109+
],
110+
SymbolKind::FUNCTION => [
111+
'PHPUnit\Framework\assertSame' => [7],
108112
],
109113
],
110114
];

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
new \DateTimeImmutable();
44
new DateTime;
5+
new PHPUnit\Framework\Error();
56

7+
PHPUnit\Framework\assertSame(1, 1);
68

79
class Foo {
810
public function someFunction(string $foo): void

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
class Foo {}
99
new namespace\Foo();
1010
new DateTimeImmutable();
11+
new Foo\Bar; // is Relative\Foo\Bar

0 commit comments

Comments
 (0)