diff --git a/src/TypeDef.php b/src/TypeDef.php index c3da488..9fa1c99 100644 --- a/src/TypeDef.php +++ b/src/TypeDef.php @@ -21,7 +21,7 @@ class TypeDef * * @var array> */ - private array $type = [[]]; + protected array $type = [[]]; public readonly bool $allowsNull; @@ -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. * diff --git a/tests/TypeDefTest.php b/tests/TypeDefTest.php index d2d5405..8347168 100644 --- a/tests/TypeDefTest.php +++ b/tests/TypeDefTest.php @@ -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)); @@ -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));