From c59cd04f727556bdcfd908b61d4bd07a591b9cdc Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Wed, 9 Dec 2020 09:32:04 +0100 Subject: [PATCH] Emulate reference and fix generated code --- src/ValueObject/ArrayFactory.php | 16 +++++++++------- tests/ClassGeneratorTest.php | 4 ++-- tests/ValueObject/ArrayFactoryTest.php | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/ValueObject/ArrayFactory.php b/src/ValueObject/ArrayFactory.php index e1c221e..6f8f715 100644 --- a/src/ValueObject/ArrayFactory.php +++ b/src/ValueObject/ArrayFactory.php @@ -19,6 +19,7 @@ use OpenCodeModeling\JsonSchemaToPhp\Type\ArrayType; use OpenCodeModeling\JsonSchemaToPhp\Type\ReferenceType; use OpenCodeModeling\JsonSchemaToPhp\Type\ScalarType; +use OpenCodeModeling\JsonSchemaToPhp\Type\StringType; use OpenCodeModeling\JsonSchemaToPhp\Type\TypeDefinition; use OpenCodeModeling\JsonSchemaToPhp\Type\TypeSet; use OpenCodeModeling\JsonSchemaToPhpAst\Common\IteratorFactory; @@ -219,14 +220,15 @@ private function determineType(string $name, TypeSet ...$typeSets): TypeDefiniti $resolvedTypeSet = $type->resolvedType(); if ($resolvedTypeSet === null) { - throw new \RuntimeException(\sprintf('Reference has no resolved type for "%s".', $name)); + $resolvedTypeSet = new TypeSet( + StringType::fromDefinition(['type' => StringType::type(), 'name' => $type->name()]) + ); } - if (\count($resolvedTypeSet) !== 1) { throw new \RuntimeException('Can only handle one JSON type'); } - $type = $typeSet->first(); - break; + $type = $resolvedTypeSet->first(); + // no break case $type instanceof ScalarType: break; default: @@ -365,7 +367,7 @@ public function methodRemove( $copy->%s = array_values( array_filter( $copy->%s, - static function($v) { return !$v->equals($%s); } + static function($v) use ($%s) { return !$v->equals($%s); } ) ); return $copy; @@ -377,7 +379,7 @@ static function($v) { return !$v->equals($%s); } (new ParameterGenerator(($this->propertyNameFilter)($argumentType), ($this->classNameFilter)($argumentType))), ], MethodGenerator::FLAG_PUBLIC, - new BodyGenerator($this->parser, \sprintf($body, $propertyName, $propertyName, ($this->propertyNameFilter)($argumentType))) + new BodyGenerator($this->parser, \sprintf($body, $propertyName, $propertyName, ($this->propertyNameFilter)($argumentType), ($this->propertyNameFilter)($argumentType))) ); $method->setTyped($this->typed); $method->setReturnType('self'); @@ -466,7 +468,7 @@ public function methodFilter( ...array_values( array_filter( $this->%s, - static function($%s) { return $filter($%s); } + static function($%s) use ($filter) { return $filter($%s); } ) ) ); diff --git a/tests/ClassGeneratorTest.php b/tests/ClassGeneratorTest.php index 3f9774d..3639c1d 100644 --- a/tests/ClassGeneratorTest.php +++ b/tests/ClassGeneratorTest.php @@ -412,7 +412,7 @@ public function add(Address $address) : self public function remove(Address $address) : self { $copy = clone $this; - $copy->shipping_addresses = array_values(array_filter($copy->shipping_addresses, static function ($v) { + $copy->shipping_addresses = array_values(array_filter($copy->shipping_addresses, static function ($v) use($address) { return !$v->equals($address); })); return $copy; @@ -439,7 +439,7 @@ public function contains(Address $address) : bool } public function filter(callable $filter) : self { - return new self(...array_values(array_filter($this->shipping_addresses, static function ($v) { + return new self(...array_values(array_filter($this->shipping_addresses, static function ($v) use($filter) { return $filter($v); }))); } diff --git a/tests/ValueObject/ArrayFactoryTest.php b/tests/ValueObject/ArrayFactoryTest.php index d6010d7..cc9eac0 100644 --- a/tests/ValueObject/ArrayFactoryTest.php +++ b/tests/ValueObject/ArrayFactoryTest.php @@ -182,7 +182,7 @@ public function add(ReasonType $reasonType) : self public function remove(ReasonType $reasonType) : self { $copy = clone $this; - $copy->items = array_values(array_filter($copy->items, static function ($v) { + $copy->items = array_values(array_filter($copy->items, static function ($v) use($reasonType) { return !$v->equals($reasonType); })); return $copy; @@ -209,7 +209,7 @@ public function contains(ReasonType $reasonType) : bool } public function filter(callable $filter) : self { - return new self(...array_values(array_filter($this->items, static function ($v) { + return new self(...array_values(array_filter($this->items, static function ($v) use($filter) { return $filter($v); }))); }