Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changelog/callable-composite-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: fixed
type: patch
---

Rejects union and intersection callable parameters with the documented invalid-type exception instead of an undefined reflection method error.
4 changes: 2 additions & 2 deletions src/Factory/CallableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use Closure;
use ReflectionFunction;
use ReflectionType;
use ReflectionNamedType;
use FastForward\Container\Exception\RuntimeException;
use Psr\Container\ContainerInterface;

Expand Down Expand Up @@ -77,7 +77,7 @@ private function getArguments(ContainerInterface $container, ReflectionFunction
$arguments = [];

foreach ($function->getParameters() as $parameter) {
if (! $parameter->getType() instanceof ReflectionType || $parameter->getType()->isBuiltin()) {
if (! $parameter->getType() instanceof ReflectionNamedType || $parameter->getType()->isBuiltin()) {
throw RuntimeException::forInvalidParameterType($parameter->getName());
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Factory/CallableFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,52 @@ public function invokeWillThrowRuntimeExceptionIfParameterIsNotAClass(): void

$factory($container);
}
#[Test]
public function unionParameterReceivesInvalidTypeDiagnostic(): void
{
$factory = new CallableFactory(static fn(ContainerInterface|FactoryInterface $dependency) => $dependency);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Parameter "dependency" is not a valid type.');
$factory($this->prophesize(ContainerInterface::class)->reveal());
}

#[Test]
public function intersectionParameterReceivesInvalidTypeDiagnostic(): void
{
$factory = new CallableFactory(static fn(ContainerInterface&FactoryInterface $dependency) => $dependency);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Parameter "dependency" is not a valid type.');
$factory($this->prophesize(ContainerInterface::class)->reveal());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

#[Test]
public function nullableNamedParameterStillResolvesDependency(): void
{
$container = $this->prophesize(ContainerInterface::class);
$dependency = $this->prophesize(FactoryInterface::class)->reveal();
$container->get(FactoryInterface::class)->willReturn($dependency);
$factory = new CallableFactory(static fn(?FactoryInterface $dependency) => $dependency);
self::assertSame($dependency, $factory($container->reveal()));
}

#[Test]
public function dnfParameterReceivesInvalidTypeDiagnostic(): void
{
$factory = new CallableFactory(
static fn((ContainerInterface&FactoryInterface)|ServiceProviderInterface $dependency) => $dependency
);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Parameter "dependency" is not a valid type.');
$factory($this->prophesize(ContainerInterface::class)->reveal());
}

#[Test]
public function untypedParameterReceivesInvalidTypeDiagnostic(): void
{
$factory = new CallableFactory(static fn($dependency) => $dependency);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Parameter "dependency" is not a valid type.');
$factory($this->prophesize(ContainerInterface::class)->reveal());
}

}
Loading