forked from phpstan/phpstan-src
/
ArrayFillFunctionReturnTypeExtension.php
95 lines (79 loc) · 3.03 KB
/
ArrayFillFunctionReturnTypeExtension.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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
class ArrayFillFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
private const MAX_SIZE_USE_CONSTANT_ARRAY = 100;
public function __construct(private PhpVersion $phpVersion)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'array_fill';
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if (count($functionCall->getArgs()) < 3) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}
$numberType = $scope->getType($functionCall->getArgs()[1]->value);
$isValidNumberType = IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($numberType);
// check against negative-int, which is not allowed
if ($isValidNumberType->no()) {
if ($this->phpVersion->throwsValueErrorForInternalFunctions()) {
return new NeverType();
}
return new ConstantBooleanType(false);
}
$startIndexType = $scope->getType($functionCall->getArgs()[0]->value);
$valueType = $scope->getType($functionCall->getArgs()[2]->value);
if (
$startIndexType instanceof ConstantIntegerType
&& $numberType instanceof ConstantIntegerType
&& $numberType->getValue() <= self::MAX_SIZE_USE_CONSTANT_ARRAY
) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$nextIndex = $startIndexType->getValue();
for ($i = 0; $i < $numberType->getValue(); $i++) {
$arrayBuilder->setOffsetValueType(
new ConstantIntegerType($nextIndex),
$valueType,
);
if ($nextIndex < 0) {
$nextIndex = 0;
} else {
$nextIndex++;
}
}
return $arrayBuilder->getArray();
}
$resultType = new ArrayType(new IntegerType(), $valueType);
if ((new ConstantIntegerType(0))->isSuperTypeOf($startIndexType)->yes()) {
$resultType = AccessoryArrayListType::intersectWith($resultType);
}
if (IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($numberType)->yes()) {
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
}
if (!$isValidNumberType->yes() && !$this->phpVersion->throwsValueErrorForInternalFunctions()) {
$resultType = TypeCombinator::union($resultType, new ConstantBooleanType(false));
}
return $resultType;
}
}