2018-07-13 03:25:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// based on PhpParser's builtin one
|
2020-03-15 04:54:42 +01:00
|
|
|
namespace Psalm\Internal\PhpVisitor;
|
2018-07-13 03:25:06 +02:00
|
|
|
|
2018-07-23 00:00:08 +02:00
|
|
|
use PhpParser;
|
2018-07-13 03:25:06 +02:00
|
|
|
use PhpParser\ErrorHandler;
|
|
|
|
use PhpParser\NameContext;
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-07-13 03:25:06 +02:00
|
|
|
class SimpleNameResolver extends NodeVisitorAbstract
|
|
|
|
{
|
|
|
|
/** @var NameContext Naming context */
|
2018-10-26 06:59:14 +02:00
|
|
|
private $nameContext;
|
|
|
|
|
|
|
|
/** @var int|null */
|
|
|
|
private $start_change;
|
|
|
|
|
|
|
|
/** @var int|null */
|
|
|
|
private $end_change;
|
2018-07-13 03:25:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a name resolution visitor.
|
|
|
|
*
|
|
|
|
* Options:
|
|
|
|
* * preserveOriginalNames (default false): An "originalName" attribute will be added to
|
|
|
|
* all name nodes that underwent resolution.
|
|
|
|
* * replaceNodes (default true): Resolved names are replaced in-place. Otherwise, a
|
|
|
|
* resolvedName attribute is added. (Names that cannot be statically resolved receive a
|
|
|
|
* namespacedName attribute, as usual.)
|
|
|
|
*
|
2018-10-26 06:59:14 +02:00
|
|
|
* @param ErrorHandler $errorHandler Error handler
|
2021-02-28 07:36:06 +01:00
|
|
|
* @param array<int, array{int, int, int, int}> $offset_map
|
2018-07-13 03:25:06 +02:00
|
|
|
*/
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(ErrorHandler $errorHandler, ?array $offset_map = null)
|
2018-10-26 06:59:14 +02:00
|
|
|
{
|
|
|
|
if ($offset_map) {
|
2020-09-02 06:17:41 +02:00
|
|
|
foreach ($offset_map as [, , $b_s, $b_e]) {
|
2018-10-26 06:59:14 +02:00
|
|
|
if ($this->start_change === null) {
|
|
|
|
$this->start_change = $b_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->end_change = $b_e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nameContext = new NameContext($errorHandler);
|
2018-07-13 03:25:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function beforeTraverse(array $nodes): ?array
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
$this->nameContext->startNamespace();
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-07-13 03:25:06 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function enterNode(Node $node): ?int
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
if ($node instanceof Stmt\Namespace_) {
|
|
|
|
$this->nameContext->startNamespace($node->name);
|
|
|
|
} elseif ($node instanceof Stmt\Use_) {
|
|
|
|
foreach ($node->uses as $use) {
|
|
|
|
$this->addAlias($use, $node->type, null);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof Stmt\GroupUse) {
|
|
|
|
foreach ($node->uses as $use) {
|
|
|
|
$this->addAlias($use, $node->type, $node->prefix);
|
|
|
|
}
|
2018-10-26 06:59:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 21:04:37 +01:00
|
|
|
if ($node instanceof Stmt\ClassMethod
|
|
|
|
&& $this->start_change
|
|
|
|
&& $this->end_change
|
|
|
|
) {
|
2019-10-01 21:44:43 +02:00
|
|
|
/** @var array{startFilePos: int, endFilePos: int} */
|
2018-10-26 06:59:14 +02:00
|
|
|
$attrs = $node->getAttributes();
|
|
|
|
|
2018-11-01 21:04:37 +01:00
|
|
|
if ($cs = $node->getComments()) {
|
2020-09-20 14:56:49 +02:00
|
|
|
$attrs['startFilePos'] = $cs[0]->getStartFilePos();
|
2018-11-01 21:04:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 06:59:14 +02:00
|
|
|
if ($attrs['endFilePos'] < $this->start_change
|
|
|
|
|| $attrs['startFilePos'] > $this->end_change
|
|
|
|
) {
|
|
|
|
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof Stmt\ClassMethod
|
2018-07-13 03:25:06 +02:00
|
|
|
|| $node instanceof Expr\Closure
|
|
|
|
) {
|
|
|
|
$this->resolveSignature($node);
|
|
|
|
} elseif ($node instanceof Expr\StaticCall
|
|
|
|
|| $node instanceof Expr\StaticPropertyFetch
|
|
|
|
|| $node instanceof Expr\ClassConstFetch
|
|
|
|
|| $node instanceof Expr\New_
|
|
|
|
|| $node instanceof Expr\Instanceof_
|
|
|
|
) {
|
|
|
|
if ($node->class instanceof Name) {
|
|
|
|
$node->class = $this->resolveClassName($node->class);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof Stmt\Catch_) {
|
|
|
|
foreach ($node->types as &$type) {
|
|
|
|
$type = $this->resolveClassName($type);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof Expr\FuncCall) {
|
|
|
|
if ($node->name instanceof Name) {
|
|
|
|
$node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION);
|
|
|
|
}
|
|
|
|
} elseif ($node instanceof Expr\ConstFetch) {
|
|
|
|
$node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT);
|
2020-03-09 21:41:40 +01:00
|
|
|
} elseif ($node instanceof Stmt\Trait_) {
|
|
|
|
$this->resolveTrait($node);
|
2018-07-13 03:25:06 +02:00
|
|
|
} elseif ($node instanceof Stmt\TraitUse) {
|
|
|
|
foreach ($node->traits as &$trait) {
|
|
|
|
$trait = $this->resolveClassName($trait);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->adaptations as $adaptation) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
private function addAlias(Stmt\UseUse $use, int $type, ?Name $prefix = null): void
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
// Add prefix for group uses
|
2018-07-23 00:00:08 +02:00
|
|
|
/** @var Name $name */
|
2018-07-13 03:25:06 +02:00
|
|
|
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
|
|
|
|
// Type is determined either by individual element or whole use declaration
|
|
|
|
$type |= $use->type;
|
|
|
|
|
|
|
|
$this->nameContext->addAlias(
|
2019-07-05 22:24:00 +02:00
|
|
|
$name,
|
|
|
|
(string) $use->getAlias(),
|
|
|
|
$type,
|
|
|
|
$use->getAttributes()
|
2018-07-13 03:25:06 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-23 00:00:08 +02:00
|
|
|
/**
|
|
|
|
* @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node
|
|
|
|
*/
|
2020-10-12 21:46:47 +02:00
|
|
|
private function resolveSignature(PhpParser\NodeAbstract $node): void
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
foreach ($node->params as $param) {
|
|
|
|
$param->type = $this->resolveType($param->type);
|
|
|
|
}
|
|
|
|
$node->returnType = $this->resolveType($node->returnType);
|
|
|
|
}
|
|
|
|
|
2018-07-23 00:00:08 +02:00
|
|
|
/**
|
|
|
|
* @param PhpParser\Node|string|null $node
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-07-23 00:00:08 +02:00
|
|
|
* @return null|PhpParser\Node\Identifier|PhpParser\Node\Name|PhpParser\Node\NullableType
|
2019-01-05 06:15:53 +01:00
|
|
|
* @psalm-suppress InvalidReturnType
|
|
|
|
* @psalm-suppress InvalidReturnStatement
|
2018-07-23 00:00:08 +02:00
|
|
|
*/
|
2020-10-12 21:02:52 +02:00
|
|
|
private function resolveType($node): ?Node
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
if ($node instanceof Node\NullableType) {
|
2018-07-23 00:00:08 +02:00
|
|
|
/** @psalm-suppress PossiblyInvalidPropertyAssignmentValue */
|
2018-07-13 03:25:06 +02:00
|
|
|
$node->type = $this->resolveType($node->type);
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-07-13 03:25:06 +02:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
if ($node instanceof Name) {
|
|
|
|
return $this->resolveClassName($node);
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-07-13 03:25:06 +02:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve name, according to name resolver options.
|
|
|
|
*
|
|
|
|
* @param Name $name Function or constant name to resolve
|
2020-02-07 16:54:57 +01:00
|
|
|
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
|
2018-07-13 03:25:06 +02:00
|
|
|
*
|
|
|
|
* @return Name Resolved name, or original name with attribute
|
|
|
|
*/
|
2020-10-12 21:46:47 +02:00
|
|
|
protected function resolveName(Name $name, int $type): Name
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
$resolvedName = $this->nameContext->getResolvedName($name, $type);
|
|
|
|
if (null !== $resolvedName) {
|
|
|
|
$name->setAttribute('resolvedName', $resolvedName->toString());
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-07-13 03:25:06 +02:00
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
protected function resolveClassName(Name $name): Name
|
2019-07-05 22:24:00 +02:00
|
|
|
{
|
2018-07-13 03:25:06 +02:00
|
|
|
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
|
|
|
|
}
|
2020-03-09 21:41:40 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
protected function resolveTrait(Stmt\Trait_ $node): void
|
2020-03-09 21:41:40 +01:00
|
|
|
{
|
|
|
|
$resolvedName = Name::concat($this->nameContext->getNamespace(), (string) $node->name);
|
|
|
|
|
|
|
|
if (null !== $resolvedName) {
|
|
|
|
$node->setAttribute('resolvedName', $resolvedName->toString());
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 03:25:06 +02:00
|
|
|
}
|