Skip to content

Commit de7bf67

Browse files
committed
Support no operation grouping
Also includes to support the main goal: * Drops any promise based API * Extracting certain WET code into DRY helpers * Pretty print state file so that human diffing of that file becomes possible * More consistent typing
1 parent 069153a commit de7bf67

File tree

83 files changed

+1704
-1248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1704
-1248
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"nikic/php-parser": "^4.15",
2424
"nunomaduro/termwind": "^1.15",
2525
"ondram/ci-detector": "^4.1",
26+
"phpstan/phpdoc-parser": "^1.22",
2627
"pointybeard/reverse-regex": "1.0.0.3",
2728
"psr/http-message": "^1.1 || ^2 || ^3",
2829
"react/async": "^4.0",
@@ -53,7 +54,7 @@
5354
},
5455
"autoload-dev": {
5556
"psr-4": {
56-
"ApiClients\\Tests\\Tools\\OpenApiClientGenerator\\": "tests/"
57+
"ApiClients\\Tests\\Tools\\OpenApiClientGenerator\\": "tests/unit/"
5758
}
5859
},
5960
"bin": [

etc/qa/phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<phpunit bootstrap="../../vendor/autoload.php" colors="true">
33
<testsuites>
44
<testsuite name="Test Suite">
5-
<file>../../tests/GenerateTest.php</file>
5+
<directory suffix=".php">../../tests/unit/</directory>
66
</testsuite>
77
</testsuites>
88
<filter>

example/openapi-client-subsplit.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ state:
33
additionalFiles:
44
- composer.json
55
- composer.lock
6-
spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.yaml
7-
#spec: api.github.com.yaml
6+
#spec: https://raw.githubusercontent.com/github/rest-api-description/main/descriptions-next/api.github.com/api.github.com.yaml
7+
spec: api.github.com.yaml
88
entryPoints:
99
call: true
1010
operations: true

example/templates/composer.json

+8-11
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"{{ require.name }}": "{{ require.version }}",
3131
{% endfor %}
3232
{% endif %}
33-
"wyrihaximus/async-test-utilities": "^6.1"
33+
"wyrihaximus/async-test-utilities": "^7"
3434
},
3535
"autoload": {
3636
"psr-4": {
@@ -42,6 +42,13 @@
4242
"{{ namespace|trim('\\', 'left')|replace({'\\': '\\\\'}) }}": "src/"
4343
}
4444
},
45+
{% if suggests is iterable and suggests|length > 0 %}
46+
"suggest": {
47+
{% for suggest in suggests %}
48+
"api-clients/{{ suggest.name }}": "{{ suggest.reason }}"{% if not loop.last %},{% endif %}
49+
{% endfor %}
50+
},
51+
{% endif %}
4552
"config": {
4653
"sort-packages": true,
4754
"platform": {
@@ -55,15 +62,5 @@
5562
"wyrihaximus/composer-update-bin-autoload-path": true,
5663
"infection/extension-installer": true
5764
}
58-
},
59-
{% if suggests is iterable and suggests|length > 0 %}
60-
"sugest": {
61-
{% for suggest in suggests %}
62-
"api-clients/{{ suggest.name }}": "{{ suggest.reason }}"{% if not loop.last %},{% endif %}
63-
{% endfor %}
64-
},
65-
{% endif %}
66-
"require-dev": {
67-
"wyrihaximus/async-test-utilities": "^5.0"
6865
}
6966
}

src/Configuration.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@
1717

1818
final readonly class Configuration
1919
{
20-
/**
21-
* @param array<class-string<ContentType>>|null $contentType
22-
*/
20+
/** @param array<class-string<ContentType>>|null $contentType */
2321
public function __construct(
2422
public State $state,
2523
public string $spec,
2624
#[MapFrom('entryPoints')]
2725
public EntryPoints $entryPoints,
28-
public ?Templates $templates,
26+
public Templates|null $templates,
2927
public Namespace_ $namespace,
3028
public Destination $destination,
3129
#[MapFrom('contentType')]
32-
public ?array $contentType,
30+
public array|null $contentType,
3331
#[MapFrom('subSplit')]
34-
public ?SubSplit $subSplit,
35-
public ?Schemas $schemas,
36-
public ?Voter $voter,
32+
public SubSplit|null $subSplit,
33+
public Schemas|null $schemas,
34+
public Voter|null $voter,
3735
) {
3836
}
3937
}

src/Configuration/State.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99

1010
final readonly class State
1111
{
12-
/**
13-
* @param array<string> $additionalFiles
14-
*/
12+
/** @param array<string> $additionalFiles */
1513
public function __construct(
1614
public string $file,
1715
#[MapFrom('additionalFiles')]
1816
#[CastListToType('string')]
19-
public ?array $additionalFiles,
17+
public array|null $additionalFiles,
2018
) {
2119
}
2220
}

src/Configuration/SubSplit.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
final readonly class SubSplit
1313
{
14-
/**
15-
* @param array<class-string<SectionGenerator>>|null $sectionGenerator
16-
*/
14+
/** @param array<class-string<SectionGenerator>>|null $sectionGenerator */
1715
public function __construct(
1816
#[MapFrom('subSplitsDestination')]
1917
public string $subSplitsDestination,
@@ -26,7 +24,7 @@ public function __construct(
2624
public string $fullName,
2725
public string $vendor,
2826
#[MapFrom('sectionGenerator')]
29-
public ?array $sectionGenerator,
27+
public array|null $sectionGenerator,
3028
#[MapFrom('rootPackage')]
3129
public RootPackage $rootPackage,
3230
#[MapFrom('sectionPackage')]

src/Configuration/Templates.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
final readonly class Templates
88
{
9-
/**
10-
* @param array<string, mixed>|null $variables
11-
*/
9+
/** @param array<string, mixed>|null $variables */
1210
public function __construct(
1311
public string $dir,
14-
public ?array $variables,
12+
public array|null $variables,
1513
) {
1614
}
1715
}

src/Configuration/Voter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
public function __construct(
1717
#[MapFrom('listOperation')]
18-
public ?array $listOperation,
18+
public array|null $listOperation,
1919
#[MapFrom('streamOperation')]
20-
public ?array $streamOperation,
20+
public array|null $streamOperation,
2121
) {
2222
}
2323
}

src/ContentType/Json.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
final class Json implements ContentType
1313
{
14-
/**
15-
* @return iterable<string>
16-
*/
14+
/** @return iterable<string> */
1715
public static function contentType(): iterable
1816
{
1917
yield 'application/json';

src/ContentType/Raw.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
final class Raw implements ContentType
1111
{
12-
/**
13-
* @return iterable<string>
14-
*/
12+
/** @return iterable<string> */
1513
public static function contentType(): iterable
1614
{
1715
yield 'text/plain';

src/Contract/ContentType.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
interface ContentType
1010
{
11-
/**
12-
* @return iterable<string>
13-
*/
11+
/** @return iterable<string> */
1412
public static function contentType(): iterable;
1513

1614
public static function parse(Expr $expr): Expr;

src/Contract/Voter/ListOperation.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public static function incrementorKey(): string;
1212

1313
public static function incrementorInitialValue(): int;
1414

15-
/**
16-
* @return array<string>
17-
*/
15+
/** @return array<string> */
1816
public static function keys(): array;
1917

2018
public static function list(Operation $operation): bool;

src/Gatherer/ExampleData.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function determiteType(mixed $exampleData): Representation\Example
8888
}
8989

9090
/** @phpstan-ignore-next-line */
91-
public static function scalarData(int $seed, string $type, ?string $format, ?string $pattern = null): Representation\ExampleData
91+
public static function scalarData(int $seed, string $type, string|null $format, string|null $pattern = null): Representation\ExampleData
9292
{
9393
if ($type === 'int' || $type === '?int') {
9494
return new Representation\ExampleData($seed, new Node\Scalar\LNumber($seed));
@@ -159,9 +159,7 @@ public static function scalarData(int $seed, string $type, ?string $format, ?str
159159
);
160160
}
161161

162-
/**
163-
* @param array<mixed> $array
164-
*/
162+
/** @param array<mixed> $array */
165163
private static function turnArrayIntoNode(array $array): Node\Expr
166164
{
167165
return new Node\Expr\FuncCall(

src/Gatherer/HydratorUtils.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
final class HydratorUtils
1111
{
12-
/**
13-
* @return iterable<Schema>
14-
*/
12+
/** @return iterable<Schema> */
1513
public static function listSchemas(Schema $schema): iterable
1614
{
1715
yield $schema;
@@ -21,9 +19,7 @@ public static function listSchemas(Schema $schema): iterable
2119
}
2220
}
2321

24-
/**
25-
* @return iterable<Schema>
26-
*/
22+
/** @return iterable<Schema> */
2723
private static function listSchemasFromPropertyType(PropertyType $propertyType): iterable
2824
{
2925
if ($propertyType->payload instanceof Schema) {

src/Gatherer/Operation.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434

3535
final class Operation
3636
{
37-
/**
38-
* @param array<string, mixed> $metaData
39-
*/
37+
/** @param array<string, mixed> $metaData */
4038
public static function gather(
4139
Namespace_ $baseNamespace,
4240
string $className,
@@ -102,7 +100,7 @@ public static function gather(
102100
'/',
103101
'_',
104102
$contentType,
105-
) . '\\' . ($code === 'default' ? 'Default' : (HttpReasonPhraseLookup::getReasonPhrase($code) ?? 'Unknown'))
103+
) . '\\' . ($code === 'default' ? 'Default' : (HttpReasonPhraseLookup::getReasonPhrase($code) ?? 'Unknown')),
106104
),
107105
);
108106

@@ -140,7 +138,7 @@ public static function gather(
140138
'WebHookHeader\\' . ucfirst(preg_replace('/\PL/u', '', $headerName)),
141139
),
142140
$headerSpec->schema,
143-
$schemaRegistry
141+
$schemaRegistry,
144142
), ExampleData::determiteType($headerSpec->example));
145143
}
146144

@@ -152,7 +150,7 @@ public static function gather(
152150
}
153151

154152
$name = lcfirst(trim(Utils::basename($className), '\\'));
155-
$group = strlen(trim(trim(Utils::dirname($className), '\\'), '.')) > 0 ? trim(Utils::dirname($className), '\\') : 'Fallback';
153+
$group = strlen(trim(trim(Utils::dirname($className), '\\'), '.')) > 0 ? trim(Utils::dirname($className), '\\') : null;
156154

157155
return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Operation(
158156
ClassString::factory($baseNamespace, 'Operation\\' . Utils::fixKeyword($className)),
@@ -161,7 +159,7 @@ public static function gather(
161159
$name,
162160
(new Convert($name))->toCamel(),
163161
$group,
164-
(new Convert($group))->toCamel(),
162+
$group === null ? null : (new Convert($group))->toCamel(),
165163
$operation->operationId,
166164
strtoupper($matchMethod),
167165
strtoupper($method),

src/Gatherer/Path.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function gather(
2424
PathItem $pathItem,
2525
SchemaRegistry $schemaRegistry,
2626
ThrowableSchema $throwableSchemaRegistry,
27-
?Voter $voters,
27+
Voter|null $voters,
2828
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Path {
2929
$className = Utils::fixKeyword($className);
3030
$operations = [];

src/Gatherer/Schema.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function gather(
4343
false,
4444
),
4545
$property,
46-
$schemaRegistry
46+
$schemaRegistry,
4747
);
4848
$properties[] = $gatheredProperty;
4949

@@ -72,7 +72,7 @@ public static function gather(
7272
$properties,
7373
$schema,
7474
$isArray,
75-
($schema->type === null ? ['object'] : (is_array($schema->type) ? $schema->type : [$schema->type]))
75+
($schema->type === null ? ['object'] : (is_array($schema->type) ? $schema->type : [$schema->type])),
7676
);
7777
}
7878
}

src/Gatherer/Type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static function gather(
186186
null,
187187
null,
188188
$arrayItems,
189-
$nullable
189+
$nullable,
190190
);
191191
}
192192

src/Gatherer/WebHook.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function gather(
2424
PathItem $webhook,
2525
SchemaRegistry $schemaRegistry,
2626
): \ApiClients\Tools\OpenApiClientGenerator\Representation\WebHook {
27-
if ($webhook->post === null || $webhook->post->requestBody === null || ! property_exists($webhook->post->requestBody, 'content')) {
27+
if ($webhook->post?->requestBody === null || ! property_exists($webhook->post->requestBody, 'content')) {
2828
// var_export(json_decode(json_encode($webhook->getSerializableData())));
2929
throw new RuntimeException('Missing request body content to deal with');
3030
}
@@ -44,7 +44,7 @@ public static function gather(
4444
'WebHookHeader\\' . ucfirst(preg_replace('/\PL/u', '', $header->name)),
4545
),
4646
$header->schema,
47-
$schemaRegistry
47+
$schemaRegistry,
4848
), ExampleData::determiteType($headerSpec->example));
4949
}
5050

0 commit comments

Comments
 (0)