Support use function/const in name resolver

This commit is contained in:
nikic 2014-03-26 23:26:31 +01:00
parent 3b7829b011
commit a5e0bbcb62
2 changed files with 202 additions and 101 deletions

View File

@ -17,32 +17,21 @@ class NameResolver extends NodeVisitorAbstract
protected $namespace;
/**
* @var array Currently defined namespace and class aliases
* @var array Map of format [aliasType => [aliasName => originalName]]
*/
protected $aliases;
public function beforeTraverse(array $nodes) {
$this->namespace = null;
$this->aliases = array();
$this->resetState();
}
public function enterNode(Node $node) {
if ($node instanceof Stmt\Namespace_) {
$this->namespace = $node->name;
$this->aliases = array();
} elseif ($node instanceof Stmt\UseUse) {
$aliasName = strtolower($node->alias);
if (isset($this->aliases[$aliasName])) {
throw new Error(
sprintf(
'Cannot use "%s" as "%s" because the name is already in use',
$node->name, $node->alias
),
$node->getLine()
);
$this->resetState($node->name);
} elseif ($node instanceof Stmt\Use_) {
foreach ($node->uses as $use) {
$this->addAlias($use, $node->type);
}
$this->aliases[$aliasName] = $node->name;
} elseif ($node instanceof Stmt\Class_) {
if (null !== $node->extends) {
$node->extends = $this->resolveClassName($node->extends);
@ -78,12 +67,12 @@ class NameResolver extends NodeVisitorAbstract
}
} elseif ($node instanceof Stmt\Catch_) {
$node->type = $this->resolveClassName($node->type);
} elseif ($node instanceof Expr\FuncCall
|| $node instanceof Expr\ConstFetch
) {
} elseif ($node instanceof Expr\FuncCall) {
if ($node->name instanceof Name) {
$node->name = $this->resolveOtherName($node->name);
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION);
}
} elseif ($node instanceof Expr\ConstFetch) {
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_CONSTANT);
} elseif ($node instanceof Stmt\TraitUse) {
foreach ($node->traits as &$trait) {
$trait = $this->resolveClassName($trait);
@ -95,6 +84,42 @@ class NameResolver extends NodeVisitorAbstract
}
}
protected function resetState(Name $namespace = null) {
$this->namespace = $namespace;
$this->aliases = array(
Stmt\Use_::TYPE_NORMAL => array(),
Stmt\Use_::TYPE_FUNCTION => array(),
Stmt\Use_::TYPE_CONSTANT => array(),
);
}
protected function addAlias(Stmt\UseUse $use, $type) {
// Constant names are case sensitive, everything else case insensitive
if ($type === Stmt\Use_::TYPE_CONSTANT) {
$aliasName = $use->alias;
} else {
$aliasName = strtolower($use->alias);
}
if (isset($this->aliases[$type][$aliasName])) {
$typeStringMap = array(
Stmt\Use_::TYPE_NORMAL => '',
Stmt\Use_::TYPE_FUNCTION => 'function ',
Stmt\Use_::TYPE_CONSTANT => 'const ',
);
throw new Error(
sprintf(
'Cannot use %s%s as %s because the name is already in use',
$typeStringMap[$type], $use->name, $use->alias
),
$use->getLine()
);
}
$this->aliases[$type][$aliasName] = $use->name;
}
protected function resolveClassName(Name $name) {
// don't resolve special class names
if (in_array((string) $name, array('self', 'parent', 'static'))) {
@ -106,31 +131,43 @@ class NameResolver extends NodeVisitorAbstract
return $name;
}
// resolve aliases (for non-relative names)
$aliasName = strtolower($name->getFirst());
if (!$name->isRelative() && isset($this->aliases[$aliasName])) {
$name->setFirst($this->aliases[$aliasName]);
// if no alias exists prepend current namespace
if (!$name->isRelative() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
// resolve aliases (for non-relative names)
$name->setFirst($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName]);
} elseif (null !== $this->namespace) {
// if no alias exists prepend current namespace
$name->prepend($this->namespace);
}
return new Name\FullyQualified($name->parts, $name->getAttributes());
}
protected function resolveOtherName(Name $name) {
// fully qualified names are already resolved and we can't do anything about unqualified
// ones at compiler-time
if ($name->isFullyQualified() || $name->isUnqualified()) {
protected function resolveOtherName(Name $name, $type) {
// fully qualified names are already resolved
if ($name->isFullyQualified()) {
return $name;
}
// resolve aliases for qualified names
$aliasName = strtolower($name->getFirst());
if ($name->isQualified() && isset($this->aliases[$aliasName])) {
$name->setFirst($this->aliases[$aliasName]);
// prepend namespace for relative names
if ($name->isQualified() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
$name->setFirst($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName]);
} elseif ($name->isUnqualified()) {
if ($type === Stmt\Use_::TYPE_CONSTANT) {
// constant aliases are case-sensitive, function aliases case-insensitive
$aliasName = $name->getFirst();
}
if (isset($this->aliases[$type][$aliasName])) {
// resolve unqualified aliases
$name->set($this->aliases[$type][$aliasName]);
} else {
// unqualified, unaliased names cannot be resolved at compile-time
return $name;
}
} elseif (null !== $this->namespace) {
// if no alias exists prepend current namespace
$name->prepend($this->namespace);
}

View File

@ -13,7 +13,7 @@ class NameResolverTest extends \PHPUnit_Framework_TestCase
* @covers NameResolver
*/
public function testResolveNames() {
$code = <<<EOC
$code = <<<'EOC'
<?php
namespace Foo {
@ -21,62 +21,106 @@ namespace Foo {
new Bar();
new Hi();
new Hi\\Bar();
new \\Bar();
new namespace\\Bar();
new Hi\Bar();
new \Bar();
new namespace\Bar();
bar();
hi();
Hi\\bar();
foo\\bar();
\\bar();
namespace\\bar();
Hi\bar();
foo\bar();
\bar();
namespace\bar();
}
namespace {
use Hallo as Hi;
new Bar();
new Hi();
new Hi\\Bar();
new \\Bar();
new namespace\\Bar();
new Hi\Bar();
new \Bar();
new namespace\Bar();
bar();
hi();
Hi\\bar();
foo\\bar();
\\bar();
namespace\\bar();
Hi\bar();
foo\bar();
\bar();
namespace\bar();
}
namespace Bar {
use function foo\bar as baz;
use const foo\BAR as BAZ;
use foo as bar;
bar();
baz();
bar\foo();
baz\foo();
BAR();
BAZ();
BAR\FOO();
BAZ\FOO();
bar;
baz;
bar\foo;
baz\foo;
BAR;
BAZ;
BAR\FOO;
BAZ\FOO;
}
EOC;
$expectedCode = <<<EOC
$expectedCode = <<<'EOC'
namespace Foo {
use Hallo as Hi;
new \\Foo\\Bar();
new \\Hallo();
new \\Hallo\\Bar();
new \\Bar();
new \\Foo\\Bar();
new \Foo\Bar();
new \Hallo();
new \Hallo\Bar();
new \Bar();
new \Foo\Bar();
bar();
hi();
\\Hallo\\bar();
\\Foo\\foo\\bar();
\\bar();
\\Foo\\bar();
\Hallo\bar();
\Foo\foo\bar();
\bar();
\Foo\bar();
}
namespace {
use Hallo as Hi;
new \\Bar();
new \\Hallo();
new \\Hallo\\Bar();
new \\Bar();
new \\Bar();
new \Bar();
new \Hallo();
new \Hallo\Bar();
new \Bar();
new \Bar();
bar();
hi();
\\Hallo\\bar();
\\foo\\bar();
\\bar();
\\bar();
\Hallo\bar();
\foo\bar();
\bar();
\bar();
}
namespace Bar {
use function foo\bar as baz;
use const foo\BAR as BAZ;
use foo as bar;
bar();
\foo\bar();
\foo\foo();
\Bar\baz\foo();
BAR();
\foo\bar();
\foo\FOO();
\Bar\BAZ\FOO();
bar;
baz;
\foo\foo;
\Bar\baz\foo;
BAR;
\foo\BAR;
\foo\FOO;
\Bar\BAZ\FOO;
}
EOC;
@ -95,7 +139,7 @@ EOC;
* @covers NameResolver
*/
public function testResolveLocations() {
$code = <<<EOC
$code = <<<'EOC'
<?php
namespace NS;
@ -104,46 +148,46 @@ class A extends B implements C {
}
interface A extends C {
public function a(A \$a);
public function a(A $a);
}
A::b();
A::\$b;
A::$b;
A::B;
new A;
\$a instanceof A;
$a instanceof A;
namespace\a();
namespace\A;
try {
\$someThing;
} catch (A \$a) {
\$someThingElse;
$someThing;
} catch (A $a) {
$someThingElse;
}
EOC;
$expectedCode = <<<EOC
$expectedCode = <<<'EOC'
namespace NS;
class A extends \\NS\\B implements \\NS\\C
class A extends \NS\B implements \NS\C
{
use \\NS\\A;
use \NS\A;
}
interface A extends \\NS\\C
interface A extends \NS\C
{
public function a(\\NS\\A \$a);
public function a(\NS\A $a);
}
\\NS\\A::b();
\\NS\\A::\$b;
\\NS\\A::B;
new \\NS\\A();
\$a instanceof \\NS\\A;
\\NS\\a();
\\NS\\A;
\NS\A::b();
\NS\A::$b;
\NS\A::B;
new \NS\A();
$a instanceof \NS\A;
\NS\a();
\NS\A;
try {
\$someThing;
} catch (\\NS\\A \$a) {
\$someThingElse;
$someThing;
} catch (\NS\A $a) {
$someThingElse;
}
EOC;
@ -214,29 +258,49 @@ EOC;
}
/**
* @expectedException PhpParser\Error
* @expectedExceptionMessage Cannot use "C" as "B" because the name is already in use on line 2
* @dataProvider provideTestAlreadyInUseError
*/
public function testAlreadyInUseError() {
$stmts = array(
new Stmt\Use_(array(
new Stmt\UseUse(new Name('A\B'), 'B', array('startLine' => 1)),
new Stmt\UseUse(new Name('C'), 'B', array('startLine' => 2)),
))
);
public function testAlreadyInUseError(Stmt\Use_ $use, $errorMsg) {
$this->setExpectedException('PhpParser\Error', $errorMsg);
$traverser = new PhpParser\NodeTraverser;
$traverser->addVisitor(new NameResolver);
$traverser->traverse($stmts);
$traverser->traverse(array($use));
}
public function provideTestAlreadyInUseError() {
return array(
array(
new Stmt\Use_(array(
new Stmt\UseUse(new Name('A\B'), 'B', array('startLine' => 1)),
new Stmt\UseUse(new Name('C\D'), 'B', array('startLine' => 2)),
), Stmt\Use_::TYPE_NORMAL),
'Cannot use C\D as B because the name is already in use on line 2'
),
array(
new Stmt\Use_(array(
new Stmt\UseUse(new Name('a\b'), 'b', array('startLine' => 1)),
new Stmt\UseUse(new Name('c\d'), 'B', array('startLine' => 2)),
), Stmt\Use_::TYPE_FUNCTION),
'Cannot use function c\d as B because the name is already in use on line 2'
),
array(
new Stmt\Use_(array(
new Stmt\UseUse(new Name('A\B'), 'B', array('startLine' => 1)),
new Stmt\UseUse(new Name('C\D'), 'B', array('startLine' => 2)),
), Stmt\Use_::TYPE_CONSTANT),
'Cannot use const C\D as B because the name is already in use on line 2'
),
);
}
public function testClassNameIsCaseInsensitive()
{
$source = <<<EOC
$source = <<<'EOC'
<?php
namespace Foo;
use Bar\\Baz;
\$test = new baz();
use Bar\Baz;
$test = new baz();
EOC;
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);