From ae6d3f955b979752848e07da1b758fe9bf51977a Mon Sep 17 00:00:00 2001 From: Yoshitaka Jingu Date: Tue, 9 Apr 2024 17:10:35 +0900 Subject: [PATCH] Support nullable param in ParamInjector --- src/ParamInjector.php | 3 ++- tests/FakeParamInjectMethod.php | 2 +- tests/ParamInjectorTest.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ParamInjector.php b/src/ParamInjector.php index bb8a8b9a..a2de4826 100644 --- a/src/ParamInjector.php +++ b/src/ParamInjector.php @@ -37,7 +37,8 @@ public function getArgumentes(MethodInvocation $invocation): array foreach ($parameters as $parameter) { $pos = $parameter->getPosition(); /** @psalm-suppress MixedAssignment */ - $namedArgs[$parameter->getName()] = $args[$pos] ?? $this->getInjectedParam($parameter); + $namedArgs[$parameter->getName()] = array_key_exists($pos, $args) + ? $args[$pos] : $this->getInjectedParam($parameter); } return $namedArgs; diff --git a/tests/FakeParamInjectMethod.php b/tests/FakeParamInjectMethod.php index 7f682524..11cf70a6 100644 --- a/tests/FakeParamInjectMethod.php +++ b/tests/FakeParamInjectMethod.php @@ -12,7 +12,7 @@ public function noInject(int $a): void { } - public function paramInject(DateTimeInterface|null $dateTime = null): void + public function paramInject(string|null $nullableString, DateTimeInterface|null $dateTime = null): void { } diff --git a/tests/ParamInjectorTest.php b/tests/ParamInjectorTest.php index dac07bb4..9fb7e96e 100644 --- a/tests/ParamInjectorTest.php +++ b/tests/ParamInjectorTest.php @@ -33,7 +33,7 @@ public function testNoInjection(): void public function testGetArgumentes(): void { - $namedArgs = $this->injector->getArgumentes(new ReflectiveMethodInvocation(new FakeParamInjectMethod(), 'paramInject', [])); + $namedArgs = $this->injector->getArgumentes(new ReflectiveMethodInvocation(new FakeParamInjectMethod(), 'paramInject', [null])); $this->assertInstanceOf(DateTimeImmutable::class, $namedArgs['dateTime']); }