Skip to content

Commit 90834bf

Browse files
committed
Add namespacedName attribute on runtime-resolved names
The NameResolver now adds a namespacedName attribute on function/ const names which cannot be statically resolved.
1 parent a910f6a commit 90834bf

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/PhpParser/NodeVisitor/NameResolver.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,21 @@ protected function resolveOtherName(Name $name, $type) {
208208
$aliasName = $name->getFirst();
209209
}
210210

211-
if (!isset($this->aliases[$type][$aliasName])) {
212-
// unqualified, unaliased names cannot be resolved at compile-time
213-
return $name;
211+
if (isset($this->aliases[$type][$aliasName])) {
212+
// resolve unqualified aliases
213+
return new FullyQualified($this->aliases[$type][$aliasName], $name->getAttributes());
214214
}
215215

216-
// resolve unqualified aliases
217-
return new FullyQualified($this->aliases[$type][$aliasName], $name->getAttributes());
216+
if (null === $this->namespace) {
217+
// outside of a namespace unaliased unqualified is same as fully qualified
218+
return new FullyQualified($name->parts, $name->getAttributes());
219+
}
220+
221+
// unqualified names inside a namespace cannot be resolved at compile-time
222+
// add the namespaced version of the name as an attribute
223+
$name->setAttribute('namespacedName',
224+
FullyQualified::concat($this->namespace, $name, $name->getAttributes()));
225+
return $name;
218226
}
219227

220228
if (null !== $this->namespace) {

test/PhpParser/NodeVisitor/NameResolverTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function testResolveNames() {
118118
new \Hallo\Bar();
119119
new \Bar();
120120
new \Bar();
121-
bar();
122-
hi();
121+
\bar();
122+
\hi();
123123
\Hallo\bar();
124124
\foo\bar();
125125
\bar();
@@ -278,7 +278,7 @@ public function testNoResolveSpecialName() {
278278
$this->assertEquals($stmts, $traverser->traverse($stmts));
279279
}
280280

281-
public function testAddNamespacedName() {
281+
public function testAddDeclarationNamespacedName() {
282282
$nsStmts = array(
283283
new Stmt\Class_('A'),
284284
new Stmt\Interface_('B'),
@@ -310,6 +310,29 @@ public function testAddNamespacedName() {
310310
$this->assertObjectNotHasAttribute('namespacedName', $stmts[0]->stmts[5]->class);
311311
}
312312

313+
public function testAddRuntimeResolvedNamespacedName() {
314+
$stmts = array(
315+
new Stmt\Namespace_(new Name('NS'), array(
316+
new Expr\FuncCall(new Name('foo')),
317+
new Expr\ConstFetch(new Name('FOO')),
318+
)),
319+
new Stmt\Namespace_(null, array(
320+
new Expr\FuncCall(new Name('foo')),
321+
new Expr\ConstFetch(new Name('FOO')),
322+
)),
323+
);
324+
325+
$traverser = new PhpParser\NodeTraverser;
326+
$traverser->addVisitor(new NameResolver);
327+
$stmts = $traverser->traverse($stmts);
328+
329+
$this->assertSame('NS\\foo', (string) $stmts[0]->stmts[0]->name->getAttribute('namespacedName'));
330+
$this->assertSame('NS\\FOO', (string) $stmts[0]->stmts[1]->name->getAttribute('namespacedName'));
331+
332+
$this->assertFalse($stmts[1]->stmts[0]->name->hasAttribute('namespacedName'));
333+
$this->assertFalse($stmts[1]->stmts[1]->name->hasAttribute('namespacedName'));
334+
}
335+
313336
/**
314337
* @dataProvider provideTestError
315338
*/

0 commit comments

Comments
 (0)