2011-09-28 20:14:27 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
|
|
|
use PhpParser\Error;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\ErrorHandler;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2014-02-06 14:44:16 +01:00
|
|
|
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\Stmt;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\NodeVisitorAbstract;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
/** @var ErrorHandler Error handler */
|
|
|
|
protected $errorHandler;
|
|
|
|
|
2016-12-17 11:13:43 +01:00
|
|
|
/** @var bool Whether to preserve original names */
|
|
|
|
protected $preserveOriginalNames;
|
|
|
|
|
2016-12-26 21:34:25 +01:00
|
|
|
/** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
|
|
|
|
protected $replaceNodes;
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
/**
|
|
|
|
* Constructs a name resolution visitor.
|
|
|
|
*
|
2016-12-26 21:34:25 +01:00
|
|
|
* Options:
|
|
|
|
* * preserveOriginalNames (default false): An "originalName" attribute will be added to
|
|
|
|
* all name nodes that underwent resolution.
|
|
|
|
* * replaceNodes (default true): Resolved names are not replaced in-place. Instead a
|
|
|
|
* resolvedName attribute is added. (Names that cannot be statically resolved receive a
|
|
|
|
* namespacedName attribute, as usual.)
|
2016-12-17 11:13:43 +01:00
|
|
|
*
|
2016-10-09 13:15:24 +02:00
|
|
|
* @param ErrorHandler|null $errorHandler Error handler
|
2016-12-17 11:13:43 +01:00
|
|
|
* @param array $options Options
|
2016-10-09 13:15:24 +02:00
|
|
|
*/
|
2016-12-17 11:13:43 +01:00
|
|
|
public function __construct(ErrorHandler $errorHandler = null, array $options = []) {
|
2016-10-09 13:15:24 +02:00
|
|
|
$this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;
|
2016-12-17 11:13:43 +01:00
|
|
|
$this->preserveOriginalNames = !empty($options['preserveOriginalNames']);
|
2016-12-26 21:34:25 +01:00
|
|
|
$this->replaceNodes = isset($options['replaceNodes']) ? $options['replaceNodes'] : true;
|
2016-10-09 13:15:24 +02:00
|
|
|
}
|
|
|
|
|
2011-09-28 20:14:27 +02:00
|
|
|
public function beforeTraverse(array $nodes) {
|
2014-03-26 23:26:31 +01:00
|
|
|
$this->resetState();
|
2016-12-10 21:35:32 +01:00
|
|
|
return null;
|
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_) {
|
2016-07-22 15:40:00 +02:00
|
|
|
foreach ($node->types as &$type) {
|
|
|
|
$type = $this->resolveClassName($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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 22:21:34 +01:00
|
|
|
} elseif ($node instanceof Node\NullableType) {
|
|
|
|
if ($node->type instanceof Name) {
|
|
|
|
$node->type = $this->resolveClassName($node->type);
|
|
|
|
}
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
2016-12-10 21:35:32 +01:00
|
|
|
|
|
|
|
return null;
|
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 ',
|
|
|
|
);
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
$this->errorHandler->handleError(new Error(
|
2014-03-26 23:26:31 +01:00
|
|
|
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
|
|
|
),
|
2016-10-09 13:15:24 +02:00
|
|
|
$use->getAttributes()
|
|
|
|
));
|
|
|
|
return;
|
2014-03-26 23:26:31 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
|
|
|
* @return null|Name
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function resolveClassName(Name $name) {
|
2016-12-26 21:34:25 +01:00
|
|
|
if (!$this->replaceNodes) {
|
|
|
|
$name->setAttribute('resolvedName', $this->getResolvedClassName($name));
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2016-12-17 11:13:43 +01:00
|
|
|
if ($this->preserveOriginalNames) {
|
|
|
|
// Save the original name
|
|
|
|
$originalName = $name;
|
|
|
|
$name = clone $originalName;
|
|
|
|
$name->setAttribute('originalName', $originalName);
|
|
|
|
}
|
|
|
|
|
2016-12-26 21:34:25 +01:00
|
|
|
return $this->getResolvedClassName($name);
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
|
|
|
* @return Name
|
|
|
|
*/
|
2016-12-26 21:34:25 +01:00
|
|
|
protected function resolveOtherName(Name $name, $type) {
|
|
|
|
if (!$this->replaceNodes) {
|
|
|
|
$resolvedName = $this->getResolvedOtherName($name, $type);
|
|
|
|
if (null !== $resolvedName) {
|
|
|
|
$name->setAttribute('resolvedName', $resolvedName);
|
|
|
|
} else {
|
|
|
|
$name->setAttribute('namespacedName',
|
|
|
|
FullyQualified::concat($this->namespace, $name, $name->getAttributes()));
|
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->preserveOriginalNames) {
|
|
|
|
// Save the original name
|
|
|
|
$originalName = $name;
|
|
|
|
$name = clone $originalName;
|
|
|
|
$name->setAttribute('originalName', $originalName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolvedName = $this->getResolvedOtherName($name, $type);
|
|
|
|
if (null !== $resolvedName) {
|
|
|
|
return $resolvedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unqualified names inside a namespace cannot be resolved at compile-time
|
|
|
|
// add the namespaced version of the name as an attribute
|
|
|
|
$name->setAttribute('namespacedName',
|
|
|
|
FullyQualified::concat($this->namespace, $name, $name->getAttributes()));
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
|
|
|
* @return null|Name
|
|
|
|
*/
|
2016-12-26 21:34:25 +01:00
|
|
|
protected function getResolvedClassName(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()) {
|
2016-10-09 13:15:24 +02:00
|
|
|
$this->errorHandler->handleError(new Error(
|
2014-08-11 22:04:52 +02:00
|
|
|
sprintf("'\\%s' is an invalid class name", $name->toString()),
|
2016-10-09 13:15:24 +02:00
|
|
|
$name->getAttributes()
|
|
|
|
));
|
2014-08-11 22:04:52 +02:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
2016-10-22 17:02:38 +02:00
|
|
|
// if no alias exists prepend current namespace
|
|
|
|
return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
|
|
|
* @return null|Name
|
|
|
|
*/
|
2016-12-26 21:34:25 +01:00
|
|
|
protected function getResolvedOtherName(Name $name, $type) {
|
2014-03-26 23:26:31 +01:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:43:29 +02:00
|
|
|
if (isset($this->aliases[$type][$aliasName])) {
|
|
|
|
// resolve unqualified aliases
|
|
|
|
return new FullyQualified($this->aliases[$type][$aliasName], $name->getAttributes());
|
2014-03-26 23:26:31 +01:00
|
|
|
}
|
2015-07-12 23:55:57 +02:00
|
|
|
|
2016-10-11 19:43:29 +02:00
|
|
|
if (null === $this->namespace) {
|
|
|
|
// outside of a namespace unaliased unqualified is same as fully qualified
|
2016-10-22 16:41:58 +02:00
|
|
|
return new FullyQualified($name, $name->getAttributes());
|
2016-10-11 19:43:29 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 21:34:25 +01:00
|
|
|
// Cannot resolve statically
|
|
|
|
return null;
|
2015-07-12 23:55:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 17:02:38 +02:00
|
|
|
// if no alias exists prepend current namespace
|
|
|
|
return FullyQualified::concat($this->namespace, $name, $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) {
|
2016-12-26 21:34:25 +01:00
|
|
|
$node->namespacedName = Name::concat($this->namespace, (string) $node->name);
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
2013-11-13 11:49:45 +01:00
|
|
|
}
|