From b6372f8342f089e269096445d85d448963789c2a Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 22 Jul 2018 18:00:08 -0400 Subject: [PATCH] Analyse SimpleNameResolver too --- assets/config_levels/1.xml | 3 ++ psalm.xml.dist | 1 - src/Psalm/Visitor/SimpleNameResolver.php | 40 ++++++++++++++---------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/assets/config_levels/1.xml b/assets/config_levels/1.xml index 117bde9bd..6c93a94ae 100644 --- a/assets/config_levels/1.xml +++ b/assets/config_levels/1.xml @@ -7,6 +7,9 @@ > + + + diff --git a/psalm.xml.dist b/psalm.xml.dist index 489a3d3a0..4fe7b5f14 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -19,7 +19,6 @@ - diff --git a/src/Psalm/Visitor/SimpleNameResolver.php b/src/Psalm/Visitor/SimpleNameResolver.php index eec65ef6d..a535e9d6e 100644 --- a/src/Psalm/Visitor/SimpleNameResolver.php +++ b/src/Psalm/Visitor/SimpleNameResolver.php @@ -4,6 +4,7 @@ namespace Psalm\Visitor; +use PhpParser; use PhpParser\Error; use PhpParser\ErrorHandler; use PhpParser\NameContext; @@ -32,19 +33,10 @@ class SimpleNameResolver extends NodeVisitorAbstract * @param ErrorHandler|null $errorHandler Error handler * @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); } - /** - * Get name resolution context. - * - * @return NameContext - */ - public function getNameContext() : NameContext { - return $this->nameContext; - } - public function beforeTraverse(array $nodes) { $this->nameContext->startNamespace(); return null; @@ -90,6 +82,7 @@ class SimpleNameResolver extends NodeVisitorAbstract } foreach ($node->adaptations as $adaptation) { + /** @psalm-suppress RedundantConditionGivenDocblockType */ if (null !== $adaptation->trait) { $adaptation->trait = $this->resolveClassName($adaptation->trait); } @@ -105,8 +98,13 @@ class SimpleNameResolver extends NodeVisitorAbstract return null; } + /** + * @param int $type + * @return void + */ private function addAlias(Stmt\UseUse $use, $type, Name $prefix = null) { // Add prefix for group uses + /** @var Name $name */ $name = $prefix ? Name::concat($prefix, $use->name) : $use->name; // Type is determined either by individual element or whole use declaration $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) { foreach ($node->params as $param) { $param->type = $this->resolveType($param->type); @@ -124,8 +125,15 @@ class SimpleNameResolver extends NodeVisitorAbstract $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) { if ($node instanceof Node\NullableType) { + /** @psalm-suppress PossiblyInvalidPropertyAssignmentValue */ $node->type = $this->resolveType($node->type); return $node; } @@ -143,7 +151,7 @@ class SimpleNameResolver extends NodeVisitorAbstract * * @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); if (null !== $resolvedName) { $name->setAttribute('resolvedName', $resolvedName->toString()); @@ -151,12 +159,10 @@ class SimpleNameResolver extends NodeVisitorAbstract return $name; } + /** + * @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); - } }