55use PhpParser \Comment \Doc ;
66use PhpParser \Node \Stmt ;
77use PhpParser \Node \Stmt \Class_ ;
8+ use PhpParser \Node \NullableType ;
89use PhpParser \Node \Stmt \ClassMethod ;
910use PhpParser \Node \Stmt \Namespace_ ;
1011use PhpParser \Node \Stmt \Use_ ;
@@ -93,8 +94,12 @@ protected function processStatements(array $statements, $prefix = '')
9394
9495 $ type = $ method ->returnType ;
9596
96- if (!is_null ($ type )) {
97- $ type = (string )$ type ;
97+ if (!$ method ->returnType instanceof NullableType) {
98+ if (!is_null ($ type )) {
99+ $ type = (string )$ type ;
100+ }
101+ } else {
102+ $ type = (string )$ type ->type ;
98103 }
99104
100105 if (isset ($ uses [$ type ])) {
@@ -103,6 +108,10 @@ protected function processStatements(array $statements, $prefix = '')
103108
104109 $ type = substr ($ type , 0 , 1 ) == '\\' ? substr ($ type , 1 ) : $ type ;
105110
111+ if ($ method ->returnType instanceof NullableType) {
112+ $ type = ['null ' , $ type ];
113+ sort ($ type );
114+ }
106115
107116 $ thisMethod = [
108117 'file ' => $ this ->file ,
@@ -117,8 +126,12 @@ protected function processStatements(array $statements, $prefix = '')
117126 foreach ($ method ->params as $ param ) {
118127 $ type = $ param ->type ;
119128
120- if (!is_null ($ type )) {
121- $ type = (string )$ type ;
129+ if (!$ type instanceof NullableType) {
130+ if (!is_null ($ type )) {
131+ $ type = (string )$ type ;
132+ }
133+ } else {
134+ $ type = (string )$ type ->type ;
122135 }
123136
124137 if (isset ($ uses [$ type ])) {
@@ -177,13 +190,14 @@ protected function processDocblock($text, array $uses = [])
177190 $ type = (string )$ type ;
178191 }
179192
180- if (isset ($ uses [$ type ])) {
181- $ type = $ uses [$ type ];
193+ $ types = [];
194+ foreach (explode ('| ' , $ type ) as $ tmpType ) {
195+ if (isset ($ uses [$ tmpType ])) {
196+ $ tmpType = $ uses [$ tmpType ];
197+ }
198+ $ types [] = substr ($ tmpType , 0 , 1 ) == '\\' ? substr ($ tmpType , 1 ) : $ tmpType ;
182199 }
183-
184- $ type = substr ($ type , 0 , 1 ) == '\\' ? substr ($ type , 1 ) : $ type ;
185-
186- $ rtn ['params ' ][$ param ['var ' ]] = $ type ;
200+ $ rtn ['params ' ][$ param ['var ' ]] = implode ('| ' , $ types );
187201 }
188202 }
189203
@@ -196,13 +210,14 @@ protected function processDocblock($text, array $uses = [])
196210 $ type = (string )$ type ;
197211 }
198212
199- if (isset ($ uses [$ type ])) {
200- $ type = $ uses [$ type ];
213+ $ types = [];
214+ foreach (explode ('| ' , $ type ) as $ tmpType ) {
215+ if (isset ($ uses [$ tmpType ])) {
216+ $ tmpType = $ uses [$ tmpType ];
217+ }
218+ $ types [] = substr ($ tmpType , 0 , 1 ) == '\\' ? substr ($ tmpType , 1 ) : $ tmpType ;
201219 }
202-
203- $ type = substr ($ type , 0 , 1 ) == '\\' ? substr ($ type , 1 ) : $ type ;
204-
205- $ rtn ['return ' ] = $ type ;
220+ $ rtn ['return ' ] = implode ('| ' , $ types );
206221 }
207222
208223 return $ rtn ;
0 commit comments