-
Notifications
You must be signed in to change notification settings - Fork 426
/
ArrayColumnFunctionReturnTypeExtension.php
201 lines (170 loc) · 5.91 KB
/
ArrayColumnFunctionReturnTypeExtension.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use function count;
class ArrayColumnFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'array_column';
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$numArgs = count($functionCall->getArgs());
if ($numArgs < 2) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}
$arrayType = $scope->getType($functionCall->getArgs()[0]->value);
$columnType = $scope->getType($functionCall->getArgs()[1]->value);
$indexType = $numArgs >= 3 ? $scope->getType($functionCall->getArgs()[2]->value) : null;
$constantArrayTypes = TypeUtils::getConstantArrays($arrayType);
if (count($constantArrayTypes) === 1) {
$type = $this->handleConstantArray($constantArrayTypes[0], $columnType, $indexType, $scope);
if ($type !== null) {
return $type;
}
}
return $this->handleAnyArray($arrayType, $columnType, $indexType, $scope);
}
private function handleAnyArray(Type $arrayType, Type $columnType, ?Type $indexType, Scope $scope): Type
{
$iterableAtLeastOnce = $arrayType->isIterableAtLeastOnce();
if ($iterableAtLeastOnce->no()) {
return new ConstantArrayType([], []);
}
$iterableValueType = $arrayType->getIterableValueType();
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false);
if ($returnValueType === null) {
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, true);
$iterableAtLeastOnce = TrinaryLogic::createMaybe();
if ($returnValueType === null) {
throw new ShouldNotHappenException();
}
}
if ($returnValueType instanceof NeverType) {
return new ConstantArrayType([], []);
}
if ($indexType !== null) {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false);
if ($type !== null) {
$returnKeyType = $type;
} else {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
$returnKeyType = TypeCombinator::union($type, new IntegerType());
} else {
$returnKeyType = new IntegerType();
}
}
} else {
$returnKeyType = new IntegerType();
}
$returnType = new ArrayType($this->castToArrayKeyType($returnKeyType), $returnValueType);
if ($iterableAtLeastOnce->yes()) {
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType());
}
return $returnType;
}
private function handleConstantArray(ConstantArrayType $arrayType, Type $columnType, ?Type $indexType, Scope $scope): ?Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
foreach ($arrayType->getValueTypes() as $iterableValueType) {
$valueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false);
if ($valueType === null) {
return null;
}
if ($indexType !== null) {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false);
if ($type !== null) {
$keyType = $type;
} else {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
$keyType = TypeCombinator::union($type, new IntegerType());
} else {
$keyType = null;
}
}
} else {
$keyType = null;
}
if ($keyType !== null) {
$keyType = $this->castToArrayKeyType($keyType);
}
$builder->setOffsetValueType($keyType, $valueType);
}
return $builder->getArray();
}
private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope, bool $allowMaybe): ?Type
{
$offsetIsNull = (new NullType())->isSuperTypeOf($offsetOrProperty);
if ($offsetIsNull->yes()) {
return $type;
}
$returnTypes = [];
if ($offsetIsNull->maybe()) {
$returnTypes[] = $type;
}
if (!$type->canAccessProperties()->no()) {
$propertyTypes = TypeUtils::getConstantStrings($offsetOrProperty);
if ($propertyTypes === []) {
return new MixedType();
}
foreach ($propertyTypes as $propertyType) {
$propertyName = $propertyType->getValue();
$hasProperty = $type->hasProperty($propertyName);
if ($hasProperty->maybe()) {
return $allowMaybe ? new MixedType() : null;
}
if (!$hasProperty->yes()) {
continue;
}
$returnTypes[] = $type->getProperty($propertyName, $scope)->getReadableType();
}
}
if ($type->isOffsetAccessible()->yes()) {
$hasOffset = $type->hasOffsetValueType($offsetOrProperty);
if (!$allowMaybe && $hasOffset->maybe()) {
return null;
}
if (!$hasOffset->no()) {
$returnTypes[] = $type->getOffsetValueType($offsetOrProperty);
}
}
if ($returnTypes === []) {
return new NeverType();
}
return TypeCombinator::union(...$returnTypes);
}
private function castToArrayKeyType(Type $type): Type
{
$isArray = $type->isArray();
if ($isArray->yes()) {
return new IntegerType();
}
if ($isArray->no()) {
return ArrayType::castToArrayKeyType($type);
}
return TypeCombinator::union(
ArrayType::castToArrayKeyType(TypeCombinator::remove($type, new ArrayType(new MixedType(), new MixedType()))),
new IntegerType(),
);
}
}