Skip to content

Commit e06f6f0

Browse files
committed
IntersectionType - truncate HasOffsetValueType after 32 occurences
1 parent dbdcb81 commit e06f6f0

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

src/Type/IntersectionType.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
2020
use PHPStan\Type\Accessory\AccessoryNumericStringType;
2121
use PHPStan\Type\Accessory\AccessoryType;
22+
use PHPStan\Type\Accessory\HasOffsetValueType;
2223
use PHPStan\Type\Accessory\NonEmptyArrayType;
2324
use PHPStan\Type\Generic\TemplateType;
2425
use PHPStan\Type\Generic\TemplateTypeMap;
2526
use PHPStan\Type\Generic\TemplateTypeVariance;
2627
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
2728
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
2829
use function array_map;
30+
use function array_values;
2931
use function count;
3032
use function implode;
3133
use function in_array;
@@ -40,21 +42,43 @@ class IntersectionType implements CompoundType
4042
use NonRemoveableTypeTrait;
4143
use NonGeneralizableTypeTrait;
4244

45+
/** @var Type[] */
46+
private array $types;
47+
4348
private bool $sortedTypes = false;
4449

4550
/**
4651
* @api
4752
* @param Type[] $types
4853
*/
49-
public function __construct(private array $types)
54+
public function __construct(array $types)
5055
{
51-
if (count($types) < 2) {
56+
$hasOffsetValueTypeCount = 0;
57+
$newTypes = [];
58+
foreach ($types as $type) {
59+
if (!$type instanceof HasOffsetValueType) {
60+
$newTypes[] = $type;
61+
continue;
62+
}
63+
64+
$hasOffsetValueTypeCount++;
65+
if ($hasOffsetValueTypeCount > 32) {
66+
continue;
67+
}
68+
69+
$newTypes[] = $type;
70+
}
71+
72+
$newTypes = array_values($newTypes);
73+
if (count($newTypes) < 2) {
5274
throw new ShouldNotHappenException(sprintf(
5375
'Cannot create %s with: %s',
5476
self::class,
55-
implode(', ', array_map(static fn (Type $type): string => $type->describe(VerbosityLevel::value()), $types)),
77+
implode(', ', array_map(static fn (Type $type): string => $type->describe(VerbosityLevel::value()), $newTypes)),
5678
));
5779
}
80+
81+
$this->types = $newTypes;
5882
}
5983

6084
/**

0 commit comments

Comments
 (0)