Skip to content

Commit 9884141

Browse files
committed
PHP compatibility to avoid parse errors in supported versions
1 parent ba50f98 commit 9884141

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/SchemaProcessor/PostProcessor/EnumPostProcessor.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,20 @@ public function postProcess(): void
138138
parent::postProcess();
139139
}
140140

141+
/**
142+
* @throws SchemaException
143+
*/
141144
private function validateEnum(PropertyInterface $property): bool
142145
{
143-
$throw = fn (string $message) => throw new SchemaException(
144-
sprintf(
145-
$message,
146-
$property->getName(),
147-
$property->getJsonSchema()->getFile()
148-
)
149-
);
146+
$throw = function (string $message) use ($property): void {
147+
throw new SchemaException(
148+
sprintf(
149+
$message,
150+
$property->getName(),
151+
$property->getJsonSchema()->getFile()
152+
)
153+
);
154+
};
150155

151156
$json = $property->getJsonSchema()->getJson();
152157

@@ -168,7 +173,7 @@ private function validateEnum(PropertyInterface $property): bool
168173
|| count(array_uintersect(
169174
$json['enum-map'],
170175
$json['enum'],
171-
fn($a, $b) => $a === $b ? 0 : 1
176+
function ($a, $b): int { return $a === $b ? 0 : 1; }
172177
)) !== count($json['enum'])
173178
)
174179
) {
@@ -200,6 +205,12 @@ private function renderEnum(
200205
$cases[ucfirst($map ? array_search($value, $map) : $value)] = var_export($value, true);
201206
}
202207

208+
$backedType = null;
209+
switch ($this->getArrayTypes($values)) {
210+
case ['string']: $backedType = 'string'; break;
211+
case ['integer']: $backedType = 'int'; break;
212+
}
213+
203214
file_put_contents(
204215
$this->targetDirectory . DIRECTORY_SEPARATOR . $name . '.php',
205216
$this->renderer->renderTemplate(
@@ -208,11 +219,7 @@ private function renderEnum(
208219
'namespace' => $this->namespace,
209220
'name' => $name,
210221
'cases' => $cases,
211-
'backedType' => match ($this->getArrayTypes($values)) {
212-
['string'] => 'string',
213-
['integer'] => 'int',
214-
default => null,
215-
},
222+
'backedType' => $backedType,
216223
]
217224
)
218225
);

tests/PostProcessor/EnumPostProcessorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
1515
use ReflectionEnum;
1616

17+
// TODO: mixed enums, multiple enums, enum redirect
1718
class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
1819
{
1920
/**

0 commit comments

Comments
 (0)