Make NameResolver add namespacedName subnodes

This commit is contained in:
nikic 2011-11-12 13:24:59 +01:00
parent c44f6375ef
commit 2ec6ae4b03
2 changed files with 51 additions and 1 deletions

View File

@ -41,10 +41,20 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract
foreach ($node->implements as &$interface) { foreach ($node->implements as &$interface) {
$interface = $this->resolveClassName($interface); $interface = $this->resolveClassName($interface);
} }
$this->addNamespacedName($node);
} elseif ($node instanceof PHPParser_Node_Stmt_Interface) { } elseif ($node instanceof PHPParser_Node_Stmt_Interface) {
foreach ($node->extends as &$interface) { foreach ($node->extends as &$interface) {
$interface = $this->resolveClassName($interface); $interface = $this->resolveClassName($interface);
} }
$this->addNamespacedName($node);
} elseif ($node instanceof PHPParser_Node_Stmt_Function) {
$this->addNamespacedName($node);
} elseif ($node instanceof PHPParser_Node_Stmt_Const) {
foreach ($node->consts as $const) {
$this->addNamespacedName($const);
}
} elseif ($node instanceof PHPParser_Node_Expr_StaticCall } elseif ($node instanceof PHPParser_Node_Expr_StaticCall
|| $node instanceof PHPParser_Node_Expr_StaticPropertyFetch || $node instanceof PHPParser_Node_Expr_StaticPropertyFetch
|| $node instanceof PHPParser_Node_Expr_ClassConstFetch || $node instanceof PHPParser_Node_Expr_ClassConstFetch
@ -106,4 +116,13 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract
return new PHPParser_Node_Name_FullyQualified($name->parts); return new PHPParser_Node_Name_FullyQualified($name->parts);
} }
protected function addNamespacedName(PHPParser_Node $node) {
if (null !== $this->namespace) {
$node->namespacedName = clone $this->namespace;
$node->namespacedName->append($node->name);
} else {
$node->namespacedName = new PHPParser_Node_Name($node->name);
}
}
} }

View File

@ -2,7 +2,7 @@
class PHPParser_Tests_NodeVisitor_NameResolverTest extends PHPUnit_Framework_TestCase class PHPParser_Tests_NodeVisitor_NameResolverTest extends PHPUnit_Framework_TestCase
{ {
public function testResolve() { public function testResolveNames() {
$code = <<<EOC $code = <<<EOC
<?php <?php
@ -80,4 +80,35 @@ EOC;
$this->assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts)); $this->assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts));
} }
public function testAddNamespacedName() {
$code = <<<EOC
<?php
namespace Foo {
class A {}
function B() {}
const C = 'D';
}
namespace {
class A {}
function B() {}
const C = 'D';
}
EOC;
$parser = new PHPParser_Parser;
$traverser = new PHPParser_NodeTraverser;
$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver);
$stmts = $parser->parse(new PHPParser_Lexer($code));
$stmts = $traverser->traverse($stmts);
$this->assertEquals('Foo\\A', (string) $stmts[0]->stmts[0]->namespacedName);
$this->assertEquals('Foo\\B', (string) $stmts[0]->stmts[1]->namespacedName);
$this->assertEquals('Foo\\C', (string) $stmts[0]->stmts[2]->consts[0]->namespacedName);
$this->assertEquals('A', (string) $stmts[1]->stmts[0]->namespacedName);
$this->assertEquals('B', (string) $stmts[1]->stmts[1]->namespacedName);
$this->assertEquals('C', (string) $stmts[1]->stmts[2]->consts[0]->namespacedName);
}
} }