Skip to content

Commit 42f73dd

Browse files
authored
Merge pull request #80 from php-api-clients/drop-return-types-of-call-method
Drop return types of call* methods and fully rely on conditional return typing
2 parents 2de56d4 + faed644 commit 42f73dd

File tree

2 files changed

+73
-51
lines changed

2 files changed

+73
-51
lines changed

src/Generator/Client.php

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,64 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
176176
}
177177

178178
$class->addStmt(
179-
$factory->method('callAsync')->makePublic()->setReturnType(
180-
new Node\Name('\\' . PromiseInterface::class)
181-
)->setDocComment(
179+
$factory->method('call')->makePublic()->setDocComment(
182180
new Doc(implode(PHP_EOL, [
183181
'/**',
184-
' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>',
182+
' * @return ' . (function (array $operationCalls): string {
183+
$count = count($operationCalls);
184+
$lastItem = $count - 1;
185+
$left = '';
186+
$right = '';
187+
for ($i = 0; $i < $count; $i++) {
188+
if ($i !== $lastItem) {
189+
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? ' . implode('|', array_unique($operationCalls[$i]['returnType'])) . ' : ';
190+
} else {
191+
$left .= implode('|', array_unique($operationCalls[$i]['returnType']));
192+
}
193+
$right .= ')';
194+
}
195+
return $left . $right;
196+
})($operationCalls),
197+
' */',
198+
]))
199+
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->addStmt(new Node\Stmt\Return_(
200+
new Node\Expr\FuncCall(
201+
new Node\Name('\React\Async\await'),
202+
[
203+
new Node\Arg(
204+
new Node\Expr\MethodCall(
205+
new Node\Expr\Variable('this'),
206+
new Node\Name('callAsync'),
207+
[
208+
new Node\Arg(new Node\Expr\Variable('call')),
209+
new Node\Arg(new Node\Expr\Variable('params')),
210+
]
211+
)
212+
),
213+
],
214+
)
215+
))
216+
);
217+
218+
$class->addStmt(
219+
$factory->method('callAsync')->makePublic()->setDocComment(
220+
new Doc(implode(PHP_EOL, [
221+
'/**',
222+
' * @return ' . (function (array $operationCalls): string {
223+
$count = count($operationCalls);
224+
$lastItem = $count - 1;
225+
$left = '';
226+
$right = '';
227+
for ($i = 0; $i < $count; $i++) {
228+
if ($i !== $lastItem) {
229+
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : ';
230+
} else {
231+
$left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>';
232+
}
233+
$right .= ')';
234+
}
235+
return $left . $right;
236+
})($operationCalls),
185237
' */',
186238
]))
187239
)->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
341393
))
342394
);
343395

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-
384396
yield new File($namespace . '\\' . 'Client', $stmt->addStmt($class)->getNode());
385397
}
386398
}

src/Generator/ClientInterface.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
7474
}
7575

7676
$class->addStmt(
77-
$factory->method('call')->makePublic()->setReturnType(
78-
new Node\Name(implode('|', array_unique($rawCallReturnTypes)))
79-
)->setDocComment(
77+
$factory->method('call')->makePublic()->setDocComment(
8078
new Doc(implode(PHP_EOL, [
8179
'/**',
8280
' * @return ' . (function (array $operationCalls): string {
@@ -100,12 +98,24 @@ public static function generate(string $namespace, array $clients, SchemaRegistr
10098
);
10199

102100
$class->addStmt(
103-
$factory->method('callAsync')->makePublic()->setReturnType(
104-
new Node\Name('\\' . PromiseInterface::class)
105-
)->setDocComment(
101+
$factory->method('callAsync')->makePublic()->setDocComment(
106102
new Doc(implode(PHP_EOL, [
107103
'/**',
108-
' * @return \\' . PromiseInterface::class . '<' . implode('|', array_unique($callReturnTypes)) . '>',
104+
' * @return ' . (function (array $operationCalls): string {
105+
$count = count($operationCalls);
106+
$lastItem = $count - 1;
107+
$left = '';
108+
$right = '';
109+
for ($i = 0; $i < $count; $i++) {
110+
if ($i !== $lastItem) {
111+
$left .= '($call is ' . $operationCalls[$i]['className'] . '::OPERATION_MATCH ? \\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '> : ';
112+
} else {
113+
$left .= '\\' . PromiseInterface::class . '<' . implode('|', array_unique($operationCalls[$i]['returnType'])) . '>';
114+
}
115+
$right .= ')';
116+
}
117+
return $left . $right;
118+
})($operationCalls),
109119
' */',
110120
]))
111121
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))

0 commit comments

Comments
 (0)