Skip to content

Introduce central operators object #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Gatherer/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public static function gather(
ClassString::factory($baseNamespace, 'Operation\\' . Utils::fixKeyword($className)),
ClassString::factory($baseNamespace, $classNameSanitized),
ClassString::factory($baseNamespace, 'Operator\\' . Utils::fixKeyword($className)),
lcfirst(
str_replace(
['\\'],
['👷'],
ClassString::factory($baseNamespace, Utils::fixKeyword($className))->relative,
),
),
$name,
(new Convert($name))->toCamel(),
$group,
Expand Down
126 changes: 64 additions & 62 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,39 +158,46 @@ public static function generate(Configuration $configuration, string $pathPrefix
new Node\Name('Operations'),
[
new Arg(
new Node\Expr\Variable('browser'),
false,
false,
[],
new Node\Identifier('browser'),
),
new Arg(
new Node\Expr\Variable('authentication'),
false,
false,
[],
new Node\Identifier('authentication'),
),
new Arg(
new Node\Expr\Variable('requestSchemaValidator'),
false,
false,
[],
new Node\Identifier('requestSchemaValidator'),
),
new Arg(
new Node\Expr\Variable('responseSchemaValidator'),
false,
false,
[],
new Node\Identifier('responseSchemaValidator'),
),
new Arg(
new Node\Expr\Variable('hydrators'),
false,
false,
[],
new Node\Identifier('hydrators'),
new Node\Expr\New_(
new Node\Name('Operators'),
[
new Arg(
new Node\Expr\Variable('browser'),
false,
false,
[],
new Node\Identifier('browser'),
),
new Arg(
new Node\Expr\Variable('authentication'),
false,
false,
[],
new Node\Identifier('authentication'),
),
new Arg(
new Node\Expr\Variable('requestSchemaValidator'),
false,
false,
[],
new Node\Identifier('requestSchemaValidator'),
),
new Arg(
new Node\Expr\Variable('responseSchemaValidator'),
false,
false,
[],
new Node\Identifier('responseSchemaValidator'),
),
new Arg(
new Node\Expr\Variable('hydrators'),
false,
false,
[],
new Node\Identifier('hydrators'),
),
],
),
),
],
),
Expand All @@ -206,14 +213,20 @@ public static function generate(Configuration $configuration, string $pathPrefix
new Node\Expr\New_(
new Node\Name('WebHooks'),
[
new Node\Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'requestSchemaValidator',
)),
new Node\Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'hydrators',
)),
new Arg(
new Node\Expr\Variable('requestSchemaValidator'),
false,
false,
[],
new Node\Identifier('requestSchemaValidator'),
),
new Arg(
new Node\Expr\Variable('hydrators'),
false,
false,
[],
new Node\Identifier('hydrator'),
),
],
),
),
Expand All @@ -229,50 +242,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
new Node\Name('Routers'),
[
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'browser',
),
new Node\Expr\Variable('browser'),
false,
false,
[],
new Node\Identifier('browser'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'authentication',
),
new Node\Expr\Variable('authentication'),
false,
false,
[],
new Node\Identifier('authentication'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'requestSchemaValidator',
),
new Node\Expr\Variable('requestSchemaValidator'),
false,
false,
[],
new Node\Identifier('requestSchemaValidator'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'responseSchemaValidator',
),
new Node\Expr\Variable('responseSchemaValidator'),
false,
false,
[],
new Node\Identifier('responseSchemaValidator'),
),
new Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'hydrators',
),
new Node\Expr\Variable('hydrators'),
false,
false,
[],
Expand Down Expand Up @@ -695,7 +693,11 @@ private static function traverseOperationPaths(array $operations, array &$operat
return $operations;
}

/** @param array<Representation\Path> $paths */
/**
* @param array<Representation\Path> $paths
*
* @return iterable<Representation\Path>
*/
private static function operationsInThisThree(array $paths, int $level, Routers $routers): iterable
{
foreach ($paths as $path) {
Expand Down
116 changes: 23 additions & 93 deletions src/Generator/Helper/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ApiClients\Tools\OpenApiClientGenerator\Generator\Helper;

use ApiClients\Tools\OpenApiClientGenerator\Representation;
use ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator;
use PhpParser\Builder;
use PhpParser\Comment\Doc;
use PhpParser\Node;
Expand All @@ -27,7 +26,6 @@
use function is_string;
use function str_replace;
use function strpos;
use function ucfirst;

use const PHP_EOL;

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

public static function methodReturnType(Builder\Method $method, Representation\Operation $operation): Builder\Method
{
$docComment = ReflectionTypes::copyDocBlock($operation->operatorClassName->fullyQualified->source, 'call');

if ($docComment !== null) {
$method = $method->setDocComment($docComment);
}

return $method->setReturnType(
new Node\UnionType(
Types::normalizeNodeName(...$operation->returnType),
),
ReflectionTypes::copyReturnType($operation->operatorClassName->fullyQualified->source, 'call'),
);
}

/**
* @param array<string, Hydrator> $operationHydratorMap
*
* @return array<Node>
*/
public static function methodCallOperation(Representation\Operation $operation, array $operationHydratorMap): array
public static function methodCallOperation(Representation\Operation $operation): Node\Stmt\Return_
{
return [
new Node\Stmt\If_(
new Node\Expr\BinaryOp\Equal(
new Node\Expr\FuncCall(
new Node\Name('\array_key_exists'),
[
new Arg(new Node\Expr\ClassConstFetch(
new Node\Name($operation->operatorClassName->relative),
'class',
)),
new Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'operator',
)),
],
return new Node\Stmt\Return_(
new Expr\MethodCall(
new Expr\MethodCall(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'operators',
),
new Node\Expr\ConstFetch(new Node\Name('false')),
$operation->operatorLookUpMethod,
),
'call',
[
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'operator',
), new Node\Expr\ClassConstFetch(
new Node\Name($operation->operatorClassName->relative),
'class',
)),
new Node\Expr\New_(
new Node\Name($operation->operatorClassName->relative),
[
new Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'browser',
)),
new Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'authentication',
)),
...(count($operation->requestBody) > 0 ? [
new Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'requestSchemaValidator',
)),
] : []),
new Arg(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'responseSchemaValidator',
)),
new Arg(
new Expr\MethodCall(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'hydrators',
),
'getObjectMapper' . ucfirst($operationHydratorMap[$operation->operationId]->methodName),
),
),
],
),
),
),
],
...(static function (array $params): iterable {
foreach ($params as $param) {
yield new Arg(new Node\Expr\Variable($param->name));
}
})($operation->parameters),
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
],
),
new Node\Stmt\Return_(
new Expr\MethodCall(
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'operator',
), new Node\Expr\ClassConstFetch(
new Node\Name($operation->operatorClassName->relative),
'class',
)),
'call',
[
...(static function (array $params): iterable {
foreach ($params as $param) {
yield new Arg(new Node\Expr\Variable($param->name));
}
})($operation->parameters),
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
],
),
),
];
);
}

public static function getResultTypeFromOperation(Representation\Operation $operation): string
Expand Down
58 changes: 58 additions & 0 deletions src/Generator/Helper/ReflectionTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace ApiClients\Tools\OpenApiClientGenerator\Generator\Helper;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Name;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionType;
use ReflectionUnionType;

use function strpos;

final class ReflectionTypes
{
public static function copyReturnType(string $class, string $method): Node\ComplexType|Name|string
{
$reflection = (new ReflectionClass($class))->getMethod($method)->getReturnType();
switch ($reflection::class) {
//ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType
case ReflectionNamedType::class:
return new Name(
(strpos((string) $reflection, '\\') !== false ? '\\' : '') . $reflection,
);

break;
case ReflectionUnionType::class:
return new Node\UnionType(
[
...(static function (ReflectionType ...$types): iterable {
foreach ($types as $type) {
yield new Name(
(strpos((string) $type, '\\') !== false ? '\\' : '') . $type,
);
}
})(...$reflection->getTypes()),
],
);

break;
default:
return '';
}
}

public static function copyDocBlock(string $class, string $method): Doc|null
{
$comment = (new ReflectionClass($class))->getMethod($method)->getDocComment();
if ($comment !== null) {
return new Doc($comment);
}

return null;
}
}
Loading