Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 23, 2025
1 parent 857bf9f commit 950daed
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

namespace Rector\Php74\NodeAnalyzer;

use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Util\ArrayChecker;
use PHPStan\Analyser\Scope;
use Rector\NodeTypeResolver\NodeTypeResolver;

final readonly class ClosureArrowFunctionAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator,
private ArrayChecker $arrayChecker
private ArrayChecker $arrayChecker,
private PhpDocInfoFactory $phpDocInfoFactory,
private NodeTypeResolver $nodeTypeResolver
) {
}

Expand All @@ -40,6 +49,28 @@ public function matchArrowFunctionExpr(Closure $closure): ?Expr
return null;
}

// ensure @var doc usage
// with more specific type on purpose to be skipped
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($return);

if ($phpDocInfo instanceof PhpDocInfo && $phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
$varType = $phpDocInfo->getVarType();
$variableName = ltrim($phpDocInfo->getVarTagValueNode()->variableName, '$');

$variable = $this->betterNodeFinder->findFirst(
$return->expr,
static fn (Node $node): bool => $node instanceof Variable && $node->name === $variableName
);

if ($variable instanceof Variable) {
$nativeVariableType = $this->nodeTypeResolver->getNativeType($variable);
// not equal with native type means more specific type
if (! $nativeVariableType->equals($varType)) {
return null;
}
}
}

if ($this->shouldSkipForUsedReferencedValue($closure)) {
return null;
}
Expand Down

0 comments on commit 950daed

Please sign in to comment.