Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/TypeDef.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TypeDef
*
* @var array<array<string>>
*/
private array $type = [[]];
protected array $type = [[]];

public readonly bool $allowsNull;

Expand Down Expand Up @@ -71,6 +71,16 @@ public function getSimpleType(): ?string
return $this->isSimple() ? $this->type[0][0] : null;
}

/**
* Returns a list of the unioned types, or null if it's not a union type.
*
* @return string[]|null
*/
public function getUnionTypes(): ?array
{
return $this->complexity === TypeComplexity::Union ? array_column($this->type, 0) : null;
}

/**
* Determines if this type definition will accept a value of the specified type.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/TypeDefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public static function typeDefProvider(): iterable
static::assertFalse($typeDef->allowsNull);
static::assertFalse($typeDef->isSimple());
static::assertEquals(TypeComplexity::Union, $typeDef->complexity);
static::assertEquals(['string', 'int'], $typeDef->getUnionTypes());
static::assertTrue($typeDef->accepts('int'));
static::assertTrue($typeDef->accepts('string'));
static::assertFalse($typeDef->accepts(SomeClass::class));
Expand All @@ -163,6 +164,7 @@ public static function typeDefProvider(): iterable
static::assertFalse($typeDef->allowsNull);
static::assertFalse($typeDef->isSimple());
static::assertEquals(TypeComplexity::Union, $typeDef->complexity);
static::assertEquals([SomeClass::class, 'string'], $typeDef->getUnionTypes());
static::assertTrue($typeDef->accepts(SomeClass::class));
static::assertTrue($typeDef->accepts('string'));
static::assertFalse($typeDef->accepts(OtherClass::class));
Expand Down