Skip to content

Commit c59cd04

Browse files
committed
Emulate reference and fix generated code
1 parent d1c2468 commit c59cd04

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/ValueObject/ArrayFactory.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OpenCodeModeling\JsonSchemaToPhp\Type\ArrayType;
2020
use OpenCodeModeling\JsonSchemaToPhp\Type\ReferenceType;
2121
use OpenCodeModeling\JsonSchemaToPhp\Type\ScalarType;
22+
use OpenCodeModeling\JsonSchemaToPhp\Type\StringType;
2223
use OpenCodeModeling\JsonSchemaToPhp\Type\TypeDefinition;
2324
use OpenCodeModeling\JsonSchemaToPhp\Type\TypeSet;
2425
use OpenCodeModeling\JsonSchemaToPhpAst\Common\IteratorFactory;
@@ -219,14 +220,15 @@ private function determineType(string $name, TypeSet ...$typeSets): TypeDefiniti
219220
$resolvedTypeSet = $type->resolvedType();
220221

221222
if ($resolvedTypeSet === null) {
222-
throw new \RuntimeException(\sprintf('Reference has no resolved type for "%s".', $name));
223+
$resolvedTypeSet = new TypeSet(
224+
StringType::fromDefinition(['type' => StringType::type(), 'name' => $type->name()])
225+
);
223226
}
224-
225227
if (\count($resolvedTypeSet) !== 1) {
226228
throw new \RuntimeException('Can only handle one JSON type');
227229
}
228-
$type = $typeSet->first();
229-
break;
230+
$type = $resolvedTypeSet->first();
231+
// no break
230232
case $type instanceof ScalarType:
231233
break;
232234
default:
@@ -365,7 +367,7 @@ public function methodRemove(
365367
$copy->%s = array_values(
366368
array_filter(
367369
$copy->%s,
368-
static function($v) { return !$v->equals($%s); }
370+
static function($v) use ($%s) { return !$v->equals($%s); }
369371
)
370372
);
371373
return $copy;
@@ -377,7 +379,7 @@ static function($v) { return !$v->equals($%s); }
377379
(new ParameterGenerator(($this->propertyNameFilter)($argumentType), ($this->classNameFilter)($argumentType))),
378380
],
379381
MethodGenerator::FLAG_PUBLIC,
380-
new BodyGenerator($this->parser, \sprintf($body, $propertyName, $propertyName, ($this->propertyNameFilter)($argumentType)))
382+
new BodyGenerator($this->parser, \sprintf($body, $propertyName, $propertyName, ($this->propertyNameFilter)($argumentType), ($this->propertyNameFilter)($argumentType)))
381383
);
382384
$method->setTyped($this->typed);
383385
$method->setReturnType('self');
@@ -466,7 +468,7 @@ public function methodFilter(
466468
...array_values(
467469
array_filter(
468470
$this->%s,
469-
static function($%s) { return $filter($%s); }
471+
static function($%s) use ($filter) { return $filter($%s); }
470472
)
471473
)
472474
);

tests/ClassGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public function add(Address $address) : self
412412
public function remove(Address $address) : self
413413
{
414414
$copy = clone $this;
415-
$copy->shipping_addresses = array_values(array_filter($copy->shipping_addresses, static function ($v) {
415+
$copy->shipping_addresses = array_values(array_filter($copy->shipping_addresses, static function ($v) use($address) {
416416
return !$v->equals($address);
417417
}));
418418
return $copy;
@@ -439,7 +439,7 @@ public function contains(Address $address) : bool
439439
}
440440
public function filter(callable $filter) : self
441441
{
442-
return new self(...array_values(array_filter($this->shipping_addresses, static function ($v) {
442+
return new self(...array_values(array_filter($this->shipping_addresses, static function ($v) use($filter) {
443443
return $filter($v);
444444
})));
445445
}

tests/ValueObject/ArrayFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function add(ReasonType $reasonType) : self
182182
public function remove(ReasonType $reasonType) : self
183183
{
184184
$copy = clone $this;
185-
$copy->items = array_values(array_filter($copy->items, static function ($v) {
185+
$copy->items = array_values(array_filter($copy->items, static function ($v) use($reasonType) {
186186
return !$v->equals($reasonType);
187187
}));
188188
return $copy;
@@ -209,7 +209,7 @@ public function contains(ReasonType $reasonType) : bool
209209
}
210210
public function filter(callable $filter) : self
211211
{
212-
return new self(...array_values(array_filter($this->items, static function ($v) {
212+
return new self(...array_values(array_filter($this->items, static function ($v) use($filter) {
213213
return $filter($v);
214214
})));
215215
}

0 commit comments

Comments
 (0)