-
Notifications
You must be signed in to change notification settings - Fork 426
/
ImpossibleCheckTypeHelper.php
371 lines (319 loc) · 9.58 KB
/
ImpossibleCheckTypeHelper.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use function array_map;
use function array_pop;
use function count;
use function implode;
use function in_array;
use function is_string;
use function reset;
use function sprintf;
use function strtolower;
class ImpossibleCheckTypeHelper
{
/**
* @param string[] $universalObjectCratesClasses
*/
public function __construct(
private ReflectionProvider $reflectionProvider,
private TypeSpecifier $typeSpecifier,
private array $universalObjectCratesClasses,
private bool $treatPhpDocTypesAsCertain,
)
{
}
public function findSpecifiedType(
Scope $scope,
Expr $node,
): ?bool
{
if (
$node instanceof FuncCall
&& count($node->getArgs()) > 0
) {
if ($node->name instanceof Node\Name) {
$functionName = strtolower((string) $node->name);
if ($functionName === 'assert') {
$assertValue = $scope->getType($node->getArgs()[0]->value)->toBoolean();
if (!$assertValue instanceof ConstantBooleanType) {
return null;
}
return $assertValue->getValue();
}
if (in_array($functionName, [
'class_exists',
'interface_exists',
'trait_exists',
'enum_exists',
], true)) {
return null;
}
if (in_array($functionName, ['count', 'sizeof'], true)) {
return null;
} elseif ($functionName === 'defined') {
return null;
} elseif ($functionName === 'array_search') {
return null;
} elseif (
$functionName === 'in_array'
&& count($node->getArgs()) >= 3
) {
$haystackType = $scope->getType($node->getArgs()[1]->value);
if ($haystackType instanceof MixedType) {
return null;
}
if (!$haystackType->isArray()->yes()) {
return null;
}
$constantArrays = TypeUtils::getConstantArrays($haystackType);
$needleType = $scope->getType($node->getArgs()[0]->value);
$valueType = $haystackType->getIterableValueType();
$constantNeedleTypesCount = count(TypeUtils::getConstantScalars($needleType));
$constantHaystackTypesCount = count(TypeUtils::getConstantScalars($valueType));
$isNeedleSupertype = $needleType->isSuperTypeOf($valueType);
if (count($constantArrays) === 0) {
if ($haystackType->isIterableAtLeastOnce()->yes()) {
if ($constantNeedleTypesCount === 1 && $constantHaystackTypesCount === 1) {
if ($isNeedleSupertype->yes()) {
return true;
}
if ($isNeedleSupertype->no()) {
return false;
}
}
}
return null;
}
if (!$haystackType instanceof ConstantArrayType || count($haystackType->getValueTypes()) > 0) {
$haystackArrayTypes = TypeUtils::getArrays($haystackType);
if (count($haystackArrayTypes) === 1 && $haystackArrayTypes[0]->getIterableValueType() instanceof NeverType) {
return null;
}
if ($isNeedleSupertype->maybe() || $isNeedleSupertype->yes()) {
foreach ($haystackArrayTypes as $haystackArrayType) {
foreach (TypeUtils::getConstantScalars($haystackArrayType->getIterableValueType()) as $constantScalarType) {
if ($needleType->isSuperTypeOf($constantScalarType)->yes()) {
continue 2;
}
}
return null;
}
}
if ($isNeedleSupertype->yes()) {
$hasConstantNeedleTypes = $constantNeedleTypesCount > 0;
$hasConstantHaystackTypes = $constantHaystackTypesCount > 0;
if (
(!$hasConstantNeedleTypes && !$hasConstantHaystackTypes)
|| $hasConstantNeedleTypes !== $hasConstantHaystackTypes
) {
return null;
}
}
}
} elseif ($functionName === 'method_exists' && count($node->getArgs()) >= 2) {
$objectType = $scope->getType($node->getArgs()[0]->value);
$methodType = $scope->getType($node->getArgs()[1]->value);
if ($objectType instanceof ConstantStringType
&& !$this->reflectionProvider->hasClass($objectType->getValue())
) {
return false;
}
if ($methodType instanceof ConstantStringType) {
if ($objectType instanceof ConstantStringType) {
$objectType = new ObjectType($objectType->getValue());
}
if ($objectType instanceof TypeWithClassName) {
if ($objectType->hasMethod($methodType->getValue())->yes()) {
return true;
}
if ($objectType->hasMethod($methodType->getValue())->no()) {
return false;
}
}
}
}
}
}
$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $node, TypeSpecifierContext::createTruthy());
// don't validate types on overwrite
if ($specifiedTypes->shouldOverwrite()) {
return null;
}
$sureTypes = $specifiedTypes->getSureTypes();
$sureNotTypes = $specifiedTypes->getSureNotTypes();
return $this->findSpecifiedSureAndSureNotTypes($scope, $node, $sureTypes, $sureNotTypes);
}
/**
* @param array<string|int, array{Expr, Type}> $sureTypes
* @param array<string|int, array{Expr, Type}> $sureNotTypes
*/
private function findSpecifiedSureAndSureNotTypes(
Scope $scope,
Expr $node,
array $sureTypes,
array $sureNotTypes,
): ?bool
{
$isSpecified = static function (Expr $expr) use ($scope, $node): bool {
if ($expr === $node) {
return true;
}
if ($expr instanceof Expr\Variable && is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes()) {
return true;
}
return (
$node instanceof FuncCall
|| $node instanceof MethodCall
|| $node instanceof Expr\StaticCall
) && $scope->isSpecified($expr);
};
if (count($sureTypes) === 1 && count($sureNotTypes) === 0) {
$sureType = reset($sureTypes);
if ($isSpecified($sureType[0])) {
return null;
}
if ($this->treatPhpDocTypesAsCertain) {
$argumentType = $scope->getType($sureType[0]);
} else {
$argumentType = $scope->getNativeType($sureType[0]);
}
/** @var Type $resultType */
$resultType = $sureType[1];
$isSuperType = $resultType->isSuperTypeOf($argumentType);
if ($isSuperType->yes()) {
return true;
} elseif ($isSuperType->no()) {
return false;
}
return null;
} elseif (count($sureNotTypes) === 1 && count($sureTypes) === 0) {
$sureNotType = reset($sureNotTypes);
if ($isSpecified($sureNotType[0])) {
return null;
}
if ($this->treatPhpDocTypesAsCertain) {
$argumentType = $scope->getType($sureNotType[0]);
} else {
$argumentType = $scope->getNativeType($sureNotType[0]);
}
/** @var Type $resultType */
$resultType = $sureNotType[1];
$isSuperType = $resultType->isSuperTypeOf($argumentType);
if ($isSuperType->yes()) {
return false;
} elseif ($isSuperType->no()) {
return true;
}
return null;
}
if (count($sureTypes) > 0) {
foreach ($sureTypes as $sureType) {
if ($isSpecified($sureType[0])) {
return null;
}
}
foreach ($sureTypes as $sureType) {
$types = $this->sureUnionTypes($sureTypes, $sureType[0]);
if (count($types) === 1 && count($sureNotTypes) === 0) {
$findTypes = $this->findSpecifiedSureAndSureNotTypes($scope, $node, [$sureType], $sureNotTypes);
if ($findTypes !== null) {
return $findTypes;
}
}
if (TypeCombinator::union(...$types) instanceof NeverType) {
return false;
}
}
}
if (count($sureNotTypes) > 0) {
foreach ($sureNotTypes as $sureNotType) {
if ($isSpecified($sureNotType[0])) {
return null;
}
}
foreach ($sureNotTypes as $sureNotType) {
$types = $this->sureUnionTypes($sureNotTypes, $sureNotType[0]);
if (count($types) === 1 && count($sureTypes) === 0) {
$findTypes = $this->findSpecifiedSureAndSureNotTypes($scope, $node, $sureTypes, [$sureNotType]);
if ($findTypes !== null) {
return $findTypes;
}
}
if (TypeCombinator::union(...$types) instanceof NeverType) {
return true;
}
}
}
return null;
}
/**
* @param array<string|int, array{Expr, Type}> $sureOrSureNotTypes
*
* @return list<Type>
*/
private function sureUnionTypes(array $sureOrSureNotTypes, Expr $expr): array
{
$types = [];
foreach ($sureOrSureNotTypes as [$sureExpr, $type]) {
if ($sureExpr !== $expr) {
continue;
}
$types[] = $type;
}
return $types;
}
/**
* @param Node\Arg[] $args
*/
public function getArgumentsDescription(
Scope $scope,
array $args,
): string
{
if (count($args) === 0) {
return '';
}
$descriptions = array_map(static fn (Arg $arg): string => $scope->getType($arg->value)->describe(VerbosityLevel::value()), $args);
if (count($descriptions) < 3) {
return sprintf(' with %s', implode(' and ', $descriptions));
}
$lastDescription = array_pop($descriptions);
return sprintf(
' with arguments %s and %s',
implode(', ', $descriptions),
$lastDescription,
);
}
public function doNotTreatPhpDocTypesAsCertain(): self
{
if (!$this->treatPhpDocTypesAsCertain) {
return $this;
}
return new self(
$this->reflectionProvider,
$this->typeSpecifier,
$this->universalObjectCratesClasses,
false,
);
}
}