-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathClosureArrowFunctionAnalyzer.php
More file actions
203 lines (168 loc) · 6 KB
/
ClosureArrowFunctionAnalyzer.php
File metadata and controls
203 lines (168 loc) · 6 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?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 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;
final readonly class ClosureArrowFunctionAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator,
private ArrayChecker $arrayChecker,
private PhpDocInfoFactory $phpDocInfoFactory,
private NodeTypeResolver $nodeTypeResolver,
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;
}
/** @var Return_ $return */
$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->expr)) {
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 with more specific type on purpose to be skipped
*/
private function shouldSkipMoreSpecificTypeWithVarDoc(Return_ $return, Expr $expr): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($return);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}
$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);
}
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;
}
}