@@ -136,10 +136,7 @@ private function isNestedTypeSuperTypeOf(Type $a, Type $b): IsSuperTypeOfResult
136136 public function isSubTypeOf (Type $ otherType ): IsSuperTypeOfResult
137137 {
138138 if ($ otherType instanceof IntersectionType || $ otherType instanceof UnionType) {
139- return $ otherType ->isSuperTypeOf (new UnionType ([
140- $ this ->toArray (),
141- $ this ->toTraversable (),
142- ]));
139+ return $ otherType ->isSuperTypeOf ($ this ->toArrayOrTraversable ());
143140 }
144141
145142 if ($ otherType instanceof self) {
@@ -228,6 +225,17 @@ public function toArray(): Type
228225 return new ArrayType ($ this ->keyType , $ this ->getItemType ());
229226 }
230227
228+ public function toArrayOrTraversable (): Type
229+ {
230+ return new UnionType ([
231+ new ArrayType ($ this ->keyType , $ this ->itemType ),
232+ new GenericObjectType (Traversable::class, [
233+ $ this ->keyType ,
234+ $ this ->itemType ,
235+ ]),
236+ ]);
237+ }
238+
231239 public function toArrayKey (): Type
232240 {
233241 return new ErrorType ();
@@ -247,7 +255,10 @@ public function toCoercedArgumentType(bool $strictTypes): Type
247255 ),
248256 $ this ->itemType ,
249257 ),
250- $ this ->toTraversable (),
258+ new GenericObjectType (Traversable::class, [
259+ $ this ->keyType ,
260+ $ this ->itemType ,
261+ ]),
251262 );
252263 }
253264
@@ -475,12 +486,15 @@ public function tryRemove(Type $typeToRemove): ?Type
475486 {
476487 $ arrayType = new ArrayType (new MixedType (), new MixedType ());
477488 if ($ typeToRemove ->isSuperTypeOf ($ arrayType )->yes ()) {
478- return $ this ->toTraversable ();
489+ return new GenericObjectType (Traversable::class, [
490+ $ this ->getIterableKeyType (),
491+ $ this ->getIterableValueType (),
492+ ]);
479493 }
480494
481495 $ traversableType = new ObjectType (Traversable::class);
482496 if ($ typeToRemove ->isSuperTypeOf ($ traversableType )->yes ()) {
483- return $ this ->toArray ( );
497+ return new ArrayType ( $ this ->getIterableKeyType (), $ this -> getIterableValueType () );
484498 }
485499
486500 return null ;
@@ -528,12 +542,4 @@ public function hasTemplateOrLateResolvableType(): bool
528542 return $ this ->keyType ->hasTemplateOrLateResolvableType () || $ this ->itemType ->hasTemplateOrLateResolvableType ();
529543 }
530544
531- public function toTraversable (): Type
532- {
533- return new GenericObjectType (Traversable::class, [
534- $ this ->keyType ,
535- $ this ->itemType ,
536- ]);
537- }
538-
539545}
0 commit comments