Skip to content

Commit

Permalink
update NameResolver to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleorselli committed Feb 12, 2025
1 parent c6eff64 commit 7143749
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ psalm: ## it launches psalm
build: ## it launches all the build
composer install
PHP_CS_FIXER_IGNORE_ENV=1 bin/php-cs-fixer fix -v
bin/psalm
bin/psalm.phar --no-cache
bin/phpunit

sfbuild: ## it launches all the build
Expand Down
45 changes: 31 additions & 14 deletions src/Analyzer/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
class NameResolver extends NodeVisitorAbstract
{
/** @var NameContext Naming context */
protected $nameContext;
protected NameContext $nameContext;

/** @var bool Whether to preserve original names */
protected $preserveOriginalNames;
protected bool $preserveOriginalNames;

/** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
protected $replaceNodes;
protected bool $replaceNodes;

/** @var bool Whether to parse DocBlock Custom Annotations */
protected $parseCustomAnnotations;
Expand All @@ -55,8 +55,8 @@ class NameResolver extends NodeVisitorAbstract
* namespacedName attribute, as usual.)
* * parseCustomAnnotations (default true): Whether to parse DocBlock Custom Annotations.
*
* @param ErrorHandler|null $errorHandler Error handler
* @param array $options Options
* @param ErrorHandler|null $errorHandler Error handler
* @param array{preserveOriginalNames?: bool, replaceNodes?: bool, parseCustomAnnotations?: bool} $options Options
*/
public function __construct(?ErrorHandler $errorHandler = null, array $options = [])
{
Expand All @@ -79,7 +79,7 @@ public function getNameContext(): NameContext
return $this->nameContext;
}

public function beforeTraverse(array $nodes)
public function beforeTraverse(array $nodes): ?array
{
$this->nameContext->startNamespace();

Expand Down Expand Up @@ -110,6 +110,8 @@ public function enterNode(Node $node)
$this->resolveAttrGroups($node);
if (null !== $node->name) {
$this->addNamespacedName($node);
} else {
$node->namespacedName = null;
}
} elseif ($node instanceof Stmt\Interface_) {
foreach ($node->extends as &$interface) {
Expand All @@ -134,7 +136,8 @@ public function enterNode(Node $node)
$this->resolveSignature($node);
$this->resolveAttrGroups($node);
$this->addNamespacedName($node);
} elseif ($node instanceof Stmt\ClassMethod
} elseif (
$node instanceof Stmt\ClassMethod
|| $node instanceof Expr\Closure
|| $node instanceof Expr\ArrowFunction
) {
Expand Down Expand Up @@ -183,15 +186,25 @@ public function enterNode(Node $node)
}
}
}
} elseif ($node instanceof Node\PropertyHook) {
foreach ($node->params as $param) {
$param->type = $this->resolveType($param->type);
$this->resolveAttrGroups($param);
}
$this->resolveAttrGroups($node);
} elseif ($node instanceof Stmt\Const_) {
foreach ($node->consts as $const) {
$this->addNamespacedName($const);
}
} elseif ($node instanceof Stmt\ClassConst) {
if (null !== $node->type) {
$node->type = $this->resolveType($node->type);
}
$this->resolveAttrGroups($node);
} elseif ($node instanceof Stmt\EnumCase) {
$this->resolveAttrGroups($node);
} elseif ($node instanceof Expr\StaticCall
} elseif (
$node instanceof Expr\StaticCall
|| $node instanceof Expr\StaticPropertyFetch
|| $node instanceof Expr\ClassConstFetch
|| $node instanceof Expr\New_
Expand Down Expand Up @@ -234,8 +247,8 @@ public function enterNode(Node $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_*
* @param Name $name Function or constant name to resolve
* @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
*
* @return Name Resolved name, or original name with attribute
*/
Expand Down Expand Up @@ -307,10 +320,10 @@ protected function resolveAttrGroups(Node $node): void
}
}

private function addAlias(Stmt\UseUse $use, int $type, ?Name $prefix = null): void
/** @param Stmt\Use_::TYPE_* $type */
private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void
{
// 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;
Expand Down Expand Up @@ -365,9 +378,13 @@ private function resolveSignature($node): void
* @psalm-suppress PossiblyNullArgument
* @psalm-suppress MissingReturnType
*
* @param mixed $node
* @template T of Node\Identifier|Name|Node\ComplexType|null
*
* @param T $node
*
* @return T
*/
private function resolveType($node)
private function resolveType(?Node $node): ?Node
{
if ($node instanceof Name) {
return $this->resolveClassName($node);

Check failure on line 390 in src/Analyzer/NameResolver.php

View workflow job for this annotation

GitHub Actions / build (7.4, pcov)

InvalidReturnStatement

src/Analyzer/NameResolver.php:390:20: InvalidReturnStatement: The inferred type 'PhpParser\Node\Name' does not match the declared return type 'T:fn-arkitect\analyzer\nameresolver::resolvetype as PhpParser\Node\ComplexType|PhpParser\Node\Identifier|PhpParser\Node\Name|null' for Arkitect\Analyzer\NameResolver::resolveType (see https://psalm.dev/128)

Check failure on line 390 in src/Analyzer/NameResolver.php

View workflow job for this annotation

GitHub Actions / build (7.4, pcov)

InvalidReturnStatement

src/Analyzer/NameResolver.php:390:20: InvalidReturnStatement: The inferred type 'PhpParser\Node\Name' does not match the declared return type 'T:fn-arkitect\analyzer\nameresolver::resolvetype as PhpParser\Node\ComplexType|PhpParser\Node\Identifier|PhpParser\Node\Name|null' for Arkitect\Analyzer\NameResolver::resolveType (see https://psalm.dev/128)
Expand Down

0 comments on commit 7143749

Please sign in to comment.