1919use PHPStan \Type \Accessory \AccessoryNonFalsyStringType ;
2020use PHPStan \Type \Accessory \AccessoryNumericStringType ;
2121use PHPStan \Type \Accessory \AccessoryType ;
22+ use PHPStan \Type \Accessory \HasOffsetValueType ;
2223use PHPStan \Type \Accessory \NonEmptyArrayType ;
2324use PHPStan \Type \Generic \TemplateType ;
2425use PHPStan \Type \Generic \TemplateTypeMap ;
2526use PHPStan \Type \Generic \TemplateTypeVariance ;
2627use PHPStan \Type \Traits \NonGeneralizableTypeTrait ;
2728use PHPStan \Type \Traits \NonRemoveableTypeTrait ;
2829use function array_map ;
30+ use function array_values ;
2931use function count ;
3032use function implode ;
3133use 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