From faed64481021e43c225195d3ee1d8903f51c50b8 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 2 Feb 2023 08:45:26 +0100 Subject: [PATCH] Drop return types of call* methods and fully rely on conditional return typing --- src/Generator/Client.php | 100 +++++++++++++++++------------- src/Generator/ClientInterface.php | 24 ++++--- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/src/Generator/Client.php b/src/Generator/Client.php index 0bc966d..fa29cb0 100644 --- a/src/Generator/Client.php +++ b/src/Generator/Client.php @@ -176,12 +176,64 @@ public static function generate(string $namespace, array $clients, SchemaRegistr } $class->addStmt( - $factory->method('callAsync')->makePublic()->setReturnType( - new Node\Name('\\' . PromiseInterface::class) - )->setDocComment( + $factory->method('call')->makePublic()->setDocComment( new Doc(implode(PHP_EOL, [ '/**', - ' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>', + ' * @return ' . (function (array $operationCalls): string { + $count = count($operationCalls); + $lastItem = $count - 1; + $left = ''; + $right = ''; + for ($i = 0; $i < $count; $i++) { + if ($i !== $lastItem) { + $left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? ' . implode('|', array_unique($operationCalls[$i]['returnType'])) . ' : '; + } else { + $left .= implode('|', array_unique($operationCalls[$i]['returnType'])); + } + $right .= ')'; + } + return $left . $right; + })($operationCalls), + ' */', + ])) + )->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_( + new Node\Expr\FuncCall( + new Node\Name('\React\Async\await'), + [ + new Node\Arg( + new Node\Expr\MethodCall( + new Node\Expr\Variable('this'), + new Node\Name('callAsync'), + [ + new Node\Arg(new Node\Expr\Variable('call')), + new Node\Arg(new Node\Expr\Variable('params')), + ] + ) + ), + ], + ) + )) + ); + + $class->addStmt( + $factory->method('callAsync')->makePublic()->setDocComment( + new Doc(implode(PHP_EOL, [ + '/**', + ' * @return ' . (function (array $operationCalls): string { + $count = count($operationCalls); + $lastItem = $count - 1; + $left = ''; + $right = ''; + for ($i = 0; $i < $count; $i++) { + if ($i !== $lastItem) { + $left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : '; + } else { + $left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>'; + } + $right .= ')'; + } + return $left . $right; + })($operationCalls), ' */', ])) )->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Switch_( @@ -341,46 +393,6 @@ public static function generate(string $namespace, array $clients, SchemaRegistr )) ); - $class->addStmt( - $factory->method('call')->makePublic()->setDocComment( - new Doc(implode(PHP_EOL, [ - '/**', - ' * @return ' . (function (array $operationCalls): string { - $count = count($operationCalls); - $lastItem = $count - 1; - $left = ''; - $right = ''; - for ($i = 0; $i < $count; $i++) { - if ($i !== $lastItem) { - $left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? ' . implode('|', array_unique($operationCalls[$i]['returnType'])) . ' : '; - } else { - $left .= implode('|', array_unique($operationCalls[$i]['returnType'])); - } - $right .= ')'; - } - return $left . $right; - })($operationCalls), - ' */', - ])) - )->setReturnType(implode('|', array_unique($rawCallReturnTypes)))->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_( - new Node\Expr\FuncCall( - new Node\Name('\React\Async\await'), - [ - new Node\Arg( - new Node\Expr\MethodCall( - new Node\Expr\Variable('this'), - new Node\Name('callAsync'), - [ - new Node\Arg(new Node\Expr\Variable('call')), - new Node\Arg(new Node\Expr\Variable('params')), - ] - ) - ), - ], - ) - )) - ); - yield new File($namespace . '\\' . 'Client', $stmt->addStmt($class)->getNode()); } } diff --git a/src/Generator/ClientInterface.php b/src/Generator/ClientInterface.php index 93a2ea5..602979b 100644 --- a/src/Generator/ClientInterface.php +++ b/src/Generator/ClientInterface.php @@ -74,9 +74,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr } $class->addStmt( - $factory->method('call')->makePublic()->setReturnType( - new Node\Name(implode('|', array_unique($rawCallReturnTypes))) - )->setDocComment( + $factory->method('call')->makePublic()->setDocComment( new Doc(implode(PHP_EOL, [ '/**', ' * @return ' . (function (array $operationCalls): string { @@ -100,12 +98,24 @@ public static function generate(string $namespace, array $clients, SchemaRegistr ); $class->addStmt( - $factory->method('callAsync')->makePublic()->setReturnType( - new Node\Name('\\' . PromiseInterface::class) - )->setDocComment( + $factory->method('callAsync')->makePublic()->setDocComment( new Doc(implode(PHP_EOL, [ '/**', - ' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>', + ' * @return ' . (function (array $operationCalls): string { + $count = count($operationCalls); + $lastItem = $count - 1; + $left = ''; + $right = ''; + for ($i = 0; $i < $count; $i++) { + if ($i !== $lastItem) { + $left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : '; + } else { + $left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>'; + } + $right .= ')'; + } + return $left . $right; + })($operationCalls), ' */', ])) )->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))