Skip to content

Commit 17972fe

Browse files
phpstan-botstaabmclaude
authored
Generalize keys and values of oversized arrays in ConstantTypeHelper::getTypeFromValue() like OversizedArrayBuilder (#6572)
Co-authored-by: staabm <120441+staabm@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b0b0f75 commit 17972fe

9 files changed

Lines changed: 219 additions & 66 deletions

File tree

‎src/Turbo/TurboExtensionEnabler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
final class TurboExtensionEnabler
3434
{
3535

36-
public const EXPECTED_EXTENSION_VERSION = '4c0e9a0';
36+
public const EXPECTED_EXTENSION_VERSION = 'a40b926';
3737

3838
private static bool $active = false;
3939

‎src/Type/ConstantTypeHelper.php‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
namespace PHPStan\Type;
44

55
use PHPStan\Turbo\ShadowedByTurboExtension;
6+
use PHPStan\Type\Accessory\AccessoryArrayListType;
7+
use PHPStan\Type\Accessory\NonEmptyArrayType;
8+
use PHPStan\Type\Accessory\OversizedArrayType;
69
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
710
use PHPStan\Type\Constant\ConstantBooleanType;
811
use PHPStan\Type\Constant\ConstantFloatType;
912
use PHPStan\Type\Constant\ConstantIntegerType;
1013
use PHPStan\Type\Constant\ConstantStringType;
1114
use PHPStan\Type\Enum\EnumCaseObjectType;
1215
use UnitEnum;
16+
use function array_is_list;
1317
use function count;
1418
use function function_exists;
1519
use function get_class;
@@ -43,10 +47,10 @@ public static function getTypeFromValue($value): Type
4347
} elseif (is_string($value)) {
4448
return new ConstantStringType($value);
4549
} elseif (is_array($value)) {
46-
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
4750
if (count($value) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
48-
$arrayBuilder->degradeToGeneralArray(true);
51+
return self::getOversizedArrayType($value);
4952
}
53+
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
5054
foreach ($value as $k => $v) {
5155
$arrayBuilder->setOffsetValueType(self::getTypeFromValue($k), self::getTypeFromValue($v));
5256
}
@@ -66,4 +70,39 @@ public static function getTypeFromValue($value): Type
6670
return new MixedType();
6771
}
6872

73+
/**
74+
* Generalizes keys and values like OversizedArrayBuilder does for array literals,
75+
* so that the result does not carry unions of thousands of constant types.
76+
*
77+
* @param non-empty-array<mixed> $value
78+
*/
79+
private static function getOversizedArrayType(array $value): Type
80+
{
81+
$precision = GeneralizePrecision::moreSpecific();
82+
$keyType = new NeverType();
83+
$valueType = new NeverType();
84+
foreach ($value as $k => $v) {
85+
$keyType = self::unionGeneralized($keyType, self::getTypeFromValue($k), $precision);
86+
$valueType = self::unionGeneralized($valueType, self::getTypeFromValue($v), $precision);
87+
}
88+
89+
$accessories = [new NonEmptyArrayType(), new OversizedArrayType()];
90+
if (array_is_list($value)) {
91+
$accessories[] = new AccessoryArrayListType();
92+
}
93+
94+
return TypeCombinator::intersect(new ArrayType($keyType, $valueType), ...$accessories);
95+
}
96+
97+
/** Skips the union when the generalized type is already the accumulated one, which is the common case. */
98+
private static function unionGeneralized(Type $accumulated, Type $type, GeneralizePrecision $precision): Type
99+
{
100+
$generalized = $type->generalize($precision);
101+
if ($generalized->equals($accumulated)) {
102+
return $accumulated;
103+
}
104+
105+
return TypeCombinator::union($accumulated, $generalized);
106+
}
107+
69108
}

‎src/Type/Php/PregSplitDynamicReturnTypeExtension.php‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Type\Constant\ConstantBooleanType;
1818
use PHPStan\Type\Constant\ConstantIntegerType;
1919
use PHPStan\Type\Constant\ConstantStringType;
20+
use PHPStan\Type\ConstantTypeHelper;
2021
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
2122
use PHPStan\Type\ErrorType;
2223
use PHPStan\Type\IntegerRangeType;
@@ -27,7 +28,6 @@
2728
use PHPStan\Type\TypeCombinator;
2829
use PHPStan\Type\UnionType;
2930
use function count;
30-
use function is_array;
3131
use function is_int;
3232
use function is_numeric;
3333
use function preg_split;
@@ -161,20 +161,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
161161
if ($result === false) {
162162
return new ErrorType();
163163
}
164-
$constantArray = ConstantArrayTypeBuilder::createEmpty();
165-
foreach ($result as $key => $value) {
166-
if (is_array($value)) {
167-
$valueConstantArray = ConstantArrayTypeBuilder::createEmpty();
168-
$valueConstantArray->setOffsetValueType(new ConstantIntegerType(0), new ConstantStringType($value[0]));
169-
$valueConstantArray->setOffsetValueType(new ConstantIntegerType(1), new ConstantIntegerType($value[1]));
170-
$returnInternalValueType = $valueConstantArray->getArray();
171-
} else {
172-
$returnInternalValueType = new ConstantStringType($value);
173-
}
174-
$constantArray->setOffsetValueType(new ConstantIntegerType($key), $returnInternalValueType);
175-
}
176-
177-
$resultTypes[] = $constantArray->getArray();
164+
$resultTypes[] = ConstantTypeHelper::getTypeFromValue($result);
178165
}
179166
}
180167
}

‎src/Type/Php/StrSplitFunctionReturnTypeExtension.php‎

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\FunctionReflection;
9-
use PHPStan\ShouldNotHappenException;
10-
use PHPStan\TrinaryLogic;
119
use PHPStan\Type\Accessory\AccessoryArrayListType;
1210
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
1311
use PHPStan\Type\Accessory\NonEmptyArrayType;
1412
use PHPStan\Type\ArrayType;
15-
use PHPStan\Type\Constant\ConstantArrayType;
1613
use PHPStan\Type\Constant\ConstantBooleanType;
1714
use PHPStan\Type\Constant\ConstantIntegerType;
1815
use PHPStan\Type\Constant\ConstantStringType;
@@ -24,7 +21,6 @@
2421
use PHPStan\Type\Type;
2522
use PHPStan\Type\TypeCombinator;
2623
use PHPStan\Type\UnionType;
27-
use function array_is_list;
2824
use function array_map;
2925
use function array_unique;
3026
use function count;
@@ -99,10 +95,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
9995
if ($encoding === null && $value === '') {
10096
// Simulate the str_split call with the analysed PHP Version instead of the runtime one.
10197
if (!$returnsEmptyArray->no()) {
102-
$results[] = self::createConstantArrayFrom([], $scope);
98+
$results[] = $scope->getTypeFromValue([]);
10399
}
104100
if (!$returnsEmptyArray->yes()) {
105-
$results[] = self::createConstantArrayFrom([''], $scope);
101+
$results[] = $scope->getTypeFromValue(['']);
106102
}
107103
continue;
108104
}
@@ -111,7 +107,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
111107
? str_split($value, $splitLength)
112108
: @mb_str_split($value, $splitLength, $encoding);
113109

114-
$results[] = self::createConstantArrayFrom($items, $scope);
110+
$results[] = $scope->getTypeFromValue($items);
115111
}
116112

117113
return TypeCombinator::union(...$results);
@@ -146,30 +142,4 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
146142
return $returnType;
147143
}
148144

149-
/**
150-
* @param string[] $constantArray
151-
*/
152-
private static function createConstantArrayFrom(array $constantArray, Scope $scope): ConstantArrayType
153-
{
154-
$keyTypes = [];
155-
$valueTypes = [];
156-
$isList = true;
157-
$i = 0;
158-
159-
foreach ($constantArray as $key => $value) {
160-
$keyType = $scope->getTypeFromValue($key);
161-
if (!$keyType instanceof ConstantIntegerType) {
162-
throw new ShouldNotHappenException();
163-
}
164-
$keyTypes[] = $keyType;
165-
166-
$valueTypes[] = $scope->getTypeFromValue($value);
167-
168-
$isList = $isList && $key === $i;
169-
$i++;
170-
}
171-
172-
return new ConstantArrayType($keyTypes, $valueTypes, $isList ? [$i] : [0], isList: TrinaryLogic::createFromBoolean(array_is_list($constantArray)));
173-
}
174-
175145
}

0 commit comments

Comments
 (0)