1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Analyse SimpleNameResolver too

This commit is contained in:
Matthew Brown 2018-07-22 18:00:08 -04:00
parent 4de33e509b
commit b6372f8342
3 changed files with 26 additions and 18 deletions

View File

@ -7,6 +7,9 @@
> >
<projectFiles> <projectFiles>
<directory name="src" /> <directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles> </projectFiles>
<issueHandlers> <issueHandlers>

View File

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

View File

@ -4,6 +4,7 @@
namespace Psalm\Visitor; namespace Psalm\Visitor;
use PhpParser;
use PhpParser\Error; use PhpParser\Error;
use PhpParser\ErrorHandler; use PhpParser\ErrorHandler;
use PhpParser\NameContext; use PhpParser\NameContext;
@ -32,19 +33,10 @@ class SimpleNameResolver extends NodeVisitorAbstract
* @param ErrorHandler|null $errorHandler Error handler * @param ErrorHandler|null $errorHandler Error handler
* @param array $options Options * @param array $options Options
*/ */
public function __construct(ErrorHandler $errorHandler = null, array $options = []) { public function __construct(ErrorHandler $errorHandler = null) {
$this->nameContext = new NameContext($errorHandler ?: new ErrorHandler\Throwing); $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) { public function beforeTraverse(array $nodes) {
$this->nameContext->startNamespace(); $this->nameContext->startNamespace();
return null; return null;
@ -90,6 +82,7 @@ class SimpleNameResolver extends NodeVisitorAbstract
} }
foreach ($node->adaptations as $adaptation) { foreach ($node->adaptations as $adaptation) {
/** @psalm-suppress RedundantConditionGivenDocblockType */
if (null !== $adaptation->trait) { if (null !== $adaptation->trait) {
$adaptation->trait = $this->resolveClassName($adaptation->trait); $adaptation->trait = $this->resolveClassName($adaptation->trait);
} }
@ -105,8 +98,13 @@ class SimpleNameResolver extends NodeVisitorAbstract
return null; return null;
} }
/**
* @param int $type
* @return void
*/
private function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) { private function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) {
// Add prefix for group uses // Add prefix for group uses
/** @var Name $name */
$name = $prefix ? Name::concat($prefix, $use->name) : $use->name; $name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
// Type is determined either by individual element or whole use declaration // Type is determined either by individual element or whole use declaration
$type |= $use->type; $type |= $use->type;
@ -116,7 +114,10 @@ class SimpleNameResolver extends NodeVisitorAbstract
); );
} }
/** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node */ /**
* @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure $node
* @return void
*/
private function resolveSignature($node) { private function resolveSignature($node) {
foreach ($node->params as $param) { foreach ($node->params as $param) {
$param->type = $this->resolveType($param->type); $param->type = $this->resolveType($param->type);
@ -124,8 +125,15 @@ class SimpleNameResolver extends NodeVisitorAbstract
$node->returnType = $this->resolveType($node->returnType); $node->returnType = $this->resolveType($node->returnType);
} }
/**
* @param PhpParser\Node|string|null $node
* @return null|PhpParser\Node\Identifier|PhpParser\Node\Name|PhpParser\Node\NullableType
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
private function resolveType($node) { private function resolveType($node) {
if ($node instanceof Node\NullableType) { if ($node instanceof Node\NullableType) {
/** @psalm-suppress PossiblyInvalidPropertyAssignmentValue */
$node->type = $this->resolveType($node->type); $node->type = $this->resolveType($node->type);
return $node; return $node;
} }
@ -143,7 +151,7 @@ class SimpleNameResolver extends NodeVisitorAbstract
* *
* @return Name Resolved name, or original name with attribute * @return Name Resolved name, or original name with attribute
*/ */
protected function resolveName(Name $name, $type) : Name { protected function resolveName(Name $name, $type) {
$resolvedName = $this->nameContext->getResolvedName($name, $type); $resolvedName = $this->nameContext->getResolvedName($name, $type);
if (null !== $resolvedName) { if (null !== $resolvedName) {
$name->setAttribute('resolvedName', $resolvedName->toString()); $name->setAttribute('resolvedName', $resolvedName->toString());
@ -151,12 +159,10 @@ class SimpleNameResolver extends NodeVisitorAbstract
return $name; return $name;
} }
/**
* @return Name
*/
protected function resolveClassName(Name $name) { protected function resolveClassName(Name $name) {
return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL); return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
} }
protected function addNamespacedName(Node $node) {
$node->namespacedName = Name::concat(
$this->nameContext->getNamespace(), (string) $node->name);
}
} }