Skip to content

Commit d416b99

Browse files
authored
[Php74] Skip used in compact on ClosureArrowFunctionAnalyzer (#7453)
* [Php74] Skip used in compact on ClosureArrowFunctionAnalyzer * [Php74] Skip used in compact on ClosureArrowFunctionAnalyzer
1 parent 188b90a commit d416b99

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Closure\ClosureToArrowFunctionRector\Fixture;
4+
5+
final class SkipUseCompact
6+
{
7+
public function run(bool $param)
8+
{
9+
$test = 'Super';
10+
11+
$this->myCallback(
12+
function () use ($test) {
13+
return compact('test');
14+
}
15+
);
16+
}
17+
18+
private function myCallback($callback)
19+
{
20+
$callback();
21+
}
22+
}

rules/Php74/NodeAnalyzer/ClosureArrowFunctionAnalyzer.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use PhpParser\Node\ClosureUse;
99
use PhpParser\Node\Expr;
1010
use PhpParser\Node\Expr\Closure;
11+
use PhpParser\Node\Expr\FuncCall;
1112
use PhpParser\Node\Expr\Variable;
1213
use PhpParser\Node\Stmt\Return_;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
1415
use PHPStan\Type\MixedType;
1516
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1617
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
18+
use Rector\NodeAnalyzer\CompactFuncCallAnalyzer;
1719
use Rector\NodeTypeResolver\NodeTypeResolver;
1820
use Rector\PhpParser\Comparing\NodeComparator;
1921
use Rector\PhpParser\Node\BetterNodeFinder;
@@ -26,7 +28,8 @@ public function __construct(
2628
private NodeComparator $nodeComparator,
2729
private ArrayChecker $arrayChecker,
2830
private PhpDocInfoFactory $phpDocInfoFactory,
29-
private NodeTypeResolver $nodeTypeResolver
31+
private NodeTypeResolver $nodeTypeResolver,
32+
private CompactFuncCallAnalyzer $compactFuncCallAnalyzer
3033
) {
3134
}
3235

@@ -51,13 +54,43 @@ public function matchArrowFunctionExpr(Closure $closure): ?Expr
5154
return null;
5255
}
5356

57+
if ($this->shouldSkipForUseVariableUsedByCompact($closure)) {
58+
return null;
59+
}
60+
5461
if ($this->shouldSkipMoreSpecificTypeWithVarDoc($return, $return->expr)) {
5562
return null;
5663
}
5764

5865
return $return->expr;
5966
}
6067

68+
private function shouldSkipForUseVariableUsedByCompact(Closure $closure): bool
69+
{
70+
$variables = array_map(fn (ClosureUse $use): Variable => $use->var, $closure->uses);
71+
72+
if ($variables === []) {
73+
return false;
74+
}
75+
76+
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
77+
$closure,
78+
function (Node $node) use ($variables): bool {
79+
if (! $node instanceof FuncCall) {
80+
return false;
81+
}
82+
83+
foreach ($variables as $variable) {
84+
if ($this->compactFuncCallAnalyzer->isInCompact($node, $variable)) {
85+
return true;
86+
}
87+
}
88+
89+
return false;
90+
}
91+
);
92+
}
93+
6194
/**
6295
* Ensure @var doc usage with more specific type on purpose to be skipped
6396
*/

0 commit comments

Comments
 (0)