@@ -115,6 +115,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
115115 )
116116 );
117117
118+ $ rawCallReturnTypes = [];
118119 $ operationCalls = [];
119120 $ callReturnTypes = [];
120121
@@ -129,10 +130,10 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
129130 $ fallbackName = 'Operation \\' . $ operationGroup . '\\Response \\' . (new Convert (str_replace ('/ ' , '\\' , $ contentType ) . '\\H ' . $ code ))->toPascal ();
130131 $ object = '\\' . $ namespace . 'Schema \\' . $ schemaRegistry ->get ($ contentTypeSchema ->schema , $ fallbackName );
131132 $ callReturnTypes [] = ($ contentTypeSchema ->schema ->type === 'array ' ? '\\' . Observable::class . '< ' : '' ) . $ object . ($ contentTypeSchema ->schema ->type === 'array ' ? '> ' : '' );
132- $ contentTypeCases [] = $ returnType [] = $ contentTypeSchema ->schema ->type === 'array ' ? '\\' . Observable::class : $ object ;
133+ $ rawCallReturnTypes [] = $ contentTypeCases [] = $ returnType [] = $ contentTypeSchema ->schema ->type === 'array ' ? '\\' . Observable::class : $ object ;
133134 }
134135 if (count ($ contentTypeCases ) === 0 ) {
135- $ returnType [] = $ callReturnTypes [] = 'int ' ;
136+ $ rawCallReturnTypes [] = $ returnType [] = $ callReturnTypes [] = 'int ' ;
136137 }
137138 }
138139 $ operationCalls [] = [
@@ -175,7 +176,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
175176 }
176177
177178 $ class ->addStmt (
178- $ factory ->method ('call ' )->makePublic ()->setReturnType (
179+ $ factory ->method ('callAsync ' )->makePublic ()->setReturnType (
179180 new Node \Name ('\\' . PromiseInterface::class)
180181 )->setDocComment (
181182 new Doc (implode (PHP_EOL , [
@@ -340,6 +341,46 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
340341 ))
341342 );
342343
344+ $ class ->addStmt (
345+ $ factory ->method ('call ' )->makePublic ()->setDocComment (
346+ new Doc (implode (PHP_EOL , [
347+ '/** ' ,
348+ ' * @return ' . (function (array $ operationCalls ): string {
349+ $ count = count ($ operationCalls );
350+ $ lastItem = $ count - 1 ;
351+ $ left = '' ;
352+ $ right = '' ;
353+ for ($ i = 0 ; $ i < $ count ; $ i ++) {
354+ if ($ i !== $ lastItem ) {
355+ $ left .= '($call is ' . $ operationCalls [$ i ]['className ' ] . '::OPERATION_MATCH ? ' . implode ('| ' , array_unique ($ operationCalls [$ i ]['returnType ' ])) . ' : ' ;
356+ } else {
357+ $ left .= implode ('| ' , array_unique ($ operationCalls [$ i ]['returnType ' ]));
358+ }
359+ $ right .= ') ' ;
360+ }
361+ return $ left . $ right ;
362+ })($ operationCalls ),
363+ ' */ ' ,
364+ ]))
365+ )->setReturnType (implode ('| ' , array_unique ($ rawCallReturnTypes )))->addParam ((new Param ('call ' ))->setType ('string ' ))->addParam ((new Param ('params ' ))->setType ('array ' )->setDefault ([]))->addStmt (new Node \Stmt \Return_ (
366+ new Node \Expr \FuncCall (
367+ new Node \Name ('\React\Async\await ' ),
368+ [
369+ new Node \Arg (
370+ new Node \Expr \MethodCall (
371+ new Node \Expr \Variable ('this ' ),
372+ new Node \Name ('callAsync ' ),
373+ [
374+ new Node \Arg (new Node \Expr \Variable ('call ' )),
375+ new Node \Arg (new Node \Expr \Variable ('params ' )),
376+ ]
377+ )
378+ ),
379+ ],
380+ )
381+ ))
382+ );
383+
343384 yield new File ($ namespace . '\\' . 'Client ' , $ stmt ->addStmt ($ class )->getNode ());
344385 }
345386}
0 commit comments