From 553b45306fe50ed3aa51db521d2b501c1e2b69e7 Mon Sep 17 00:00:00 2001 From: jordanbrauer <18744334+jordanbrauer@users.noreply.github.com> Date: Wed, 16 Jun 2021 21:29:08 -0500 Subject: [PATCH] feat: handle unions (PHP 8) --- src/Support/Reflection.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 7d46ece9..1719b9f6 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -12,6 +12,7 @@ use ReflectionException; use ReflectionFunction; use ReflectionNamedType; use ReflectionParameter; +use ReflectionUnionType; /** * @internal @@ -165,9 +166,23 @@ final class Reflection $arguments = []; foreach ($parameters as $parameter) { - /** @var ReflectionNamedType|null $type */ - $type = ($parameter->hasType()) ? $parameter->getType() : null; - $arguments[$parameter->getName()] = (is_null($type)) ? 'mixed' : $type->getName(); + /** @var ReflectionNamedType|ReflectionUnionType|null $types */ + $types = ($parameter->hasType()) ? $parameter->getType() : null; + + if (is_null($types)) { + $arguments[$parameter->getName()] = 'mixed'; + + continue; + } + + $arguments[$parameter->getName()] = implode('|', array_map( + static function (ReflectionNamedType $type): string { + return $type->getName(); + }, + ($types instanceof ReflectionNamedType) + ? [$types] // NOTE: normalize as list of to handle unions + : $types->getTypes(), + )); } return $arguments;