Skip to content

Commit

Permalink
Fix double rendering of union types
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkan committed Nov 21, 2024
1 parent 8e110fd commit adb23fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ValueObject/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ public function getTypeHint(bool $renderUnion = false): ?string
if ($renderUnion) {
$typeHint = [];
foreach ($this->getUnionTypes() as $unionType) {
$typeHint[] = $unionType->getTypeHint();
$unionTypeHint = $unionType->getTypeHint();
if (!in_array($unionTypeHint, $typeHint)) {
$typeHint[] = $unionTypeHint;
}
}
if ($this->isNullable() && !in_array('mixed', $typeHint)) {
array_unshift($typeHint, 'null');
Expand Down
8 changes: 8 additions & 0 deletions tests/ValueObject/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,12 @@ public function testEnumStringValueNullableAndClass(): void
$this->assertNull($type->getTypeHint());
$this->assertNull($type->getTypeHint(true));
}

public function testDontDoubleRenderUnionTypes(): void
{
$type = Type::fromString('array<int>');
$type->addUnion('array<string>');

$this->assertSame('array', $type->getTypeHint(true));
}
}

0 comments on commit adb23fb

Please sign in to comment.