From 0c2ac3e6dfe60472d671825862ba350cc45b7ce9 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 3 Apr 2022 12:09:17 +0200 Subject: [PATCH] Fix resolving `namespace` keyword --- ChangeLog.md | 6 ++++++ src/main/php/lang/ast/Scope.class.php | 2 ++ .../php/lang/ast/unittest/ScopeTest.class.php | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 7380a2a..d62bb4f 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,12 @@ XP AST ChangeLog ## ?.?.? / ????-??-?? +## 8.0.1 / 2022-04-03 + +* Fixed resolving types starting with `namespace` keyword (examples 4 and + 5 in https://www.php.net/manual/en/language.namespaces.nsconstants.php) + (@thekid) + ## 8.0.0 / 2022-01-07 This major release promotes annotations and comments to *Node* subclasses, diff --git a/src/main/php/lang/ast/Scope.class.php b/src/main/php/lang/ast/Scope.class.php index e626dd7..22079a7 100755 --- a/src/main/php/lang/ast/Scope.class.php +++ b/src/main/php/lang/ast/Scope.class.php @@ -72,6 +72,8 @@ public function resolve($name) { return $name; } else if (isset($this->imports[$name])) { return $this->imports[$name]; + } else if (0 === strncmp($name, 'namespace\\', 10)) { + return $this->package.'\\'.substr($name, 10); } else if ($this->package) { return $this->package.'\\'.$name; } else if ($this->parent) { diff --git a/src/test/php/lang/ast/unittest/ScopeTest.class.php b/src/test/php/lang/ast/unittest/ScopeTest.class.php index f6dc372..efd04f6 100755 --- a/src/test/php/lang/ast/unittest/ScopeTest.class.php +++ b/src/test/php/lang/ast/unittest/ScopeTest.class.php @@ -83,4 +83,19 @@ public function resolve_global() { Assert::equals('\\Traversable', $s->resolve('Traversable')); } + #[Test] + public function resolve_namespace_keyword() { + $s= new Scope(); + $s->package('test'); + + Assert::equals('\\test\\Date', $s->resolve('namespace\\Date')); + } + + #[Test] + public function resolve_sub_namespace() { + $s= new Scope(); + $s->package('test'); + + Assert::equals('\\test\\util\\Date', $s->resolve('namespace\\util\\Date')); + } } \ No newline at end of file