-
-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathExprAnalyzer.php
More file actions
274 lines (235 loc) · 8.01 KB
/
Copy pathExprAnalyzer.php
File metadata and controls
274 lines (235 loc) · 8.01 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Rector\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
use PhpParser\Node\Expr\BinaryOp\LogicalXor;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\BitwiseNot;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\ErrorSuppress;
use PhpParser\Node\Expr\Eval_;
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Print_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\UnionType;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
final readonly class ExprAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}
public function isBoolExpr(Expr $expr): bool
{
return $expr instanceof BooleanNot
|| $expr instanceof Empty_
|| $expr instanceof Isset_
|| $expr instanceof Instanceof_
|| $expr instanceof Bool_
|| $expr instanceof Equal
|| $expr instanceof NotEqual
|| $expr instanceof Identical
|| $expr instanceof NotIdentical
|| $expr instanceof Greater
|| $expr instanceof GreaterOrEqual
|| $expr instanceof Smaller
|| $expr instanceof SmallerOrEqual
|| $expr instanceof BooleanAnd
|| $expr instanceof BooleanOr
|| $expr instanceof LogicalAnd
|| $expr instanceof LogicalOr
|| $expr instanceof LogicalXor;
}
public function isCallLikeReturnNativeBool(Expr $expr): bool
{
if (! $expr instanceof CallLike) {
return false;
}
$scope = $expr->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}
$nativeType = $scope->getNativeType($expr);
return $nativeType->isBoolean()
->yes();
}
/**
* Verify that Expr has ->expr property that can be wrapped by parentheses
*/
public function isExprWithExprPropertyWrappable(Node $node): bool
{
if (! $node instanceof Expr) {
return false;
}
// ensure only verify on reprint, using token start verification is more reliable for its check
if ($node->getStartTokenPos() > 0) {
return false;
}
if (
$node instanceof Cast ||
$node instanceof YieldFrom ||
$node instanceof UnaryMinus ||
$node instanceof UnaryPlus ||
$node instanceof Throw_ ||
$node instanceof Empty_ ||
$node instanceof BooleanNot ||
$node instanceof Clone_ ||
$node instanceof ErrorSuppress ||
$node instanceof BitwiseNot ||
$node instanceof Eval_ ||
$node instanceof Print_ ||
$node instanceof Exit_ ||
$node instanceof Include_ ||
$node instanceof Instanceof_) {
return $node->expr instanceof BinaryOp;
}
return false;
}
public function isNonTypedFromParam(Expr $expr): bool
{
if (! $expr instanceof Variable) {
return false;
}
$scope = $expr->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
// uncertainty when scope not yet filled/overlapped on just refactored
return true;
}
$nativeType = $scope->getNativeType($expr);
$type = $scope->getType($expr);
if (
($nativeType instanceof MixedType && ! $nativeType->isExplicitMixed())
||
($nativeType instanceof MixedType && ! $type instanceof MixedType)
) {
return true;
}
if ($nativeType instanceof ObjectWithoutClassType && ! $type instanceof ObjectWithoutClassType) {
return true;
}
if (! $scope->hasVariableType((string) $this->nodeNameResolver->getName($expr))->yes()) {
return true;
}
if ($nativeType instanceof UnionType && ! $nativeType->equals($type)) {
return true;
}
if (! $nativeType->isSuperTypeOf($type)
->yes()) {
return true;
}
$definedVariables = $scope->getDefinedVariables();
foreach ($definedVariables as $definedVariable) {
$variableType = $scope->getVariableType($definedVariable);
if ($variableType instanceof ConstantStringType
&& in_array($variableType->getValue(), $definedVariables, true)) {
return true;
}
}
return false;
}
public function isDynamicExpr(Expr $expr): bool
{
// Unwrap UnaryPlus and UnaryMinus
if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) {
$expr = $expr->expr;
}
if ($expr instanceof Array_) {
return $this->isDynamicArray($expr);
}
if ($expr instanceof Scalar) {
// string interpolation is true, otherwise false
return $expr instanceof InterpolatedString;
}
return ! $this->isAllowedConstFetchOrClassConstFetch($expr);
}
public function isDynamicArray(Array_ $array): bool
{
foreach ($array->items as $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if (! $this->isAllowedArrayKey($item->key)) {
return true;
}
if (! $this->isAllowedArrayValue($item->value)) {
return true;
}
}
return false;
}
private function isAllowedConstFetchOrClassConstFetch(Expr $expr): bool
{
if ($expr instanceof ConstFetch) {
return true;
}
if ($expr instanceof ClassConstFetch) {
if (! $expr->class instanceof Name) {
return false;
}
if (! $expr->name instanceof Identifier) {
return false;
}
// static::class cannot be used for compile-time class name resolution
return $expr->class->toString() !== ObjectReference::STATIC;
}
return false;
}
private function isAllowedArrayKey(?Expr $expr): bool
{
if (! $expr instanceof Expr) {
return true;
}
if ($expr instanceof String_) {
return true;
}
return $expr instanceof Int_;
}
private function isAllowedArrayValue(Expr $expr): bool
{
if ($expr instanceof Array_) {
return ! $this->isDynamicArray($expr);
}
return ! $this->isDynamicExpr($expr);
}
}