-
Notifications
You must be signed in to change notification settings - Fork 426
/
StrSplitFunctionReturnTypeExtension.php
140 lines (115 loc) · 4.08 KB
/
StrSplitFunctionReturnTypeExtension.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
<?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\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_map;
use function array_unique;
use function count;
use function in_array;
use function mb_internal_encoding;
use function mb_str_split;
use function str_split;
final class StrSplitFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
use MbFunctionsReturnTypeExtensionTrait;
public function __construct(private PhpVersion $phpVersion)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['str_split', 'mb_str_split'], true);
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
if (count($functionCall->getArgs()) < 1) {
return $defaultReturnType;
}
if (count($functionCall->getArgs()) >= 2) {
$splitLengthType = $scope->getType($functionCall->getArgs()[1]->value);
if ($splitLengthType instanceof ConstantIntegerType) {
$splitLength = $splitLengthType->getValue();
if ($splitLength < 1) {
return new ConstantBooleanType(false);
}
}
} else {
$splitLength = 1;
}
$encoding = null;
if ($functionReflection->getName() === 'mb_str_split') {
if (count($functionCall->getArgs()) >= 3) {
$strings = $scope->getType($functionCall->getArgs()[2]->value)->getConstantStrings();
$values = array_unique(array_map(static fn (ConstantStringType $encoding): string => $encoding->getValue(), $strings));
if (count($values) !== 1) {
return $defaultReturnType;
}
$encoding = $values[0];
if (!$this->isSupportedEncoding($encoding)) {
return new ConstantBooleanType(false);
}
} else {
$encoding = mb_internal_encoding();
}
}
if (!isset($splitLength)) {
return $defaultReturnType;
}
$stringType = $scope->getType($functionCall->getArgs()[0]->value);
$constantStrings = $stringType->getConstantStrings();
if (count($constantStrings) > 0) {
$results = [];
foreach ($constantStrings as $constantString) {
$items = $encoding === null
? str_split($constantString->getValue(), $splitLength)
: @mb_str_split($constantString->getValue(), $splitLength, $encoding);
if ($items === false) {
throw new ShouldNotHappenException();
}
$results[] = self::createConstantArrayFrom($items, $scope);
}
return TypeCombinator::union(...$results);
}
$returnType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new StringType()));
return $encoding === null && !$this->phpVersion->strSplitReturnsEmptyArray()
? TypeCombinator::intersect($returnType, new NonEmptyArrayType())
: $returnType;
}
/**
* @param string[] $constantArray
*/
private static function createConstantArrayFrom(array $constantArray, Scope $scope): ConstantArrayType
{
$keyTypes = [];
$valueTypes = [];
$isList = true;
$i = 0;
foreach ($constantArray as $key => $value) {
$keyType = $scope->getTypeFromValue($key);
if (!$keyType instanceof ConstantIntegerType) {
throw new ShouldNotHappenException();
}
$keyTypes[] = $keyType;
$valueTypes[] = $scope->getTypeFromValue($value);
$isList = $isList && $key === $i;
$i++;
}
return new ConstantArrayType($keyTypes, $valueTypes, $isList ? [$i] : [0]);
}
}