1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Resolve as many names as possible in the parsing stage

This commit is contained in:
Matthew Brown 2018-07-12 21:25:06 -04:00
parent 7713c7a8d6
commit 5ea8b86b7c
6 changed files with 179 additions and 1 deletions

View File

@ -8,6 +8,7 @@
<!-- This is a vendor file that we don't want to bother linting. -->
<exclude-pattern>src/Psalm/CallMap.php</exclude-pattern>
<exclude-pattern>src/Psalm/Visitor/SimpleNameResolver.php</exclude-pattern>
<!-- These are just examples and stub classes/files, so it doesn't really matter if they're PSR-2 compliant. -->
<exclude-pattern>src/Psalm/Stubs/</exclude-pattern>

View File

@ -19,6 +19,7 @@
<file name="src/Psalm/CallMap.php" />
<directory name="src/Psalm/Stubs" />
<directory name="tests/stubs" />
<file name="src/Psalm/Visitor/SimpleNameResolver.php" />
</ignoreFiles>
</projectFiles>

View File

@ -328,6 +328,13 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
*/
public static function getFQCLNFromNameObject(PhpParser\Node\Name $class_name, Aliases $aliases)
{
/** @var string|null */
$resolved_name = $class_name->getAttribute('resolvedName');
if ($resolved_name) {
return $resolved_name;
}
if ($class_name instanceof PhpParser\Node\Name\FullyQualified) {
return implode('\\', $class_name->parts);
}

View File

@ -262,6 +262,6 @@ class ParserCacheProvider
*/
public static function getParserCacheKey($file_name, $use_igbinary)
{
return md5($file_name) . ($use_igbinary ? '-igbinary' : '');
return md5($file_name) . ($use_igbinary ? '-igbinary' : '') . '-r';
}
}

View File

@ -76,6 +76,13 @@ class StatementsProvider
$from_cache = true;
}
$nameResolver = new \Psalm\Visitor\SimpleNameResolver;
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor($nameResolver);
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = $nodeTraverser->traverse($stmts);
$this->cache_provider->saveStatementsToCache($file_cache_key, $file_content_hash, $stmts, $from_cache);
if (!$stmts) {

View File

@ -0,0 +1,162 @@
<?php
// based on PhpParser's builtin one
namespace Psalm\Visitor;
use PhpParser\Error;
use PhpParser\ErrorHandler;
use PhpParser\NameContext;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
class SimpleNameResolver extends NodeVisitorAbstract
{
/** @var NameContext Naming context */
protected $nameContext;
/**
* 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.)
*
* @param ErrorHandler|null $errorHandler Error handler
* @param array $options Options
*/
public function __construct(ErrorHandler $errorHandler = null, array $options = []) {
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing);
}
/**
* Get name resolution context.
*
* @return NameContext
*/
public function getNameContext() : NameContext {
return $this->nameContext;
}
public function beforeTraverse(array $nodes) {
$this->nameContext->startNamespace();
return null;
}
public function enterNode(Node $node) {
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);
}
} elseif ($node instanceof Stmt\ClassMethod
|| $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);
} 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;
}
private function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) {
// 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;
$this->nameContext->addAlias(
$name, (string) $use->getAlias(), $type, $use->getAttributes()
);
}
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */
private function resolveSignature($node) {
foreach ($node->params as $param) {
$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;
}
if ($node instanceof Name) {
return $this->resolveClassName($node);
}
return $node;
}
/**
* Resolve name, according to name resolver options.
*
* @param Name $name Function or constant name to resolve
* @param int $type One of Stmt\Use_::TYPE_*
*
* @return Name Resolved name, or original name with attribute
*/
protected function resolveName(Name $name, int $type) : Name {
$resolvedName = $this->nameContext->getResolvedName($name, $type);
if (null !== $resolvedName) {
$name->setAttribute('resolvedName', $resolvedName->toString());
}
return $name;
}
protected function resolveClassName(Name $name) {
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
}
protected function addNamespacedName(Node $node) {
$node->namespacedName = Name::concat(
$this->nameContext->getNamespace(), (string) $node->name);
}
}