diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bd88c1..a7a4d2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ Version 0.9.5-dev ----------------- -* Added `NodeTraverser::removeVisitor()` method, which removes a visitor from the node traverser. This also modifies the +* Add `NodeTraverser::removeVisitor()` method, which removes a visitor from the node traverser. This also modifies the corresponding `NodeTraverserInterface`. +* Fix alias resolution in `NameResolver`: Class names are now correctly handled as case-insensitive. + Version 0.9.4 (25.08.2013) -------------------------- * [PHP 5.5] Add support for `ClassName::class`. This is parsed as an `Expr_ClassConstFetch` with `'class'` being the diff --git a/lib/PHPParser/NodeVisitor/NameResolver.php b/lib/PHPParser/NodeVisitor/NameResolver.php index bcfb808..25bffed 100644 --- a/lib/PHPParser/NodeVisitor/NameResolver.php +++ b/lib/PHPParser/NodeVisitor/NameResolver.php @@ -22,7 +22,8 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract $this->namespace = $node->name; $this->aliases = array(); } elseif ($node instanceof PHPParser_Node_Stmt_UseUse) { - if (isset($this->aliases[$node->alias])) { + $aliasName = strtolower($node->alias); + if (isset($this->aliases[$aliasName])) { throw new PHPParser_Error( sprintf( 'Cannot use "%s" as "%s" because the name is already in use', @@ -32,7 +33,7 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract ); } - $this->aliases[$node->alias] = $node->name; + $this->aliases[$aliasName] = $node->name; } elseif ($node instanceof PHPParser_Node_Stmt_Class) { if (null !== $node->extends) { $node->extends = $this->resolveClassName($node->extends); @@ -97,8 +98,9 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract } // resolve aliases (for non-relative names) - if (!$name->isRelative() && isset($this->aliases[$name->getFirst()])) { - $name->setFirst($this->aliases[$name->getFirst()]); + $aliasName = strtolower($name->getFirst()); + if (!$name->isRelative() && isset($this->aliases[$aliasName])) { + $name->setFirst($this->aliases[$aliasName]); // if no alias exists prepend current namespace } elseif (null !== $this->namespace) { $name->prepend($this->namespace); @@ -115,8 +117,9 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract } // resolve aliases for qualified names - if ($name->isQualified() && isset($this->aliases[$name->getFirst()])) { - $name->setFirst($this->aliases[$name->getFirst()]); + $aliasName = strtolower($name->getFirst()); + if ($name->isQualified() && isset($this->aliases[$aliasName])) { + $name->setFirst($this->aliases[$aliasName]); // prepend namespace for relative names } elseif (null !== $this->namespace) { $name->prepend($this->namespace); @@ -133,4 +136,4 @@ class PHPParser_NodeVisitor_NameResolver extends PHPParser_NodeVisitorAbstract $node->namespacedName = new PHPParser_Node_Name($node->name, $node->getAttributes()); } } -} \ No newline at end of file +} diff --git a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php index 49eaf69..2570b93 100644 --- a/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php +++ b/test/PHPParser/Tests/NodeVisitor/NameResolverTest.php @@ -222,4 +222,25 @@ EOC; $traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver); $traverser->traverse($stmts); } -} \ No newline at end of file + + public function testClassNameIsCaseInsensitive() + { + $source = <<parse($source); + + $traverser = new PHPParser_NodeTraverser; + $traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver); + + $stmts = $traverser->traverse($stmts); + $stmt = $stmts[0]; + + $this->assertEquals(array('Bar', 'Baz'), $stmt->stmts[1]->expr->class->parts); + } +}