@@ -145,48 +145,9 @@ private function defineReturnType(ReflectionMethod $method): ?Node
145145 return null ;
146146 }
147147
148- $ returnType = $ method ->getReturnType ();
148+ $ name = $ this -> defineType ( $ method, $ method ->getReturnType () );
149149
150- if ($ returnType === null ) {
151- return null ;
152- }
153-
154- $ name = $ returnType ->getName ();
155- $ name = $ this ->replacedSelfReturnTypeName ($ method , $ name );
156-
157- if ($ this ->returnTypeShouldBeQualified ($ returnType , $ name )) {
158- $ name = '\\' . $ name ;
159- }
160-
161- if ($ returnType ->allowsNull ()) {
162- $ name = '? ' . $ name ;
163- }
164-
165- return new Node \Identifier ($ name );
166- }
167-
168- /**
169- * @param ReflectionMethod $method
170- * @param string $name
171- * @return string
172- */
173- private function replacedSelfReturnTypeName (ReflectionMethod $ method , string $ name ): string
174- {
175- return $ name === 'self ' ? $ method ->class : $ name ;
176- }
177-
178- /**
179- * @param ReflectionType $returnType
180- * @param string $name
181- * @return bool
182- */
183- private function returnTypeShouldBeQualified (ReflectionType $ returnType , string $ name ): bool
184- {
185- if (in_array ($ name , self ::RESERVED_UNQUALIFIED_RETURN_TYPES , true )) {
186- return false ;
187- }
188-
189- return !$ returnType ->isBuiltin ();
150+ return $ name !== null ? new Node \Identifier ($ name ) : null ;
190151 }
191152
192153 /**
@@ -212,7 +173,7 @@ private function packParams(ReflectionMethod $method): array
212173 $ param ->makeByRef ();
213174 }
214175
215- $ type = $ this ->defineParamReturnType ($ parameter );
176+ $ type = $ this ->defineParamType ($ parameter, $ method );
216177 if ($ type !== null ) {
217178 $ param ->setType ($ type );
218179 }
@@ -225,25 +186,65 @@ private function packParams(ReflectionMethod $method): array
225186
226187 /**
227188 * @param ReflectionParameter $parameter
189+ * @param ReflectionMethod $method
228190 * @return string|null
229191 */
230- private function defineParamReturnType (ReflectionParameter $ parameter ): ?string
192+ private function defineParamType (ReflectionParameter $ parameter, ReflectionMethod $ method ): ?string
231193 {
232194 if (!$ parameter ->hasType ()) {
233195 return null ;
234196 }
235197
236- $ typeReflection = $ parameter ->getType ();
237- if ($ typeReflection === null ) {
198+ return $ this ->defineType ($ method , $ parameter ->getType ());
199+ }
200+
201+ /**
202+ * @param ReflectionMethod $method
203+ * @param ReflectionType|null $type
204+ * @return string|null
205+ */
206+ private function defineType (ReflectionMethod $ method , ?ReflectionType $ type ): ?string
207+ {
208+ if ($ type === null ) {
238209 return null ;
239210 }
240211
241- $ type = $ typeReflection ->getName ();
242- if ($ typeReflection ->allowsNull ()) {
243- $ type = "? $ type " ;
212+ $ name = $ type ->getName ();
213+ $ name = $ this ->replacedSelfTypeName ($ method , $ name );
214+
215+ if ($ this ->typeShouldBeQualified ($ type , $ name )) {
216+ $ name = '\\' . $ name ;
217+ }
218+
219+ if ($ type ->allowsNull ()) {
220+ $ name = "? $ name " ;
244221 }
245222
246- return $ type ;
223+ return $ name ;
224+ }
225+
226+ /**
227+ * @param ReflectionMethod $method
228+ * @param string $name
229+ * @return string
230+ */
231+ private function replacedSelfTypeName (ReflectionMethod $ method , string $ name ): string
232+ {
233+ return $ name === 'self ' ? $ method ->class : $ name ;
234+ }
235+
236+ /**
237+ * @param ReflectionType $returnType
238+ * @param string $name
239+ * @return bool
240+ */
241+ private function typeShouldBeQualified (ReflectionType $ returnType , string $ name ): bool
242+ {
243+ if (in_array ($ name , self ::RESERVED_UNQUALIFIED_RETURN_TYPES , true )) {
244+ return false ;
245+ }
246+
247+ return !$ returnType ->isBuiltin ();
247248 }
248249
249250 /**
0 commit comments