Skip to content

Commit cf69ab8

Browse files
committed
Handle missing properties gracefully
1 parent dd70c7e commit cf69ab8

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

src/Generator.php

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,65 +24,68 @@ public function generate(string $namespace, string $destinationPath)
2424
{
2525
$namespace = $this->cleanUpNamespace($namespace);
2626
$codePrinter = new Standard();
27-
$schemaClassNameMap = [];
28-
foreach ($this->spec->components->schemas as $name => $schema) {
29-
$schemaClassName = $this->className($name);
30-
if (strlen($schemaClassName) === 0) {
31-
continue;
27+
if (count($this->spec->components->schemas) > 0) {
28+
$schemaClassNameMap = [];
29+
foreach ($this->spec->components->schemas as $name => $schema) {
30+
$schemaClassName = $this->className($name);
31+
if (strlen($schemaClassName) === 0) {
32+
continue;
33+
}
34+
$schemaClassNameMap[spl_object_hash($schema)] = $schemaClassName;
3235
}
33-
$schemaClassNameMap[spl_object_hash($schema)] = $schemaClassName;
34-
}
35-
foreach ($this->spec->components->schemas as $name => $schema) {
36-
$schemaClassName = $schemaClassNameMap[spl_object_hash($schema)];
37-
if (strlen($schemaClassName) === 0) {
38-
continue;
36+
foreach ($this->spec->components->schemas as $name => $schema) {
37+
$schemaClassName = $schemaClassNameMap[spl_object_hash($schema)];
38+
if (strlen($schemaClassName) === 0) {
39+
continue;
40+
}
41+
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
42+
file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([
43+
Schema::generate(
44+
$name,
45+
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
46+
strrev(explode('/', strrev($schemaClassName))[0]),
47+
$schema,
48+
$schemaClassNameMap
49+
),
50+
]) . PHP_EOL);
3951
}
40-
@mkdir(dirname($destinationPath . '/Schema/' . $schemaClassName), 0777, true);
41-
file_put_contents($destinationPath . '/Schema/' . $schemaClassName . '.php', $codePrinter->prettyPrintFile([
42-
Schema::generate(
43-
$name,
44-
$this->cleanUpNamespace($namespace . dirname('Schema/' . $schemaClassName)),
45-
strrev(explode('/', strrev($schemaClassName))[0]),
46-
$schema,
47-
$schemaClassNameMap
48-
),
49-
]) . PHP_EOL);
5052
}
5153

52-
foreach ($this->spec->paths as $path => $pathItem) {
53-
$pathClassName = $this->className($path);
54-
if (strlen($pathClassName) === 0) {
55-
continue;
56-
}
57-
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
58-
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
59-
Path::generate(
60-
$path,
61-
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
62-
$namespace,
63-
strrev(explode('/', strrev($pathClassName))[0]),
64-
$pathItem
65-
),
66-
]) . PHP_EOL);
67-
foreach ($pathItem->getOperations() as $method => $operation) {
68-
$operationClassName = $this->className((new Convert($operation->operationId))->fromTrain()->toPascal());
69-
$operations[$method] = $operationClassName;
70-
if (strlen($operationClassName) === 0) {
54+
if (count($this->spec->paths) > 0) {
55+
foreach ($this->spec->paths as $path => $pathItem) {
56+
$pathClassName = $this->className($path);
57+
if (strlen($pathClassName) === 0) {
7158
continue;
7259
}
73-
@mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true);
74-
file_put_contents($destinationPath . '/Operation/' . $operationClassName . '.php', $codePrinter->prettyPrintFile([
75-
Operation::generate(
76-
$path,
77-
$method,
78-
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
79-
strrev(explode('/', strrev($operationClassName))[0]),
80-
$operation
81-
),
82-
]) . PHP_EOL);
60+
@mkdir(dirname($destinationPath . '/Path/' . $pathClassName), 0777, true);
61+
file_put_contents($destinationPath . '/Path/' . $pathClassName . '.php', $codePrinter->prettyPrintFile([
62+
Path::generate(
63+
$path,
64+
$this->cleanUpNamespace($namespace . dirname('Path/' . $pathClassName)),
65+
$namespace,
66+
strrev(explode('/', strrev($pathClassName))[0]),
67+
$pathItem
68+
),
69+
]) . PHP_EOL);
70+
foreach ($pathItem->getOperations() as $method => $operation) {
71+
$operationClassName = $this->className((new Convert($operation->operationId))->fromTrain()->toPascal());
72+
$operations[$method] = $operationClassName;
73+
if (strlen($operationClassName) === 0) {
74+
continue;
75+
}
76+
@mkdir(dirname($destinationPath . '/Operation/' . $operationClassName), 0777, true);
77+
file_put_contents($destinationPath . '/Operation/' . $operationClassName . '.php', $codePrinter->prettyPrintFile([
78+
Operation::generate(
79+
$path,
80+
$method,
81+
$this->cleanUpNamespace($namespace . dirname('Operation/' . $operationClassName)),
82+
strrev(explode('/', strrev($operationClassName))[0]),
83+
$operation
84+
),
85+
]) . PHP_EOL);
86+
}
8387
}
8488
}
85-
8689
}
8790

8891
private function className(string $className): string

0 commit comments

Comments
 (0)