-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathClosureArrowFunctionAnalyzer.php
More file actions
179 lines (148 loc) · 5.21 KB
/
ClosureArrowFunctionAnalyzer.php
File metadata and controls
179 lines (148 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Rector\Php74\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
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\NodeAnalyzer\CompactFuncCallAnalyzer;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Util\ArrayChecker;
final readonly class ClosureArrowFunctionAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator,
private ArrayChecker $arrayChecker,
private PhpDocInfoFactory $phpDocInfoFactory,
private CompactFuncCallAnalyzer $compactFuncCallAnalyzer
) {
}
public function matchArrowFunctionExpr(Closure $closure): ?Expr
{
if (count($closure->stmts) !== 1) {
return null;
}
$onlyStmt = $closure->stmts[0];
if (! $onlyStmt instanceof Return_) {
return null;
}
$return = $onlyStmt;
if (! $return->expr instanceof Expr) {
return null;
}
if ($this->shouldSkipForUsedReferencedValue($closure)) {
return null;
}
if ($this->shouldSkipForUseVariableUsedByCompact($closure)) {
return null;
}
if ($this->shouldSkipMoreSpecificTypeWithVarDoc($return)) {
return null;
}
return $return->expr;
}
private function shouldSkipForUseVariableUsedByCompact(Closure $closure): bool
{
$variables = array_map(fn (ClosureUse $use): Variable => $use->var, $closure->uses);
if ($variables === []) {
return false;
}
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$closure,
function (Node $node) use ($variables): bool {
if (! $node instanceof FuncCall) {
return false;
}
foreach ($variables as $variable) {
if ($this->compactFuncCallAnalyzer->isInCompact($node, $variable)) {
return true;
}
}
return false;
}
);
}
/**
* 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): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($return);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
return $varTagValueNode instanceof VarTagValueNode;
}
private function shouldSkipForUsedReferencedValue(Closure $closure): bool
{
$referencedValues = $this->resolveReferencedUseVariablesFromClosure($closure);
if ($referencedValues === []) {
return false;
}
$isFoundInStmt = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($closure, function (Node $node) use (
$referencedValues
): bool {
foreach ($referencedValues as $referencedValue) {
if ($this->nodeComparator->areNodesEqual($node, $referencedValue)) {
return true;
}
}
return false;
});
if ($isFoundInStmt) {
return true;
}
return $this->isFoundInInnerUses($closure, $referencedValues);
}
/**
* @param Variable[] $referencedValues
*/
private function isFoundInInnerUses(Closure $node, array $referencedValues): bool
{
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$node,
function (Node $subNode) use ($referencedValues): bool {
if (! $subNode instanceof Closure) {
return false;
}
foreach ($referencedValues as $referencedValue) {
$isFoundInInnerUses = $this->arrayChecker->doesExist(
$subNode->uses,
fn (ClosureUse $closureUse): bool => $closureUse->byRef && $this->nodeComparator->areNodesEqual(
$closureUse->var,
$referencedValue
)
);
if ($isFoundInInnerUses) {
return true;
}
}
return false;
}
);
}
/**
* @return Variable[]
*/
private function resolveReferencedUseVariablesFromClosure(Closure $closure): array
{
$referencedValues = [];
/** @var ClosureUse $use */
foreach ($closure->uses as $use) {
if ($use->byRef) {
$referencedValues[] = $use->var;
}
}
return $referencedValues;
}
}