1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 12:46:47 +01:00

Fix name resolver (class names are case insensitive)

This commit is contained in:
Jean-François Simon 2013-11-13 11:49:45 +01:00 committed by nikic
parent 700847e295
commit 7f4ab26732
3 changed files with 35 additions and 9 deletions

View File

@ -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

View File

@ -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());
}
}
}
}

View File

@ -222,4 +222,25 @@ EOC;
$traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver);
$traverser->traverse($stmts);
}
}
public function testClassNameIsCaseInsensitive()
{
$source = <<<EOC
<?php
namespace Foo;
use Bar\\Baz;
\$test = new baz();
EOC;
$parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative);
$stmts = $parser->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);
}
}