Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;

class SkipClosureWithVarDocblockGeneric
{
public function run()
{
$fn = function (object $query) {
/** @var \ArrayObject<int, string> $query */
return $query->count();
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,3 @@ class WithEqualVarDocType
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;

class WithEqualVarDocType
{
public function run()
{
/** @var string $var */
fn(string $var) => $var;
}
}

?>
29 changes: 3 additions & 26 deletions rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeAnalyzer\CompactFuncCallAnalyzer;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Util\ArrayChecker;
Expand All @@ -28,7 +26,6 @@ public function __construct(
private NodeComparator $nodeComparator,
private ArrayChecker $arrayChecker,
private PhpDocInfoFactory $phpDocInfoFactory,
private NodeTypeResolver $nodeTypeResolver,
private CompactFuncCallAnalyzer $compactFuncCallAnalyzer
) {
}
Expand Down Expand Up @@ -92,7 +89,8 @@ function (Node $node) use ($variables): bool {
}

/**
* Ensure @var doc usage with more specific type on purpose to be skipped
* Ensure @var doc usage to be skipped, as arrow functions do not support
* inline @var annotations for type narrowing (e.g. generic types like Builder<Team>)
*/
private function shouldSkipMoreSpecificTypeWithVarDoc(Return_ $return, Expr $expr): bool
{
Expand All @@ -103,29 +101,8 @@ private function shouldSkipMoreSpecificTypeWithVarDoc(Return_ $return, Expr $exp
}

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return false;
}

$varType = $phpDocInfo->getVarType();
if ($varType instanceof MixedType) {
return false;
}

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

if (! $variable instanceof Variable) {
return false;
}

$nativeVariableType = $this->nodeTypeResolver->getNativeType($variable);

// not equal with native type means more specific type
return ! $nativeVariableType->equals($varType);
return $varTagValueNode instanceof VarTagValueNode;
}

private function shouldSkipForUsedReferencedValue(Closure $closure): bool
Expand Down