Skip to content

Introduce QA settings #207

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
Oct 16, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"php": "^8.2",
"api-clients/contracts": "^0.1",
"api-clients/github": "^0.2@dev",
"api-clients/openapi-client-utils": "dev-main",
"ckr/arraymerger": "^3.0",
"codeinc/http-reason-phrase-lookup": "^1.0",
"delight-im/random": "^1.0",
Expand Down
459 changes: 258 additions & 201 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions example/openapi-client-miele.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ voter:
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery
streamOperation:
- ApiClients\Tools\OpenApiClientGenerator\Voter\StreamOperation\DownloadInOperationId
qa:
phpcs:
enabled: true
phpstan:
enabled: true
configFilePath: etc/phpstan-extension.neon
6 changes: 6 additions & 0 deletions example/openapi-client-one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ voter:
- ApiClients\Tools\OpenApiClientGenerator\Voter\ListOperation\PageAndPerPageInQuery
streamOperation:
- ApiClients\Tools\OpenApiClientGenerator\Voter\StreamOperation\DownloadInOperationId
qa:
phpcs:
enabled: true
phpstan:
enabled: true
configFilePath: etc/phpstan-extension.neon
6 changes: 6 additions & 0 deletions example/openapi-client-subsplit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ subSplit:
sectionPackage:
name: github-{{ section }}
repository: [email protected]:php-api-clients/github-{{ section }}.git
qa:
phpcs:
enabled: true
phpstan:
enabled: true
configFilePath: etc/phpstan-extension.neon
10 changes: 10 additions & 0 deletions example/templates/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{% endfor %}
{% endif %}
"api-clients/contracts": "^0.1",
"api-clients/openapi-client-utils": "dev-main",
"devizzent/cebe-php-openapi": "^1",
"eventsauce/object-hydrator": "^1.1",
"league/openapi-psr7-validator": "^0.21",
Expand Down Expand Up @@ -48,6 +49,15 @@
"api-clients/{{ suggest.name }}": "{{ suggest.reason }}"{% if not loop.last %},{% endif %}
{% endfor %}
},
{% endif %}
{% if qa.phpstan.enabled is constant('true') and qa.phpstan.configFilePath is not constant('null') %}
"extra": {
"phpstan": {
"includes": [
"{{ qa.phpstan.configFilePath }}"
]
}
},
{% endif %}
"config": {
"sort-packages": true,
Expand Down
1 change: 1 addition & 0 deletions example/templates/etc/qa/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
includes:
- ../../vendor/wyrihaximus/async-test-utilities/rules.neon
- ../phpstan-extension.neon
2 changes: 2 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Destination;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\EntryPoints;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\QA;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Schemas;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\State;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\SubSplit;
Expand All @@ -32,6 +33,7 @@ public function __construct(
public SubSplit|null $subSplit,
public Schemas|null $schemas,
public Voter|null $voter,
public QA|null $qa,
) {
}
}
17 changes: 17 additions & 0 deletions src/Configuration/QA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace ApiClients\Tools\OpenApiClientGenerator\Configuration;

use ApiClients\Tools\OpenApiClientGenerator\Configuration\QA\Tool;

final readonly class QA
{
public function __construct(
public Tool|null $phpcs,
public Tool|null $phpstan,
public Tool|null $psalm,
) {
}
}
17 changes: 17 additions & 0 deletions src/Configuration/QA/Tool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace ApiClients\Tools\OpenApiClientGenerator\Configuration\QA;

use EventSauce\ObjectHydrator\MapFrom;

final readonly class Tool
{
public function __construct(
public bool $enabled,
#[MapFrom('configFilePath')]
public string|null $configFilePath,
) {
}
}
5 changes: 2 additions & 3 deletions src/Gatherer/WebHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public static function gather(
PathItem $webhook,
SchemaRegistry $schemaRegistry,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\WebHook {
if ($webhook->post?->requestBody === null || ! property_exists($webhook->post->requestBody, 'content')) {
// var_export(json_decode(json_encode($webhook->getSerializableData())));
if ($webhook->post?->requestBody === null && ! property_exists($webhook->post->requestBody, 'content')) {
throw new RuntimeException('Missing request body content to deal with');
}

Expand All @@ -45,7 +44,7 @@ public static function gather(
),
$header->schema,
$schemaRegistry,
), ExampleData::determiteType($headerSpec->example));
), ExampleData::determiteType($header->example));
}

return new \ApiClients\Tools\OpenApiClientGenerator\Representation\WebHook(
Expand Down
2 changes: 1 addition & 1 deletion src/Gatherer/WebHookHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function gather(

return Hydrator::gather(
$baseNamespace,
'Internal\\WebHook\\' . Utils::className($event),
'WebHook\\' . Utils::className($event),
'🪝',
...$schemaClasses,
);
Expand Down
14 changes: 12 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use function str_replace;
use function strlen;
use function strpos;
use function substr;
use function trim;
use function usleep;
use function WyriHaximus\Twig\render;
Expand Down Expand Up @@ -195,7 +196,7 @@ public function generate(string $namespace, string $namespaceTest, string $confi
$codePrinter = new Standard();

foreach ($this->all($configurationLocation) as $file) {
$fileName = $configurationLocation . $this->configuration->destination->root . DIRECTORY_SEPARATOR . $file->pathPrefix . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $file->fqcn) . '.php';
$fileName = $configurationLocation . $this->configuration->destination->root . DIRECTORY_SEPARATOR . $file->pathPrefix . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $file->fqcn) . (strpos($file->fqcn, '.') !== false ? '' : '.php');
if ($file->contents instanceof Node\Stmt\Namespace_) {
array_unshift($file->contents->stmts, ...(static function (array $uses): iterable {
foreach ($uses as $use => $alias) {
Expand Down Expand Up @@ -246,7 +247,10 @@ public function generate(string $namespace, string $namespaceTest, string $confi
}
}

include_once $fileName;
if (! (strpos($fileName, DIRECTORY_SEPARATOR . 'Types' . DIRECTORY_SEPARATOR) !== false) && substr($fileName, -4) === '.php') {
include_once $fileName;
}

$existingFiles = array_filter(
$existingFiles,
static fn (string $file): bool => $file !== $fileName,
Expand Down Expand Up @@ -510,6 +514,7 @@ private function oneClient(
yield from Client::generate(
$this->configuration,
$this->configuration->destination->source . DIRECTORY_SEPARATOR,
$this->configuration->destination->test . DIRECTORY_SEPARATOR,
$client,
$routers,
);
Expand Down Expand Up @@ -600,6 +605,7 @@ private function oneClient(
'operations' => $operations,
'webHooks' => $webHooks,
];
$vars['qa'] = $configuration->qa;

return $vars;
})(
Expand Down Expand Up @@ -793,6 +799,7 @@ private function subSplitClient(
yield from Client::generate(
$this->configuration,
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->test,
$client,
$routers,
);
Expand Down Expand Up @@ -926,6 +933,7 @@ private function subSplitClient(
}
})($this->configuration->subSplit->sectionPackage->name, ...$splits),
],
'qa' => $this->configuration->qa,
],
);
$this->statusOutput->markStepDone('generating_templates_files_root_package');
Expand All @@ -950,6 +958,7 @@ private function subSplitClient(
'version' => $this->configuration->subSplit->targetVersion,
],
],
'qa' => $this->configuration->qa,
],
);
$this->statusOutput->markStepDone('generating_templates_files_common_package');
Expand Down Expand Up @@ -981,6 +990,7 @@ private function subSplitClient(
'version' => $this->configuration->subSplit->targetVersion,
],
],
'qa' => $this->configuration->qa,
],
);
$this->statusOutput->advanceStep('generating_templates_files_subsplit_package');
Expand Down
69 changes: 48 additions & 21 deletions src/Generator/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use ApiClients\Tools\OpenApiClientGenerator\Configuration;
use ApiClients\Tools\OpenApiClientGenerator\File;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Client\Methods\ChunkCount;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Client\PHPStan\ClientCallReturnTypes;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Client\PHPStan\ClientCallReturnTypesTest;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Client\Routers;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Client\Routers\RouterClass;
use ApiClients\Tools\OpenApiClientGenerator\Generator\Helper\Operation;
Expand Down Expand Up @@ -37,19 +39,22 @@
use function array_shift;
use function array_unique;
use function count;
use function dirname;
use function explode;
use function implode;
use function PHPStan\Testing\assertType;
use function strlen;
use function strpos;
use function trim;
use function ucfirst;

use const DIRECTORY_SEPARATOR;
use const PHP_EOL;

final class Client
{
/** @return iterable<File> */
public static function generate(Configuration $configuration, string $pathPrefix, Representation\Client $client, Routers $routers): iterable
public static function generate(Configuration $configuration, string $pathPrefix, string $pathPrefixTests, Representation\Client $client, Routers $routers): iterable
{
$operations = [];
foreach ($client->paths as $path) {
Expand Down Expand Up @@ -515,28 +520,28 @@ public static function generate(Configuration $configuration, string $pathPrefix
$class->addStmt(
$factory->method('call')->makePublic()->setDocComment(
new Doc(implode(PHP_EOL, [
'// phpcs:disable',
...($configuration->qa?->phpcs ? ['// phpcs:disable'] : []),
'/**',
' * @return ' . (static function (array $operations): string {
$count = count($operations);
$lastItem = $count - 1;
$left = '';
$right = '';
for ($i = 0; $i < $count; $i++) {
$returnType = Operation::getDocBlockResultTypeFromOperation($operations[$i]);
if ($i !== $lastItem) {
$left .= '($call is Operation\\' . $operations[$i]->classNameSanitized->relative . '::OPERATION_MATCH ? ' . $returnType . ' : ';
} else {
$left .= $returnType;
}

$right .= ')';
}

return $left . $right;
})($operations),
// ' * @return ' . (static function (array $operations): string {
// $count = count($operations);
// $lastItem = $count - 1;
// $left = '';
// $right = '';
// for ($i = 0; $i < $count; $i++) {
// $returnType = Operation::getDocBlockResultTypeFromOperation($operations[$i]);
// if ($i !== $lastItem) {
// $left .= '($call is "' . $operations[$i]->matchMethod . ' ' . $operations[$i]->path . '" ? ' . $returnType . ' : ';
// } else {
// $left .= $returnType;
// }
//
// $right .= ')';
// }
//
// return $left . $right;
// })($operations),
' */',
'// phpcs:enable',
...($configuration->qa?->phpcs ? ['// phpcs:enable'] : []),
])),
)->addParam((new Param('call'))->setType('string'))->addParam((new Param('params'))->setType('array')->setDefault([]))->setReturnType(
new UnionType(
Expand Down Expand Up @@ -661,6 +666,28 @@ public static function generate(Configuration $configuration, string $pathPrefix
}

yield from \ApiClients\Tools\OpenApiClientGenerator\Generator\Routers::generate($configuration, $pathPrefix, $routers);

if (! $configuration->qa?->phpstan) {
return;
}

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'phpstan-assertType-mock.php';
assertType('bool', true);

yield from ClientCallReturnTypes::generate($configuration, $pathPrefix, $client);
yield from ClientCallReturnTypesTest::generate($configuration, $pathPrefixTests, $client);

if ($configuration->qa->phpstan->configFilePath === null) {
return;
}

yield new File($pathPrefix, '../' . $configuration->qa->phpstan->configFilePath, implode(PHP_EOL, [
'services:',
' - class: ' . $configuration->namespace->source . '\PHPStan\ClientCallReturnTypes',
' tags:',
' - phpstan.broker.dynamicMethodReturnTypeExtension',
'',
]));
}

/**
Expand Down
Loading