From 2ec6ae4b0306b5fd66d8fa9a98a838a5b691c1ed Mon Sep 17 00:00:00 2001 From: nikic Date: Sat, 12 Nov 2011 13:24:59 +0100 Subject: [PATCH] Make NameResolver add namespacedName subnodes --- lib/PHPParser/NodeVisitor/NameResolver.php | 19 +++++++++++ .../Tests/NodeVisitor/NameResolverTest.php | 33 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/PHPParser/NodeVisitor/NameResolver.php b/lib/PHPParser/NodeVisitor/NameResolver.php index 1762179..4ef9afb 100644 --- a/lib/PHPParser/NodeVisitor/NameResolver.php +++ b/lib/PHPParser/NodeVisitor/NameResolver.php @@ -41,10 +41,20 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract foreach ($node->implements as &$interface) { $interface = $this->resolveClassName($interface); } + + $this->addNamespacedName($node); } elseif ($node instanceof PHPParser_Node_Stmt_Interface) { foreach ($node->extends as &$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 || $node instanceof PHPParser_Node_Expr_StaticPropertyFetch || $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); } + + 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); + } + } } \ No newline at end of file diff --git a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php index fab39e0..c39dcef 100644 --- a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php +++ b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php @@ -2,7 +2,7 @@ class PHPParser_Tests_NodeVisitor_NameResolverTest extends PHPUnit_Framework_TestCase { - public function testResolve() { + public function testResolveNames() { $code = <<assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts)); } + + public function testAddNamespacedName() { + $code = <<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); + } } \ No newline at end of file