From 6262542c1820d6d92eda4747ed66b52733ac3653 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Fri, 3 Jan 2025 10:28:46 -0300 Subject: [PATCH] fix pr #8 --- src/ReflectionParameterTyped.php | 42 +++++++++++++++++++++++++++++--- src/functions.php | 8 ++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/ReflectionParameterTyped.php b/src/ReflectionParameterTyped.php index 77635a3..6b116ac 100644 --- a/src/ReflectionParameterTyped.php +++ b/src/ReflectionParameterTyped.php @@ -34,9 +34,7 @@ public function __construct( private ReflectionParameter $reflection ) { $this->type = $this->getType(); - $parameter = ($this->type instanceof ReflectionUnionType) ? - toUnionParameter($this->type->getTypes()) : - toParameter($this->type?->getName() ?? 'mixed'); + $parameter = $this->getParameter(); try { $attribute = reflectedParameterAttribute($reflection); @@ -44,7 +42,7 @@ public function __construct( // do nothing } if (isset($attribute, $this->type)) { - $typeHint = $this->type->getName(); + $typeHint = $this->getTypeHint($this->type); $attrHint = $attribute->parameter()->type()->typeHinting(); if ($typeHint !== $attrHint) { throw new TypeError( @@ -76,6 +74,25 @@ public function parameter(): ParameterInterface return $this->parameter; } + private function getParameter(): ParameterInterface + { + if ($this->type instanceof ReflectionUnionType) { + $types = []; + foreach ($this->type->getTypes() as $type) { + if ($type instanceof ReflectionIntersectionType) { + continue; + } + $types[] = $type->getName(); + } + + return toUnionParameter(...$types); + } elseif ($this->type !== null) { + return toParameter($this->getTypeHint($this->type)); + } + + return toParameter('mixed'); + } + private function getType(): ReflectionNamedType|ReflectionUnionType|null { $reflection = $this->reflection->getType(); @@ -97,6 +114,23 @@ private function getType(): ReflectionNamedType|ReflectionUnionType|null ); } + private function getTypeHint(object $reflection): string + { + if (method_exists($reflection, 'getName')) { + return $reflection->getName(); + } + if ($reflection instanceof ReflectionUnionType) { + $types = []; + foreach ($reflection->getTypes() as $type) { + $types[] = $this->getTypeHint($type); + } + + return implode('|', $types); + } + + return 'mixed'; + } + /** * @infection-ignore-all */ diff --git a/src/functions.php b/src/functions.php index 0369d01..59ff4db 100644 --- a/src/functions.php +++ b/src/functions.php @@ -37,7 +37,6 @@ use ReflectionAttribute; use ReflectionFunction; use ReflectionMethod; -use ReflectionNamedType; use ReflectionParameter; use Throwable; use function Chevere\Message\message; @@ -137,15 +136,12 @@ function assertNamedArgument( } } -function toUnionParameter(array $types): UnionParameterInterface +function toUnionParameter(string ...$types): UnionParameterInterface { $parameters = []; - - /** @var ReflectionNamedType $type */ foreach ($types as $type) { - $parameters[] = toParameter($type->getName()); + $parameters[] = toParameter($type); } - $parameters = parameters(...$parameters); return new UnionParameter($parameters);