Skip to content

Commit

Permalink
Merge pull request #8 from mikeweb85/enhancement/union-reflection-sup…
Browse files Browse the repository at this point in the history
…port

support union type in reflection method
  • Loading branch information
rodber authored Jan 3, 2025
2 parents ce419c1 + 91234f9 commit f8800e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/ReflectionParameterTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@

final class ReflectionParameterTyped implements ReflectionParameterTypedInterface
{
private ?ReflectionNamedType $type;
private ReflectionUnionType|ReflectionNamedType|null $type;

private ParameterInterface $parameter;

public function __construct(
private ReflectionParameter $reflection
) {
$this->type = $this->getType();
$parameter = toParameter($this->type?->getName() ?? 'mixed');
$parameter = ($this->type instanceof ReflectionUnionType) ?
toUnionParameter($this->type->getTypes()) :
toParameter($this->type?->getName() ?? 'mixed');

try {
$attribute = reflectedParameterAttribute($reflection);
Expand Down Expand Up @@ -74,13 +76,13 @@ public function parameter(): ParameterInterface
return $this->parameter;
}

private function getType(): ?ReflectionNamedType
private function getType(): ReflectionNamedType|ReflectionUnionType|null
{
$reflection = $this->reflection->getType();
if ($reflection === null) {
return null;
}
if ($reflection instanceof ReflectionNamedType) {
if ($reflection instanceof ReflectionNamedType || $reflection instanceof ReflectionUnionType) {
return $reflection;
}
$name = '$' . $this->reflection->getName();
Expand Down
15 changes: 15 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use ReflectionAttribute;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use Throwable;
use function Chevere\Message\message;
Expand Down Expand Up @@ -136,6 +137,20 @@ function assertNamedArgument(
}
}

function toUnionParameter(array $types): UnionParameterInterface

Check failure on line 140 in src/functions.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-24.04

Function Chevere\Parameter\toUnionParameter() has parameter $types with no value type specified in iterable type array.

Check failure on line 140 in src/functions.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-24.04

Function Chevere\Parameter\toUnionParameter() has parameter $types with no value type specified in iterable type array.

Check failure on line 140 in src/functions.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-24.04

Function Chevere\Parameter\toUnionParameter() has parameter $types with no value type specified in iterable type array.

Check failure on line 140 in src/functions.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 test on ubuntu-24.04

Function Chevere\Parameter\toUnionParameter() has parameter $types with no value type specified in iterable type array.
{
$parameters = [];

/** @var ReflectionNamedType $type */
foreach ($types as $type) {
$parameters[] = toParameter($type->getName());
}

$parameters = parameters(...$parameters);

return new UnionParameter($parameters);
}

function toParameter(string $type): ParameterInterface
{
$class = TypeInterface::TYPE_TO_PARAMETER[$type]
Expand Down
7 changes: 4 additions & 3 deletions tests/ReflectionParameterTypedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Chevere\Parameter\Interfaces\NullParameterInterface;
use Chevere\Parameter\Interfaces\ObjectParameterInterface;
use Chevere\Parameter\Interfaces\StringParameterInterface;
use Chevere\Parameter\Interfaces\UnionParameterInterface;
use Chevere\Parameter\ReflectionParameterTyped;
use Chevere\Tests\src\Depends;
use LogicException;
Expand Down Expand Up @@ -72,9 +73,9 @@ public function testParameterDefault(): void
public function testUnion(): void
{
$parameter = $this->getReflection('useUnion');
$this->expectException(LogicException::class);
$this->expectExceptionMessage('$union of type union is not supported');
new ReflectionParameterTyped($parameter);
$reflection = new ReflectionParameterTyped($parameter);
$reflected = $reflection->parameter();
$this->assertInstanceOf(UnionParameterInterface::class, $reflected);
}

public function testIntersection(): void
Expand Down

0 comments on commit f8800e2

Please sign in to comment.