2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2011-09-28 20:14:27 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\ErrorHandler;
|
2017-04-27 21:04:51 +02:00
|
|
|
use PhpParser\NameContext;
|
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
|
|
|
{
|
2017-04-27 21:04:51 +02:00
|
|
|
/** @var NameContext Naming context */
|
|
|
|
protected $nameContext;
|
2016-10-09 13:15:24 +02:00
|
|
|
|
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.
|
2017-10-15 22:20:52 +02:00
|
|
|
* * replaceNodes (default true): Resolved names are replaced in-place. Otherwise, a
|
2016-12-26 21:34:25 +01:00
|
|
|
* 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 = []) {
|
2017-04-27 21:04:51 +02:00
|
|
|
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing);
|
2017-04-24 22:32:40 +02:00
|
|
|
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
|
|
|
|
$this->replaceNodes = $options['replaceNodes'] ?? true;
|
2016-10-09 13:15:24 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 21:04:51 +02:00
|
|
|
/**
|
|
|
|
* Get name resolution context.
|
|
|
|
*
|
|
|
|
* @return NameContext
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function getNameContext() : NameContext {
|
2017-04-27 21:04:51 +02:00
|
|
|
return $this->nameContext;
|
|
|
|
}
|
|
|
|
|
2011-09-28 20:14:27 +02:00
|
|
|
public function beforeTraverse(array $nodes) {
|
2017-04-27 21:04:51 +02:00
|
|
|
$this->nameContext->startNamespace();
|
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_) {
|
2017-04-27 21:04:51 +02:00
|
|
|
$this->nameContext->startNamespace($node->name);
|
2014-03-26 23:26:31 +01:00
|
|
|
} 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);
|
2019-01-05 12:06:18 +01:00
|
|
|
} elseif ($node instanceof Stmt\Property) {
|
|
|
|
if (null !== $node->type) {
|
|
|
|
$node->type = $this->resolveType($node->type);
|
|
|
|
}
|
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) {
|
2017-04-28 17:15:24 +02:00
|
|
|
$node->name = $this->resolveName($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) {
|
2017-04-28 17:15:24 +02:00
|
|
|
$node->name = $this->resolveName($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
|
|
|
}
|
2016-12-10 21:35:32 +01:00
|
|
|
|
|
|
|
return null;
|
2011-09-28 20:14:27 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 21:04:51 +02:00
|
|
|
private 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;
|
|
|
|
|
2017-04-28 21:05:01 +02:00
|
|
|
$this->nameContext->addAlias(
|
|
|
|
$name, (string) $use->getAlias(), $type, $use->getAttributes()
|
|
|
|
);
|
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) {
|
2017-02-26 22:50:31 +01:00
|
|
|
$param->type = $this->resolveType($param->type);
|
|
|
|
}
|
|
|
|
$node->returnType = $this->resolveType($node->returnType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveType($node) {
|
|
|
|
if ($node instanceof Node\NullableType) {
|
|
|
|
$node->type = $this->resolveType($node->type);
|
|
|
|
return $node;
|
2015-03-25 20:57:39 +01:00
|
|
|
}
|
2017-02-26 22:50:31 +01:00
|
|
|
if ($node instanceof Name) {
|
|
|
|
return $this->resolveClassName($node);
|
2015-03-25 20:57:39 +01:00
|
|
|
}
|
2017-02-26 22:50:31 +01:00
|
|
|
return $node;
|
2015-03-25 20:57:39 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
2017-04-28 17:15:24 +02:00
|
|
|
* Resolve name, according to name resolver options.
|
2017-01-26 00:16:54 +01:00
|
|
|
*
|
|
|
|
* @param Name $name Function or constant name to resolve
|
2017-04-28 17:15:24 +02:00
|
|
|
* @param int $type One of Stmt\Use_::TYPE_*
|
2017-01-26 00:16:54 +01:00
|
|
|
*
|
|
|
|
* @return Name Resolved name, or original name with attribute
|
2017-01-24 08:38:55 +01:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
protected function resolveName(Name $name, int $type) : Name {
|
2016-12-26 21:34:25 +01:00
|
|
|
if (!$this->replaceNodes) {
|
2017-04-28 17:15:24 +02:00
|
|
|
$resolvedName = $this->nameContext->getResolvedName($name, $type);
|
2016-12-26 21:34:25 +01:00
|
|
|
if (null !== $resolvedName) {
|
|
|
|
$name->setAttribute('resolvedName', $resolvedName);
|
|
|
|
} else {
|
2017-04-27 21:04:51 +02:00
|
|
|
$name->setAttribute('namespacedName', FullyQualified::concat(
|
|
|
|
$this->nameContext->getNamespace(), $name, $name->getAttributes()));
|
2016-12-26 21:34:25 +01:00
|
|
|
}
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->preserveOriginalNames) {
|
|
|
|
// Save the original name
|
|
|
|
$originalName = $name;
|
|
|
|
$name = clone $originalName;
|
|
|
|
$name->setAttribute('originalName', $originalName);
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:15:24 +02:00
|
|
|
$resolvedName = $this->nameContext->getResolvedName($name, $type);
|
2016-12-26 21:34:25 +01:00
|
|
|
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
|
2017-04-27 21:04:51 +02:00
|
|
|
$name->setAttribute('namespacedName', FullyQualified::concat(
|
|
|
|
$this->nameContext->getNamespace(), $name, $name->getAttributes()));
|
2016-12-26 21:34:25 +01:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:15:24 +02:00
|
|
|
protected function resolveClassName(Name $name) {
|
|
|
|
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function addNamespacedName(Node $node) {
|
2017-04-27 21:04:51 +02:00
|
|
|
$node->namespacedName = Name::concat(
|
|
|
|
$this->nameContext->getNamespace(), (string) $node->name);
|
2011-11-12 13:24:59 +01:00
|
|
|
}
|
2013-11-13 11:49:45 +01:00
|
|
|
}
|