Skip to content

Commit

Permalink
Support qualified names in global scope (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Jul 2, 2024
1 parent b63a9ac commit 6bbe1d0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ Another approach for DIC-only usages is to scan the generated php file, but that

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

## Contributing:
- Check your code by `composer check`
Expand Down
14 changes: 14 additions & 0 deletions src/UsedSymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function is_array;
use function ltrim;
use function strlen;
use function strpos;
use function substr;
use function token_get_all;
use const PHP_VERSION_ID;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function parseUsedSymbols(): array

$level = 0; // {, }, {$, ${
$squareLevel = 0; // [, ], #[
$inGlobalScope = true;
$inClassLevel = null;
$inAttributeSquareLevel = null;

Expand Down Expand Up @@ -105,6 +107,7 @@ public function parseUsedSymbols(): array
break;

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

Expand All @@ -121,6 +124,11 @@ public function parseUsedSymbols(): array
$symbolName = $useStatements[$neededAlias] . substr($token[1], strlen($neededAlias));
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
$usedSymbols[$kind][$symbolName][] = $token[2];

} elseif ($inGlobalScope) {
$symbolName = $token[1];
$kind = $this->getFqnSymbolKind($this->pointer - 2, $this->pointer, $inAttributeSquareLevel !== null);
$usedSymbols[$kind][$symbolName][] = $token[2];
}

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

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

break;
Expand Down Expand Up @@ -173,6 +182,11 @@ public function parseUsedSymbols(): array
$symbolName = $useStatements[$neededAlias] . substr($name, strlen($neededAlias));
$kind = $this->getFqnSymbolKind($pointerBeforeName, $this->pointer - 1, false);
$usedSymbols[$kind][$symbolName][] = $token[2];

} elseif ($inGlobalScope && strpos($name, '\\') !== false) {
$symbolName = $name;
$kind = $this->getFqnSymbolKind($pointerBeforeName, $this->pointer - 1, false);
$usedSymbols[$kind][$symbolName][] = $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 @@ -105,6 +105,10 @@ public function provideVariants(): iterable
[
SymbolKind::CLASSLIKE => [
'DateTimeImmutable' => [3],
'PHPUnit\Framework\Error' => [5],
],
SymbolKind::FUNCTION => [
'PHPUnit\Framework\assertSame' => [7],
],
],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/data/not-autoloaded/used-symbols/global-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

new \DateTimeImmutable();
new DateTime;
new PHPUnit\Framework\Error();

PHPUnit\Framework\assertSame(1, 1);

class Foo {
public function someFunction(string $foo): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
class Foo {}
new namespace\Foo();
new DateTimeImmutable();
new Foo\Bar; // is Relative\Foo\Bar

0 comments on commit 6bbe1d0

Please sign in to comment.