1111use PhpParser \Node \Stmt \ClassMethod ;
1212use PhpParser \Node \Stmt \Namespace_ ;
1313use PhpParser \Node \Stmt \Use_ ;
14+ use PhpParser \Node \UnionType ;
1415use PhpParser \ParserFactory ;
1516
1617/**
@@ -74,15 +75,14 @@ public function getMethods(): array
7475 protected function processStatements (array $ statements , string $ prefix = '' )
7576 {
7677 $ uses = [];
77-
7878 foreach ($ statements as $ statement ) {
7979 if ($ statement instanceof Namespace_) {
8080 return $ this ->processStatements ($ statement ->stmts , (string )$ statement ->name );
8181 }
8282
8383 if ($ statement instanceof Use_) {
8484 foreach ($ statement ->uses as $ use ) {
85- $ uses [(string ) $ use ->alias ] = (string )$ use ->name ;
85+ $ uses [(string )$ use ->alias ] = (string )$ use ->name ;
8686 }
8787 }
8888
@@ -102,26 +102,37 @@ protected function processStatements(array $statements, string $prefix = '')
102102 continue ;
103103 }
104104
105- $ fullMethodName = $ fullClassName . ':: ' . ( string ) $ method ->name ;
105+ $ fullMethodName = $ fullClassName . ':: ' . $ method ->name ;
106106
107107 $ returnType = $ method ->returnType ;
108-
109- if (!$ method ->returnType instanceof NullableType) {
108+ if ($ method ->returnType instanceof NullableType) {
109+ $ returnType = [$ returnType ->type , 'null ' ];
110+ } elseif ($ method ->returnType instanceof UnionType) {
111+ $ returnType = $ returnType ->types ;
112+ } else {
110113 if (!\is_null ($ returnType )) {
111114 $ returnType = (string )$ returnType ;
112115 }
113- } else {
114- $ returnType = (string )$ returnType ->type ;
115116 }
116117
117- if (isset ($ uses [$ returnType ])) {
118- $ returnType = $ uses [$ returnType ];
119- }
118+ $ returnType = (array )$ returnType ;
119+ foreach ($ returnType as &$ returnTypeItem ) {
120+ if (isset ($ uses [(string )$ returnTypeItem ])) {
121+ $ returnTypeItem = $ uses [(string )$ returnTypeItem ];
122+ }
120123
121- $ returnType = \substr ((string )$ returnType , 0 , 1 ) === '\\' ? \substr ((string )$ returnType , 1 ) : $ returnType ;
124+ $ returnTypeItem = \substr ((string )$ returnTypeItem , 0 , 1 ) === '\\'
125+ ? \substr ((string )$ returnTypeItem , 1 )
126+ : $ returnTypeItem ;
122127
123- if ($ method ->returnType instanceof NullableType) {
124- $ returnType = [$ returnType , 'null ' ];
128+ $ returnTypeItem = (null !== $ returnTypeItem )
129+ ? (string )$ returnTypeItem
130+ : null ;
131+ }
132+ unset($ returnTypeItem );
133+
134+ if (1 === \count ($ returnType )) {
135+ $ returnType = (string )$ returnType [0 ];
125136 }
126137
127138 $ thisMethod = [
@@ -136,32 +147,40 @@ protected function processStatements(array $statements, string $prefix = '')
136147
137148 foreach ($ method ->params as $ param ) {
138149 $ paramType = $ param ->type ;
139-
140- if (!$ param ->type instanceof NullableType) {
150+ if ($ param ->type instanceof NullableType) {
151+ $ paramType = [$ paramType ->type , 'null ' ];
152+ } elseif (
153+ !empty ($ param ->default ->name ->parts [0 ]) &&
154+ 'null ' === $ param ->default ->name ->parts [0 ]
155+ ) {
141156 if (!\is_null ($ param ->type )) {
142- $ paramType = (string )$ paramType ;
157+ $ paramType = [$ paramType , 'null ' ];
158+ } else {
159+ $ paramType = ['<any> ' , 'null ' ];
143160 }
161+ } elseif ($ param ->type instanceof UnionType) {
162+ $ paramType = $ paramType ->types ;
144163 } else {
145- $ paramType = (string )$ paramType ->type ;
164+ if (!\is_null ($ paramType )) {
165+ $ paramType = (string )$ paramType ;
166+ }
146167 }
147168
148- if (isset ($ uses [$ paramType ])) {
149- $ paramType = $ uses [$ paramType ];
150- }
169+ $ paramType = (array )$ paramType ;
170+ foreach ($ paramType as &$ paramTypeItem ) {
171+ if (isset ($ uses [(string )$ paramTypeItem ])) {
172+ $ paramTypeItem = $ uses [(string )$ paramTypeItem ];
173+ }
151174
152- $ paramType = \substr ((string )$ paramType , 0 , 1 ) === '\\' ? \substr ((string )$ paramType , 1 ) : $ paramType ;
175+ $ paramTypeItem = \substr ((string )$ paramTypeItem , 0 , 1 ) === '\\'
176+ ? \substr ((string )$ paramTypeItem , 1 )
177+ : $ paramTypeItem ;
153178
154- if (
155- $ param ->type instanceof NullableType
156- ) {
157- $ paramType = [$ paramType , 'null ' ];
158- } elseif (!empty ($ param ->default ->name ->parts [0 ]) && 'null ' === $ param ->default ->name ->parts [0 ]) {
159- if (!\is_null ($ param ->type )) {
160- $ paramType = [$ paramType , 'null ' ];
161- } else {
162- $ paramType = ['<any> ' , 'null ' ];
163- }
179+ $ paramTypeItem = (null !== $ paramTypeItem )
180+ ? (string )$ paramTypeItem
181+ : null ;
164182 }
183+ unset($ paramTypeItem );
165184
166185 $ thisMethod ['params ' ]['$ ' .$ param ->var ->name ] = $ paramType ;
167186 }
0 commit comments