3030use function array_filter ;
3131use function array_keys ;
3232use function array_merge ;
33+ use function array_values ;
3334use function count ;
3435use function implode ;
3536use function in_array ;
@@ -73,7 +74,7 @@ public function __construct(
7374 public function getIterableTypesWithMissingValueTypehint (Type $ type ): array
7475 {
7576 $ descriptions = [];
76- TypeTraverser::map ($ type , function (Type $ type , callable $ traverse ) use (&$ descriptions ): Type {
77+ TypeTraverser::mapMemoized ($ type , function (Type $ type , callable $ traverse ) use (&$ descriptions ): Type {
7778 if ($ type instanceof TemplateType) {
7879 return $ type ;
7980 }
@@ -98,17 +99,19 @@ public function getIterableTypesWithMissingValueTypehint(Type $type): array
9899 return $ traverse (new IntersectionType ($ nonArrayInner ));
99100 }
100101 if ($ type instanceof ConditionalType || $ type instanceof ConditionalTypeForParameter) {
101- $ descriptions = array_merge (
102- $ descriptions ,
102+ $ branchDescriptions = array_merge (
103103 $ this ->getIterableTypesWithMissingValueTypehint ($ type ->getIf ()),
104104 $ this ->getIterableTypesWithMissingValueTypehint ($ type ->getElse ()),
105105 );
106+ foreach ($ branchDescriptions as $ description ) {
107+ $ descriptions [$ description ] = $ description ;
108+ }
106109
107110 return $ type ;
108111 }
109112 if ($ type ->isIterable ()->yes ()) {
110113 if ($ type ->isConstantArray ()->yes ()) {
111- $ type = TypeTraverser::map ($ type , static function (Type $ type , callable $ traverse ) {
114+ $ type = TypeTraverser::mapMemoized ($ type , static function (Type $ type , callable $ traverse ) {
112115 if ($ type instanceof UnionType || $ type instanceof IntersectionType) {
113116 return $ traverse ($ type );
114117 }
@@ -125,7 +128,8 @@ public function getIterableTypesWithMissingValueTypehint(Type $type): array
125128 }
126129 $ iterableValue = $ type ->getIterableValueType ();
127130 if ($ iterableValue instanceof MixedType && !$ iterableValue ->isExplicitMixed ()) {
128- $ descriptions [] = sprintf ('iterable type %s ' , $ type ->describe (VerbosityLevel::typeOnly ()));
131+ $ description = sprintf ('iterable type %s ' , $ type ->describe (VerbosityLevel::typeOnly ()));
132+ $ descriptions [$ description ] = $ description ;
129133 }
130134 if ($ type instanceof IntersectionType) {
131135 if ($ type ->isList ()->yes ()) {
@@ -138,7 +142,7 @@ public function getIterableTypesWithMissingValueTypehint(Type $type): array
138142 return $ traverse ($ type );
139143 });
140144
141- return $ descriptions ;
145+ return array_values ( $ descriptions) ;
142146 }
143147
144148 /**
@@ -147,7 +151,7 @@ public function getIterableTypesWithMissingValueTypehint(Type $type): array
147151 public function getNonGenericObjectTypesWithGenericClass (Type $ type ): array
148152 {
149153 $ objectTypes = [];
150- TypeTraverser::map ($ type , function (Type $ type , callable $ traverse ) use (&$ objectTypes ): Type {
154+ TypeTraverser::mapMemoized ($ type , function (Type $ type , callable $ traverse ) use (&$ objectTypes ): Type {
151155 if ($ type instanceof GenericObjectType || $ type instanceof GenericStaticType) {
152156 $ traverse ($ type );
153157 return $ type ;
@@ -197,17 +201,15 @@ public function getNonGenericObjectTypesWithGenericClass(Type $type): array
197201 $ templateTypesList .= sprintf (' (%d-%d required) ' , $ requiredTemplateTypesCount , $ templateTypesCount );
198202 }
199203
200- $ objectTypes [] = [
201- sprintf ('%s %s ' , strtolower ($ classReflection ->getClassTypeDescription ()), $ classReflection ->getDisplayName (false )),
202- $ templateTypesList ,
203- ];
204+ $ name = sprintf ('%s %s ' , strtolower ($ classReflection ->getClassTypeDescription ()), $ classReflection ->getDisplayName (false ));
205+ $ objectTypes [$ name . "\0" . $ templateTypesList ] = [$ name , $ templateTypesList ];
204206 return $ type ;
205207 }
206208
207209 return $ traverse ($ type );
208210 });
209211
210- return $ objectTypes ;
212+ return array_values ( $ objectTypes) ;
211213 }
212214
213215 /**
0 commit comments