@@ -45,6 +45,15 @@ public static function getReturnType(\ReflectionFunctionAbstract $func): ?string
4545 }
4646
4747
48+ /**
49+ * Returns the types of return value of given function or method and normalizes `self`, `static`, and `parent` to actual class names.
50+ */
51+ public static function getReturnTypes (\ReflectionFunctionAbstract $ func ): array
52+ {
53+ return self ::getType ($ func , $ func ->getReturnType (), true );
54+ }
55+
56+
4857 /**
4958 * Returns the type of given parameter and normalizes `self` and `parent` to the actual class names.
5059 * If the parameter does not have a type, it returns null.
@@ -56,6 +65,15 @@ public static function getParameterType(\ReflectionParameter $param): ?string
5665 }
5766
5867
68+ /**
69+ * Returns the types of given parameter and normalizes `self` and `parent` to the actual class names.
70+ */
71+ public static function getParameterTypes (\ReflectionParameter $ param ): array
72+ {
73+ return self ::getType ($ param , $ param ->getType (), true );
74+ }
75+
76+
5977 /**
6078 * Returns the type of given property and normalizes `self` and `parent` to the actual class names.
6179 * If the property does not have a type, it returns null.
@@ -67,18 +85,41 @@ public static function getPropertyType(\ReflectionProperty $prop): ?string
6785 }
6886
6987
88+ /**
89+ * Returns the types of given property and normalizes `self` and `parent` to the actual class names.
90+ */
91+ public static function getPropertyTypes (\ReflectionProperty $ prop ): array
92+ {
93+ return self ::getType ($ prop , PHP_VERSION_ID >= 70400 ? $ prop ->getType () : null , true );
94+ }
95+
96+
7097 /**
7198 * @param \ReflectionFunction|\ReflectionMethod|\ReflectionParameter|\ReflectionProperty $reflection
99+ * @return string|array|null
72100 */
73- private static function getType ($ reflection , ?\ReflectionType $ type): ? string
101+ private static function getType ($ reflection , ?\ReflectionType $ type, bool $ asArray = false )
74102 {
75103 if ($ type === null ) {
76- return null ;
104+ return $ asArray ? [] : null ;
77105
78106 } elseif ($ type instanceof \ReflectionNamedType) {
79- return self ::normalizeType ($ type ->getName (), $ reflection );
107+ $ name = self ::normalizeType ($ type ->getName (), $ reflection );
108+ if ($ asArray ) {
109+ return $ type ->allowsNull () && $ type ->getName () !== 'mixed '
110+ ? [$ name , 'null ' ]
111+ : [$ name ];
112+ }
113+ return $ name ;
80114
81115 } elseif ($ type instanceof \ReflectionUnionType) {
116+ if ($ asArray ) {
117+ $ types = [];
118+ foreach ($ type ->getTypes () as $ type ) {
119+ $ types [] = self ::normalizeType ($ type ->getName (), $ reflection );
120+ }
121+ return $ types ;
122+ }
82123 throw new Nette \InvalidStateException ('The ' . self ::toString ($ reflection ) . ' is not expected to have a union type. ' );
83124
84125 } else {
0 commit comments