Repository navigation
Expand file tree
/
Copy pathPreparedAssignTarget.php
More file actions
319 lines (273 loc) · 7.65 KB
/
Copy pathPreparedAssignTarget.php
File metadata and controls
319 lines (273 loc) · 7.65 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PHPStan\Dependency\Dependencies;
use PHPStan\Node\Expr\ExistingArrayDimFetch;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Type;
/**
* The pre-value half of an assignment, produced by AssignHandler::prepareTarget():
* the target's sub-expressions (root, dimensions, receiver, dynamic name) are
* walked in PHP's evaluation order and everything AssignHandler::applyWrite()
* needs to perform the write is captured here. The caller processes the assigned
* value on getScope() between the two calls - inline, instead of through an
* injected callback.
*
* @internal
*/
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/PreparedAssignTarget.cpp')]
final class PreparedAssignTarget
{
public const KIND_VARIABLE = 'variable';
public const KIND_ARRAY_DIM_FETCH = 'arrayDimFetch';
public const KIND_PROPERTY_FETCH = 'propertyFetch';
public const KIND_STATIC_PROPERTY_FETCH = 'staticPropertyFetch';
public const KIND_LIST = 'list';
public const KIND_EXISTING_ARRAY_DIM_FETCH = 'existingArrayDimFetch';
public const KIND_FALLBACK = 'fallback';
/**
* @param self::KIND_* $kind
* @param InternalThrowPoint[] $throwPoints
* @param ImpurePoint[] $impurePoints
* @param non-empty-list<ArrayDimFetch>|null $dimFetchStack
* @param non-empty-list<array{Type|null, ArrayDimFetch}>|null $offsetTypes
* @param non-empty-list<array{Type|null, ArrayDimFetch}>|null $offsetNativeTypes
* @param non-empty-list<array{Type, ExistingArrayDimFetch}>|null $existingOffsetTypes
* @param non-empty-list<array{Type, ExistingArrayDimFetch}>|null $existingOffsetNativeTypes
* @param ExpressionResult[] $targetChainResults
* @param list<ArrayDimFetch> $illegalOffsetDimFetches
*/
public function __construct(
private string $kind,
private Expr $var,
private Expr $assignedExpr,
private MutatingScope $beforeScope,
private MutatingScope $scope,
private bool $enterExpressionAssign,
private bool $isAssignOp,
private bool $hasYield,
private array $throwPoints,
private array $impurePoints,
private bool $isAlwaysTerminating,
private ?Expr $rootVar = null,
private ?ExpressionResult $varResult = null,
private ?array $dimFetchStack = null,
private ?Expr $assignedPropertyExpr = null,
private ?array $offsetTypes = null,
private ?array $offsetNativeTypes = null,
private ?array $existingOffsetTypes = null,
private ?array $existingOffsetNativeTypes = null,
private ?ExpressionResult $offsetSetTargetResult = null,
private ?ExpressionResult $objectResult = null,
private ?string $propertyName = null,
private ?Type $propertyHolderType = null,
private ?ExpressionResult $targetReadResult = null,
private array $targetChainResults = [],
private ?ExpressionResult $variableNameResult = null,
private ?Dependencies $dependencies = null,
private array $illegalOffsetDimFetches = [],
)
{
}
/**
* What walking the parts of the target depends on, and reading it.
*/
public function getDependencies(): ?Dependencies
{
return $this->dependencies;
}
/**
* @return self::KIND_*
*/
public function getKind(): string
{
return $this->kind;
}
public function getVar(): Expr
{
return $this->var;
}
public function getAssignedExpr(): Expr
{
return $this->assignedExpr;
}
public function getBeforeScope(): MutatingScope
{
return $this->beforeScope;
}
/** The scope the caller processes the assigned value on. */
public function getScope(): MutatingScope
{
return $this->scope;
}
public function enterExpressionAssign(): bool
{
return $this->enterExpressionAssign;
}
public function isAssignOp(): bool
{
return $this->isAssignOp;
}
public function hasYield(): bool
{
return $this->hasYield;
}
/**
* @return InternalThrowPoint[]
*/
public function getThrowPoints(): array
{
return $this->throwPoints;
}
/**
* @return ImpurePoint[]
*/
public function getImpurePoints(): array
{
return $this->impurePoints;
}
public function isAlwaysTerminating(): bool
{
return $this->isAlwaysTerminating;
}
public function getRootVar(): Expr
{
if ($this->rootVar === null) {
throw new ShouldNotHappenException();
}
return $this->rootVar;
}
public function getVarResult(): ExpressionResult
{
if ($this->varResult === null) {
throw new ShouldNotHappenException();
}
return $this->varResult;
}
/**
* @return non-empty-list<ArrayDimFetch>
*/
public function getDimFetchStack(): array
{
if ($this->dimFetchStack === null) {
throw new ShouldNotHappenException();
}
return $this->dimFetchStack;
}
public function getAssignedPropertyExpr(): Expr
{
if ($this->assignedPropertyExpr === null) {
throw new ShouldNotHappenException();
}
return $this->assignedPropertyExpr;
}
/**
* @return non-empty-list<array{Type|null, ArrayDimFetch}>
*/
public function getOffsetTypes(): array
{
if ($this->offsetTypes === null) {
throw new ShouldNotHappenException();
}
return $this->offsetTypes;
}
/**
* @return non-empty-list<array{Type|null, ArrayDimFetch}>
*/
public function getOffsetNativeTypes(): array
{
if ($this->offsetNativeTypes === null) {
throw new ShouldNotHappenException();
}
return $this->offsetNativeTypes;
}
/**
* @return non-empty-list<array{Type, ExistingArrayDimFetch}>
*/
public function getExistingOffsetTypes(): array
{
if ($this->existingOffsetTypes === null) {
throw new ShouldNotHappenException();
}
return $this->existingOffsetTypes;
}
/**
* @return non-empty-list<array{Type, ExistingArrayDimFetch}>
*/
public function getExistingOffsetNativeTypes(): array
{
if ($this->existingOffsetNativeTypes === null) {
throw new ShouldNotHappenException();
}
return $this->existingOffsetNativeTypes;
}
/**
* The chain link an ArrayAccess::offsetSet would be invoked on: the
* second-outermost link's write-flavoured result, or the root's result for
* a single-dimension target.
*/
public function getOffsetSetTargetResult(): ExpressionResult
{
if ($this->offsetSetTargetResult === null) {
throw new ShouldNotHappenException();
}
return $this->offsetSetTargetResult;
}
public function getObjectResult(): ExpressionResult
{
if ($this->objectResult === null) {
throw new ShouldNotHappenException();
}
return $this->objectResult;
}
public function getPropertyName(): ?string
{
return $this->propertyName;
}
public function getPropertyHolderType(): Type
{
if ($this->propertyHolderType === null) {
throw new ShouldNotHappenException();
}
return $this->propertyHolderType;
}
/**
* The whole target priced as a read - produced only in the
* read-modify-write walk modes.
*/
public function getTargetReadResult(): ExpressionResult
{
if ($this->targetReadResult === null) {
throw new ShouldNotHappenException();
}
return $this->targetReadResult;
}
/**
* @return ExpressionResult[]
*/
public function getTargetChainResults(): array
{
return $this->targetChainResults;
}
/**
* A dynamic variable name (`$$name`) already walked by prepareTarget() -
* read-modify-write targets evaluate the name before reading the old value.
*/
public function getVariableNameResult(): ?ExpressionResult
{
return $this->variableNameResult;
}
/**
* The links of an offset write whose offset may be an array or an object,
* which throws TypeError on PHP 8+ - after the assigned value was evaluated
* and an undefined root was turned into an array.
*
* @return list<ArrayDimFetch>
*/
public function getIllegalOffsetDimFetches(): array
{
return $this->illegalOffsetDimFetches;
}
}