-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathArrayColumnHelper.php
More file actions
305 lines (261 loc) · 9.13 KB
/
Copy pathArrayColumnHelper.php
File metadata and controls
305 lines (261 loc) · 9.13 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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
#[AutowiredService]
final class ArrayColumnHelper
{
public function __construct(
private PhpVersion $phpVersion,
)
{
}
/**
* @return array{Type, TrinaryLogic}
*/
public function getReturnValueType(Type $arrayType, Type $columnType, Scope $scope): array
{
$iterableAtLeastOnce = $arrayType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return [new NeverType(), $iterableAtLeastOnce];
}
$iterableValueType = $arrayType->getIterableValueType();
[$returnValueType, $certainty] = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope);
if (!$certainty->yes()) {
$iterableAtLeastOnce = TrinaryLogic::createMaybe();
}
return [$returnValueType, $iterableAtLeastOnce];
}
public function getReturnIndexType(Type $arrayType, Type $indexType, Scope $scope): Type
{
if (!$indexType->isNull()->yes()) {
$iterableValueType = $arrayType->getIterableValueType();
[$type, $certainty] = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope);
if ($type instanceof NeverType) {
return new IntegerType();
}
if ($certainty->yes()) {
return $type;
}
return TypeCombinator::union($type, new IntegerType());
}
return new IntegerType();
}
public function handleAnyArray(Type $arrayType, Type $columnType, Type $indexType, Scope $scope): Type
{
[$returnValueType, $iterableAtLeastOnce] = $this->getReturnValueType($arrayType, $columnType, $scope);
if ($returnValueType instanceof NeverType) {
return new ConstantArrayType([], []);
}
$returnKeyType = $this->getReturnIndexType($arrayType, $indexType, $scope);
$returnType = new ArrayType($this->castToArrayKeyType($returnKeyType), $returnValueType);
if ($iterableAtLeastOnce->yes()) {
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType());
}
if ($indexType->isNull()->yes()) {
$returnType = TypeCombinator::intersect($returnType, new AccessoryArrayListType());
}
return $returnType;
}
public function handleConstantArray(ConstantArrayType $arrayType, Type $columnType, Type $indexType, Scope $scope): ?Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($arrayType->getValueTypes() as $i => $iterableValueType) {
[$valueType, $certainty] = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope);
if (!$certainty->yes()) {
return null;
}
if ($valueType instanceof NeverType) {
continue;
}
if (!$indexType->isNull()->yes()) {
[$type, $certainty] = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope);
if ($type instanceof NeverType) {
$keyType = null;
} elseif ($certainty->yes()) {
$keyType = $type;
} else {
$keyType = TypeCombinator::union($type, new IntegerType());
}
} else {
$keyType = null;
}
if ($keyType !== null) {
$keyType = $this->castToArrayKeyType($keyType);
}
$builder->setOffsetValueType($keyType, $valueType, $arrayType->isOptionalKey($i));
}
if ($arrayType->isUnsealed()->yes()) {
$unsealedTypes = $arrayType->getUnsealedTypes();
if ($unsealedTypes !== null) {
[$unsealedValueType, $unsealedCertainty] = $this->getOffsetOrProperty($unsealedTypes[1], $columnType, $scope);
if (!$unsealedCertainty->yes()) {
return null;
}
if (!$unsealedValueType instanceof NeverType) {
if (!$indexType->isNull()->yes()) {
[$unsealedKeyFromIndex, $unsealedKeyCertainty] = $this->getOffsetOrProperty($unsealedTypes[1], $indexType, $scope);
if ($unsealedKeyFromIndex instanceof NeverType) {
$unsealedKey = $unsealedTypes[0];
} elseif ($unsealedKeyCertainty->yes()) {
$unsealedKey = $this->castToArrayKeyType($unsealedKeyFromIndex);
} else {
$unsealedKey = $this->castToArrayKeyType(TypeCombinator::union($unsealedKeyFromIndex, new IntegerType()));
}
} else {
// `null` indexType keeps integer-keyed list semantics —
// the unsealed range remains keyed by the source's
// unsealed keys (typically `int`).
$unsealedKey = $unsealedTypes[0];
}
$builder->makeUnsealed($unsealedKey, $unsealedValueType);
}
}
}
return $builder->getArray();
}
/**
* Returns the property names from $columnType that the objects contained in
* $iterableValueType certainly lack, so that reading them via array_column()
* would yield nothing.
*
* Unlike the type-inference path (getOffsetOrProperty), this resolves
* properties at the ClassReflection level so that concrete and non-final
* element classes are reported - matching how AccessPropertiesRule reports
* direct property fetches.
*
* @return list<ConstantStringType>
*/
public function findMissingObjectProperties(Type $iterableValueType, Type $columnType): array
{
// array_column() reads object properties (never ArrayAccess offsets), so
// only check when the elements are definitely objects. Array elements use
// offset access, scalars never have the member - leave those to other rules.
if (!$iterableValueType->isObject()->yes()) {
return [];
}
$propertyNameTypes = $columnType->getConstantStrings();
if ($propertyNameTypes === []) {
return [];
}
$missing = [];
foreach ($propertyNameTypes as $propertyNameType) {
if (!$this->isObjectPropertyMissing($iterableValueType, $propertyNameType->getValue())) {
continue;
}
$missing[] = $propertyNameType;
}
return $missing;
}
private function isObjectPropertyMissing(Type $iterableValueType, string $propertyName): bool
{
$classReflections = $iterableValueType->getObjectClassReflections();
if ($classReflections === []) {
return false;
}
foreach ($classReflections as $classReflection) {
if ($classReflection->isEnum()) {
return false;
}
if ($classReflection->hasInstanceProperty($propertyName)) {
return false;
}
if ($classReflection->allowsDynamicProperties()) {
return false;
}
if ($classReflection->hasNativeMethod('__isset') && $classReflection->hasNativeMethod('__get')) {
return false;
}
}
return true;
}
/**
* @return array{Type, TrinaryLogic}
*/
private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope): array
{
$offsetIsNull = $offsetOrProperty->isNull();
if ($offsetIsNull->yes()) {
return [$type, TrinaryLogic::createYes()];
}
$returnTypes = [];
if ($offsetIsNull->maybe()) {
$returnTypes[] = $type;
}
if (!$type->canAccessProperties()->no()) {
$propertyTypes = $offsetOrProperty->getConstantStrings();
if ($propertyTypes === []) {
return [new MixedType(), TrinaryLogic::createMaybe()];
}
foreach ($propertyTypes as $propertyType) {
$propertyName = $propertyType->getValue();
$hasProperty = $type->hasInstanceProperty($propertyName);
if ($hasProperty->maybe()) {
return [new MixedType(), TrinaryLogic::createMaybe()];
}
if (!$hasProperty->yes()) {
continue;
}
$property = $type->getInstanceProperty($propertyName, $scope);
if (!$scope->canReadProperty($property)) {
foreach ($type->getObjectClassReflections() as $classReflection) {
if ($classReflection->hasMethod('__isset') && $classReflection->hasMethod('__get')) {
return [new MixedType(), TrinaryLogic::createMaybe()];
}
if (!$classReflection->isFinalByKeyword()) {
if ($property->isPrivate()) {
return [new MixedType(), TrinaryLogic::createMaybe()];
}
return [$property->getReadableType(), TrinaryLogic::createMaybe()];
}
}
continue;
}
$returnTypes[] = $property->getReadableType();
}
}
$certainty = TrinaryLogic::createYes();
if ($type->isOffsetAccessible()->yes()) {
$hasOffset = $type->hasOffsetValueType($offsetOrProperty);
if ($hasOffset->maybe()) {
$certainty = TrinaryLogic::createMaybe();
}
if (!$hasOffset->no()) {
$returnTypes[] = $type->getOffsetValueType($offsetOrProperty);
}
}
if ($returnTypes === []) {
return [new NeverType(), TrinaryLogic::createYes()];
}
return [TypeCombinator::union(...$returnTypes), $certainty];
}
private function castToArrayKeyType(Type $type): Type
{
$isArray = $type->isArray();
if ($isArray->yes()) {
return $this->phpVersion->throwsTypeErrorForInternalFunctions() ? new NeverType() : new IntegerType();
}
if ($isArray->no()) {
return $type->toArrayKey();
}
$withoutArrayType = TypeCombinator::remove($type, new ArrayType(new MixedType(), new MixedType()));
$keyType = $withoutArrayType->toArrayKey();
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
return $keyType;
}
return TypeCombinator::union($keyType, new IntegerType());
}
}