-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathArrayColumnHelper.php
More file actions
204 lines (172 loc) · 5.85 KB
/
ArrayColumnHelper.php
File metadata and controls
204 lines (172 loc) · 5.85 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
<?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\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));
}
return $builder->getArray();
}
/**
* @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()];
}
}
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());
}
}