feat: handle unions (PHP 8)

This commit is contained in:
jordanbrauer
2021-06-16 21:29:08 -05:00
parent 2e7192ab95
commit 553b45306f

View File

@ -12,6 +12,7 @@ use ReflectionException;
use ReflectionFunction; use ReflectionFunction;
use ReflectionNamedType; use ReflectionNamedType;
use ReflectionParameter; use ReflectionParameter;
use ReflectionUnionType;
/** /**
* @internal * @internal
@ -165,9 +166,23 @@ final class Reflection
$arguments = []; $arguments = [];
foreach ($parameters as $parameter) { foreach ($parameters as $parameter) {
/** @var ReflectionNamedType|null $type */ /** @var ReflectionNamedType|ReflectionUnionType|null $types */
$type = ($parameter->hasType()) ? $parameter->getType() : null; $types = ($parameter->hasType()) ? $parameter->getType() : null;
$arguments[$parameter->getName()] = (is_null($type)) ? 'mixed' : $type->getName();
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; return $arguments;