Skip to content

Commit b3351f9

Browse files
committed
Add support for nullable default parameters
1 parent 35192eb commit b3351f9

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

src/Resolver/ConstructorResolver.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Container\ContainerInterface;
88
use ReflectionClass;
99
use ReflectionMethod;
10+
use ReflectionParameter;
1011
use Selective\Container\Exceptions\InvalidDefinitionException;
1112
use Throwable;
1213

@@ -51,11 +52,12 @@ public function resolve(string $id)
5152
$reflectionClass = new ReflectionClass($id);
5253

5354
try {
54-
if ($reflectionClass->getConstructor() === null) {
55+
$constructor = $reflectionClass->getConstructor();
56+
if ($constructor === null) {
5557
return $reflectionClass->newInstance();
5658
}
5759

58-
return $reflectionClass->newInstanceArgs($this->resolveParameters($id, $reflectionClass->getConstructor()));
60+
return $reflectionClass->newInstanceArgs($this->resolveParameters($id, $constructor));
5961
} catch (Throwable $exception) {
6062
throw InvalidDefinitionException::create(sprintf(
6163
'Entry "%s" cannot be resolved: the class is not instantiable',
@@ -70,8 +72,6 @@ public function resolve(string $id)
7072
* @param string $id The id
7173
* @param ReflectionMethod $method The method
7274
*
73-
* @throws InvalidDefinitionException
74-
*
7575
* @return array<mixed> The resolved parameters
7676
*/
7777
private function resolveParameters(string $id, ReflectionMethod $method = null): array
@@ -83,28 +83,45 @@ private function resolveParameters(string $id, ReflectionMethod $method = null):
8383
$arguments = [];
8484

8585
foreach ($method->getParameters() as $parameter) {
86-
$reflectionClass = $parameter->getClass();
87-
88-
if ($reflectionClass === null) {
89-
throw InvalidDefinitionException::create(sprintf(
90-
'Parameter $%s of %s has no value defined or guessable',
91-
$parameter->getName(),
92-
$id
93-
));
94-
}
86+
$arguments[] = $this->resolveParameter($id, $parameter);
87+
}
9588

96-
// If the parameter is optional and wasn't specified, we take its default value
97-
if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) {
98-
$arguments[] = $parameter->getDefaultValue();
89+
return $arguments;
90+
}
9991

100-
continue;
101-
}
92+
/**
93+
* Resolve paramameter value.
94+
*
95+
* @param string $id The id
96+
* @param ReflectionParameter $parameter The parameter
97+
*
98+
* @throws InvalidDefinitionException
99+
*
100+
* @return mixed The value
101+
*/
102+
private function resolveParameter(string $id, ReflectionParameter $parameter)
103+
{
104+
$reflectionClass = $parameter->getClass();
102105

106+
if ($reflectionClass !== null) {
103107
// Look in the definitions or try to create it
104-
$arguments[] = $this->container->get($reflectionClass->getName());
108+
$className = $reflectionClass->getName();
109+
110+
if ($this->container->has($className)) {
111+
return $this->container->get($className);
112+
}
105113
}
106114

107-
return $arguments;
115+
// If the parameter is optional and wasn't specified, we take its default value
116+
if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) {
117+
return $parameter->getDefaultValue();
118+
}
119+
120+
throw InvalidDefinitionException::create(sprintf(
121+
'Parameter $%s of %s has no value defined or guessable',
122+
$parameter->getName(),
123+
$id
124+
));
108125
}
109126

110127
/**

0 commit comments

Comments
 (0)