Skip to content

Commit 1ba68c0

Browse files
authored
Merge pull request #9 from php-api-clients/nullable-all-the-things
Nullable all the things!
2 parents b9c59dd + 3e36bff commit 1ba68c0

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/Generator/Schema.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,28 @@ public static function generate(string $name, string $namespace, string $classNa
7979
if ($property->type === 'array' && $property->items instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->items), $schemaClassNameMap)) {
8080
$docBlock[] = '@var array<\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->items)] . '>';
8181
}
82-
$propertyStmt->setType(str_replace([
82+
$t = str_replace([
8383
'integer',
8484
'any',
8585
'boolean',
8686
], [
8787
'int',
8888
'',
8989
'bool',
90-
], $property->type));
91-
$method->setReturnType(str_replace([
92-
'integer',
93-
'any',
94-
'boolean',
95-
], [
96-
'int',
97-
'',
98-
'bool',
99-
], $property->type));
90+
], $property->type);
91+
if ($t !== '') {
92+
$dft = [];
93+
if ($t !== 'array') {
94+
$t = '?' . $t;
95+
$dft = null;
96+
}
97+
$propertyStmt->setType($t)->setDefault($dft);
98+
$method->setReturnType($t);
99+
}
100100
} else if (is_array($property->anyOf) && $property->anyOf[0] instanceof OpenAPiSchema && array_key_exists(spl_object_hash($property->anyOf[0]), $schemaClassNameMap)) {
101-
$propertyStmt->setType('\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])]);
101+
$fqcnn = '?\\' . $namespace . '\\' . $schemaClassNameMap[spl_object_hash($property->anyOf[0])];
102+
$propertyStmt->setType($fqcnn)->setDefault(null);
103+
$method->setReturnType($fqcnn);
102104
}
103105

104106
if (count($docBlock) > 0) {

0 commit comments

Comments
 (0)