2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
use PhpParser\Error;
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
2015-07-12 23:55:57 +02:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
class NameResolver extends NodeVisitorAbstract
|
2011-09-28 20:14:27 +02:00
|
|
|
{
|
2015-03-12 13:17:31 +01:00
|
|
|
/** @var null|Name Current namespace */
|
2011-09-28 20:14:27 +02:00
|
|
|
protected $namespace;
|
|
|
|
|
2015-03-12 13:17:31 +01:00
|
|
|
/** @var array Map of format [aliasType => [aliasName => originalName]] */
|
2011-09-28 20:14:27 +02:00
|
|
|
protected $aliases;
|
|
|
|
|
|
|
|
public function beforeTraverse(array $nodes) {
|
2014-03-26 23:26:31 +01:00
|
|
|
$this->resetState();
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
public function enterNode(Node $node) {
|
|
|
|
if ($node instanceof Stmt\Namespace_) {
|
2014-03-26 23:26:31 +01:00
|
|
|
$this->resetState($node->name);
|
|
|
|
} elseif ($node instanceof Stmt\Use_) {
|
|
|
|
foreach ($node->uses as $use) {
|
2015-06-12 23:05:28 +02:00
|
|
|
$this->addAlias($use, $node->type, null);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof Stmt\GroupUse) {
|
|
|
|
foreach ($node->uses as $use) {
|
|
|
|
$this->addAlias($use, $node->type, $node->prefix);
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Class_) {
|
2011-09-28 20:14:27 +02:00
|
|
|
if (null !== $node->extends) {
|
|
|
|
$node->extends = $this->resolveClassName($node->extends);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->implements as &$interface) {
|
|
|
|
$interface = $this->resolveClassName($interface);
|
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2015-04-26 23:04:31 +02:00
|
|
|
if (null !== $node->name) {
|
|
|
|
$this->addNamespacedName($node);
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Interface_) {
|
2011-09-28 20:14:27 +02:00
|
|
|
foreach ($node->extends as &$interface) {
|
|
|
|
$interface = $this->resolveClassName($interface);
|
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2011-11-12 18:29:50 +01:00
|
|
|
$this->addNamespacedName($node);
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Trait_) {
|
2011-11-12 13:24:59 +01:00
|
|
|
$this->addNamespacedName($node);
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Function_) {
|
2011-11-12 13:24:59 +01:00
|
|
|
$this->addNamespacedName($node);
|
2015-03-25 20:57:39 +01:00
|
|
|
$this->resolveSignature($node);
|
2015-03-12 13:17:31 +01:00
|
|
|
} elseif ($node instanceof Stmt\ClassMethod
|
|
|
|
|| $node instanceof Expr\Closure
|
|
|
|
) {
|
2015-03-25 20:57:39 +01:00
|
|
|
$this->resolveSignature($node);
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Const_) {
|
2011-11-12 13:24:59 +01:00
|
|
|
foreach ($node->consts as $const) {
|
|
|
|
$this->addNamespacedName($const);
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Expr\StaticCall
|
|
|
|
|| $node instanceof Expr\StaticPropertyFetch
|
|
|
|
|| $node instanceof Expr\ClassConstFetch
|
|
|
|
|| $node instanceof Expr\New_
|
|
|
|
|| $node instanceof Expr\Instanceof_
|
2011-09-28 20:14:27 +02:00
|
|
|
) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node->class instanceof Name) {
|
2011-11-06 17:16:40 +01:00
|
|
|
$node->class = $this->resolveClassName($node->class);
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\Catch_) {
|
2012-05-04 06:52:39 +02:00
|
|
|
$node->type = $this->resolveClassName($node->type);
|
2014-03-26 23:26:31 +01:00
|
|
|
} elseif ($node instanceof Expr\FuncCall) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node->name instanceof Name) {
|
2014-03-26 23:26:31 +01:00
|
|
|
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_FUNCTION);
|
2011-11-06 17:16:40 +01:00
|
|
|
}
|
2014-03-26 23:26:31 +01:00
|
|
|
} elseif ($node instanceof Expr\ConstFetch) {
|
|
|
|
$node->name = $this->resolveOtherName($node->name, Stmt\Use_::TYPE_CONSTANT);
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Stmt\TraitUse) {
|
2011-12-10 12:11:53 +01:00
|
|
|
foreach ($node->traits as &$trait) {
|
|
|
|
$trait = $this->resolveClassName($trait);
|
|
|
|
}
|
2014-11-03 16:06:43 +01:00
|
|
|
|
2015-03-25 20:57:39 +01:00
|
|
|
foreach ($node->adaptations as $adaptation) {
|
2014-11-03 16:06:43 +01:00
|
|
|
if (null !== $adaptation->trait) {
|
|
|
|
$adaptation->trait = $this->resolveClassName($adaptation->trait);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) {
|
|
|
|
foreach ($adaptation->insteadof as &$insteadof) {
|
|
|
|
$insteadof = $this->resolveClassName($insteadof);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 23:26:31 +01:00
|
|
|
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(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-12 23:05:28 +02:00
|
|
|
protected function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) {
|
2015-06-13 11:27:38 +02:00
|
|
|
// Add prefix for group uses
|
|
|
|
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
|
|
|
|
// Type is determined either by individual element or whole use declaration
|
|
|
|
$type |= $use->type;
|
|
|
|
|
2014-03-26 23:26:31 +01:00
|
|
|
// 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',
|
2015-06-12 23:05:28 +02:00
|
|
|
$typeStringMap[$type], $name, $use->alias
|
2014-03-26 23:26:31 +01:00
|
|
|
),
|
|
|
|
$use->getLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-12 23:05:28 +02:00
|
|
|
$this->aliases[$type][$aliasName] = $name;
|
2014-03-26 23:26:31 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:57:39 +01:00
|
|
|
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */
|
|
|
|
private function resolveSignature($node) {
|
|
|
|
foreach ($node->params as $param) {
|
|
|
|
if ($param->type instanceof Name) {
|
|
|
|
$param->type = $this->resolveClassName($param->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($node->returnType instanceof Name) {
|
|
|
|
$node->returnType = $this->resolveClassName($node->returnType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function resolveClassName(Name $name) {
|
2011-09-28 20:14:27 +02:00
|
|
|
// don't resolve special class names
|
2015-03-23 11:43:22 +01:00
|
|
|
if (in_array(strtolower($name->toString()), array('self', 'parent', 'static'))) {
|
2014-08-11 22:04:52 +02:00
|
|
|
if (!$name->isUnqualified()) {
|
|
|
|
throw new Error(
|
|
|
|
sprintf("'\\%s' is an invalid class name", $name->toString()),
|
|
|
|
$name->getLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:14:27 +02:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fully qualified names are already resolved
|
|
|
|
if ($name->isFullyQualified()) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2013-11-13 11:49:45 +01:00
|
|
|
$aliasName = strtolower($name->getFirst());
|
2014-03-26 23:26:31 +01:00
|
|
|
if (!$name->isRelative() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
|
|
|
|
// resolve aliases (for non-relative names)
|
2015-07-12 23:55:57 +02:00
|
|
|
$alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName];
|
|
|
|
return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $this->namespace) {
|
2014-03-26 23:26:31 +01:00
|
|
|
// if no alias exists prepend current namespace
|
2015-07-12 23:55:57 +02:00
|
|
|
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 23:55:57 +02:00
|
|
|
return new FullyQualified($name->parts, $name->getAttributes());
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2014-03-26 23:26:31 +01:00
|
|
|
protected function resolveOtherName(Name $name, $type) {
|
|
|
|
// fully qualified names are already resolved
|
|
|
|
if ($name->isFullyQualified()) {
|
2011-09-28 20:14:27 +02:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve aliases for qualified names
|
2013-11-13 11:49:45 +01:00
|
|
|
$aliasName = strtolower($name->getFirst());
|
2014-03-26 23:26:31 +01:00
|
|
|
if ($name->isQualified() && isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName])) {
|
2015-07-12 23:55:57 +02:00
|
|
|
$alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$aliasName];
|
|
|
|
return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name->isUnqualified()) {
|
2014-03-26 23:26:31 +01:00
|
|
|
if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
|
|
|
// constant aliases are case-sensitive, function aliases case-insensitive
|
|
|
|
$aliasName = $name->getFirst();
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:55:57 +02:00
|
|
|
if (!isset($this->aliases[$type][$aliasName])) {
|
2014-03-26 23:26:31 +01:00
|
|
|
// unqualified, unaliased names cannot be resolved at compile-time
|
|
|
|
return $name;
|
|
|
|
}
|
2015-07-12 23:55:57 +02:00
|
|
|
|
|
|
|
// resolve unqualified aliases
|
|
|
|
return new FullyQualified($this->aliases[$type][$aliasName], $name->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $this->namespace) {
|
2014-03-26 23:26:31 +01:00
|
|
|
// if no alias exists prepend current namespace
|
2015-07-12 23:55:57 +02:00
|
|
|
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 23:55:57 +02:00
|
|
|
return new FullyQualified($name->parts, $name->getAttributes());
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2011-11-12 13:24:59 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function addNamespacedName(Node $node) {
|
2011-11-12 13:24:59 +01:00
|
|
|
if (null !== $this->namespace) {
|
2015-07-12 23:55:57 +02:00
|
|
|
$node->namespacedName = Name::concat($this->namespace, $node->name);
|
2011-11-12 13:24:59 +01:00
|
|
|
} else {
|
2015-07-12 23:55:57 +02:00
|
|
|
$node->namespacedName = new Name($node->name);
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 11:49:45 +01:00
|
|
|
}
|