1616use PHPStan \Type \Constant \ConstantArrayType ;
1717use PHPStan \Type \FloatType ;
1818use PHPStan \Type \IntegerType ;
19+ use PHPStan \Type \IntersectionType ;
1920use PHPStan \Type \MixedType ;
2021use PHPStan \Type \NeverType ;
2122use PHPStan \Type \StringType ;
2223use PHPStan \Type \Type ;
24+ use PHPStan \Type \UnionType ;
2325use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
2426use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
27+ use Rector \NodeTypeResolver \PHPStan \Type \TypeFactory ;
2528use Rector \Rector \AbstractRector ;
2629use Rector \StaticTypeMapper \StaticTypeMapper ;
2730use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
@@ -35,7 +38,8 @@ final class DocblockReturnArrayFromDirectArrayInstanceRector extends AbstractRec
3538 public function __construct (
3639 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
3740 private readonly DocBlockUpdater $ docBlockUpdater ,
38- private readonly StaticTypeMapper $ staticTypeMapper
41+ private readonly StaticTypeMapper $ staticTypeMapper ,
42+ private readonly TypeFactory $ typeFactory
3943 ) {
4044 }
4145
@@ -145,6 +149,20 @@ private function constantToGenericType(Type $type): Type
145149 return new FloatType ();
146150 }
147151
152+ if ($ type instanceof UnionType || $ type instanceof IntersectionType) {
153+ $ genericComplexTypes = [];
154+ foreach ($ type ->getTypes () as $ splitType ) {
155+ $ genericComplexTypes [] = $ this ->constantToGenericType ($ splitType );
156+ }
157+
158+ $ genericComplexTypes = $ this ->typeFactory ->uniquateTypes ($ genericComplexTypes );
159+ if (count ($ genericComplexTypes ) > 1 ) {
160+ return new UnionType ($ genericComplexTypes );
161+ }
162+
163+ return $ genericComplexTypes [0 ];
164+ }
165+
148166 // unclear
149167 return new MixedType ();
150168 }
0 commit comments