Skip to content

Commit

Permalink
Emulate reference and fix generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrokeil committed Dec 9, 2020
1 parent d1c2468 commit c59cd04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/ValueObject/ArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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); }
)
)
);
Expand Down
4 changes: 2 additions & 2 deletions tests/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
})));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ValueObject/ArrayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
})));
}
Expand Down

0 comments on commit c59cd04

Please sign in to comment.