Skip to content

Commit 705fb76

Browse files
authored
Merge pull request #200 from php-api-clients/introduce-central-operators-object
Introduce central operators object
2 parents 2fb655b + 884721b commit 705fb76

29 files changed

+455
-363
lines changed

src/Gatherer/Operation.php

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ public static function gather(
156156
ClassString::factory($baseNamespace, 'Operation\\' . Utils::fixKeyword($className)),
157157
ClassString::factory($baseNamespace, $classNameSanitized),
158158
ClassString::factory($baseNamespace, 'Operator\\' . Utils::fixKeyword($className)),
159+
lcfirst(
160+
str_replace(
161+
['\\'],
162+
['👷'],
163+
ClassString::factory($baseNamespace, Utils::fixKeyword($className))->relative,
164+
),
165+
),
159166
$name,
160167
(new Convert($name))->toCamel(),
161168
$group,

src/Generator/Client.php

+64-62
Original file line numberDiff line numberDiff line change
@@ -158,39 +158,46 @@ public static function generate(Configuration $configuration, string $pathPrefix
158158
new Node\Name('Operations'),
159159
[
160160
new Arg(
161-
new Node\Expr\Variable('browser'),
162-
false,
163-
false,
164-
[],
165-
new Node\Identifier('browser'),
166-
),
167-
new Arg(
168-
new Node\Expr\Variable('authentication'),
169-
false,
170-
false,
171-
[],
172-
new Node\Identifier('authentication'),
173-
),
174-
new Arg(
175-
new Node\Expr\Variable('requestSchemaValidator'),
176-
false,
177-
false,
178-
[],
179-
new Node\Identifier('requestSchemaValidator'),
180-
),
181-
new Arg(
182-
new Node\Expr\Variable('responseSchemaValidator'),
183-
false,
184-
false,
185-
[],
186-
new Node\Identifier('responseSchemaValidator'),
187-
),
188-
new Arg(
189-
new Node\Expr\Variable('hydrators'),
190-
false,
191-
false,
192-
[],
193-
new Node\Identifier('hydrators'),
161+
new Node\Expr\New_(
162+
new Node\Name('Operators'),
163+
[
164+
new Arg(
165+
new Node\Expr\Variable('browser'),
166+
false,
167+
false,
168+
[],
169+
new Node\Identifier('browser'),
170+
),
171+
new Arg(
172+
new Node\Expr\Variable('authentication'),
173+
false,
174+
false,
175+
[],
176+
new Node\Identifier('authentication'),
177+
),
178+
new Arg(
179+
new Node\Expr\Variable('requestSchemaValidator'),
180+
false,
181+
false,
182+
[],
183+
new Node\Identifier('requestSchemaValidator'),
184+
),
185+
new Arg(
186+
new Node\Expr\Variable('responseSchemaValidator'),
187+
false,
188+
false,
189+
[],
190+
new Node\Identifier('responseSchemaValidator'),
191+
),
192+
new Arg(
193+
new Node\Expr\Variable('hydrators'),
194+
false,
195+
false,
196+
[],
197+
new Node\Identifier('hydrators'),
198+
),
199+
],
200+
),
194201
),
195202
],
196203
),
@@ -206,14 +213,20 @@ public static function generate(Configuration $configuration, string $pathPrefix
206213
new Node\Expr\New_(
207214
new Node\Name('WebHooks'),
208215
[
209-
new Node\Arg(new Node\Expr\PropertyFetch(
210-
new Node\Expr\Variable('this'),
211-
'requestSchemaValidator',
212-
)),
213-
new Node\Arg(new Node\Expr\PropertyFetch(
214-
new Node\Expr\Variable('this'),
215-
'hydrators',
216-
)),
216+
new Arg(
217+
new Node\Expr\Variable('requestSchemaValidator'),
218+
false,
219+
false,
220+
[],
221+
new Node\Identifier('requestSchemaValidator'),
222+
),
223+
new Arg(
224+
new Node\Expr\Variable('hydrators'),
225+
false,
226+
false,
227+
[],
228+
new Node\Identifier('hydrator'),
229+
),
217230
],
218231
),
219232
),
@@ -229,50 +242,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
229242
new Node\Name('Routers'),
230243
[
231244
new Arg(
232-
new Node\Expr\PropertyFetch(
233-
new Node\Expr\Variable('this'),
234-
'browser',
235-
),
245+
new Node\Expr\Variable('browser'),
236246
false,
237247
false,
238248
[],
239249
new Node\Identifier('browser'),
240250
),
241251
new Arg(
242-
new Node\Expr\PropertyFetch(
243-
new Node\Expr\Variable('this'),
244-
'authentication',
245-
),
252+
new Node\Expr\Variable('authentication'),
246253
false,
247254
false,
248255
[],
249256
new Node\Identifier('authentication'),
250257
),
251258
new Arg(
252-
new Node\Expr\PropertyFetch(
253-
new Node\Expr\Variable('this'),
254-
'requestSchemaValidator',
255-
),
259+
new Node\Expr\Variable('requestSchemaValidator'),
256260
false,
257261
false,
258262
[],
259263
new Node\Identifier('requestSchemaValidator'),
260264
),
261265
new Arg(
262-
new Node\Expr\PropertyFetch(
263-
new Node\Expr\Variable('this'),
264-
'responseSchemaValidator',
265-
),
266+
new Node\Expr\Variable('responseSchemaValidator'),
266267
false,
267268
false,
268269
[],
269270
new Node\Identifier('responseSchemaValidator'),
270271
),
271272
new Arg(
272-
new Node\Expr\PropertyFetch(
273-
new Node\Expr\Variable('this'),
274-
'hydrators',
275-
),
273+
new Node\Expr\Variable('hydrators'),
276274
false,
277275
false,
278276
[],
@@ -695,7 +693,11 @@ private static function traverseOperationPaths(array $operations, array &$operat
695693
return $operations;
696694
}
697695

698-
/** @param array<Representation\Path> $paths */
696+
/**
697+
* @param array<Representation\Path> $paths
698+
*
699+
* @return iterable<Representation\Path>
700+
*/
699701
private static function operationsInThisThree(array $paths, int $level, Routers $routers): iterable
700702
{
701703
foreach ($paths as $path) {

src/Generator/Helper/Operation.php

+23-93
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace ApiClients\Tools\OpenApiClientGenerator\Generator\Helper;
66

77
use ApiClients\Tools\OpenApiClientGenerator\Representation;
8-
use ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator;
98
use PhpParser\Builder;
109
use PhpParser\Comment\Doc;
1110
use PhpParser\Node;
@@ -27,7 +26,6 @@
2726
use function is_string;
2827
use function str_replace;
2928
use function strpos;
30-
use function ucfirst;
3129

3230
use const PHP_EOL;
3331

@@ -54,107 +52,39 @@ public static function methodParams(Builder\Method $method, Representation\Opera
5452

5553
public static function methodReturnType(Builder\Method $method, Representation\Operation $operation): Builder\Method
5654
{
55+
$docComment = ReflectionTypes::copyDocBlock($operation->operatorClassName->fullyQualified->source, 'call');
56+
57+
if ($docComment !== null) {
58+
$method = $method->setDocComment($docComment);
59+
}
60+
5761
return $method->setReturnType(
58-
new Node\UnionType(
59-
Types::normalizeNodeName(...$operation->returnType),
60-
),
62+
ReflectionTypes::copyReturnType($operation->operatorClassName->fullyQualified->source, 'call'),
6163
);
6264
}
6365

64-
/**
65-
* @param array<string, Hydrator> $operationHydratorMap
66-
*
67-
* @return array<Node>
68-
*/
69-
public static function methodCallOperation(Representation\Operation $operation, array $operationHydratorMap): array
66+
public static function methodCallOperation(Representation\Operation $operation): Node\Stmt\Return_
7067
{
71-
return [
72-
new Node\Stmt\If_(
73-
new Node\Expr\BinaryOp\Equal(
74-
new Node\Expr\FuncCall(
75-
new Node\Name('\array_key_exists'),
76-
[
77-
new Arg(new Node\Expr\ClassConstFetch(
78-
new Node\Name($operation->operatorClassName->relative),
79-
'class',
80-
)),
81-
new Arg(new Node\Expr\PropertyFetch(
82-
new Node\Expr\Variable('this'),
83-
'operator',
84-
)),
85-
],
68+
return new Node\Stmt\Return_(
69+
new Expr\MethodCall(
70+
new Expr\MethodCall(
71+
new Node\Expr\PropertyFetch(
72+
new Node\Expr\Variable('this'),
73+
'operators',
8674
),
87-
new Node\Expr\ConstFetch(new Node\Name('false')),
75+
$operation->operatorLookUpMethod,
8876
),
77+
'call',
8978
[
90-
'stmts' => [
91-
new Node\Stmt\Expression(
92-
new Node\Expr\Assign(
93-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
94-
new Node\Expr\Variable('this'),
95-
'operator',
96-
), new Node\Expr\ClassConstFetch(
97-
new Node\Name($operation->operatorClassName->relative),
98-
'class',
99-
)),
100-
new Node\Expr\New_(
101-
new Node\Name($operation->operatorClassName->relative),
102-
[
103-
new Arg(new Node\Expr\PropertyFetch(
104-
new Node\Expr\Variable('this'),
105-
'browser',
106-
)),
107-
new Arg(new Node\Expr\PropertyFetch(
108-
new Node\Expr\Variable('this'),
109-
'authentication',
110-
)),
111-
...(count($operation->requestBody) > 0 ? [
112-
new Arg(new Node\Expr\PropertyFetch(
113-
new Node\Expr\Variable('this'),
114-
'requestSchemaValidator',
115-
)),
116-
] : []),
117-
new Arg(new Node\Expr\PropertyFetch(
118-
new Node\Expr\Variable('this'),
119-
'responseSchemaValidator',
120-
)),
121-
new Arg(
122-
new Expr\MethodCall(
123-
new Node\Expr\PropertyFetch(
124-
new Node\Expr\Variable('this'),
125-
'hydrators',
126-
),
127-
'getObjectMapper' . ucfirst($operationHydratorMap[$operation->operationId]->methodName),
128-
),
129-
),
130-
],
131-
),
132-
),
133-
),
134-
],
79+
...(static function (array $params): iterable {
80+
foreach ($params as $param) {
81+
yield new Arg(new Node\Expr\Variable($param->name));
82+
}
83+
})($operation->parameters),
84+
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
13585
],
13686
),
137-
new Node\Stmt\Return_(
138-
new Expr\MethodCall(
139-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
140-
new Node\Expr\Variable('this'),
141-
'operator',
142-
), new Node\Expr\ClassConstFetch(
143-
new Node\Name($operation->operatorClassName->relative),
144-
'class',
145-
)),
146-
'call',
147-
[
148-
...(static function (array $params): iterable {
149-
foreach ($params as $param) {
150-
yield new Arg(new Node\Expr\Variable($param->name));
151-
}
152-
})($operation->parameters),
153-
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
154-
],
155-
),
156-
),
157-
];
87+
);
15888
}
15989

16090
public static function getResultTypeFromOperation(Representation\Operation $operation): string
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Tools\OpenApiClientGenerator\Generator\Helper;
6+
7+
use PhpParser\Comment\Doc;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Name;
10+
use ReflectionClass;
11+
use ReflectionNamedType;
12+
use ReflectionType;
13+
use ReflectionUnionType;
14+
15+
use function strpos;
16+
17+
final class ReflectionTypes
18+
{
19+
public static function copyReturnType(string $class, string $method): Node\ComplexType|Name|string
20+
{
21+
$reflection = (new ReflectionClass($class))->getMethod($method)->getReturnType();
22+
switch ($reflection::class) {
23+
//ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType
24+
case ReflectionNamedType::class:
25+
return new Name(
26+
(strpos((string) $reflection, '\\') !== false ? '\\' : '') . $reflection,
27+
);
28+
29+
break;
30+
case ReflectionUnionType::class:
31+
return new Node\UnionType(
32+
[
33+
...(static function (ReflectionType ...$types): iterable {
34+
foreach ($types as $type) {
35+
yield new Name(
36+
(strpos((string) $type, '\\') !== false ? '\\' : '') . $type,
37+
);
38+
}
39+
})(...$reflection->getTypes()),
40+
],
41+
);
42+
43+
break;
44+
default:
45+
return '';
46+
}
47+
}
48+
49+
public static function copyDocBlock(string $class, string $method): Doc|null
50+
{
51+
$comment = (new ReflectionClass($class))->getMethod($method)->getDocComment();
52+
if ($comment !== null) {
53+
return new Doc($comment);
54+
}
55+
56+
return null;
57+
}
58+
}

0 commit comments

Comments
 (0)