|
19 | 19 | use ReflectionMethod;
|
20 | 20 | use ReflectionNamedType;
|
21 | 21 | use ReflectionParameter;
|
| 22 | +use ReflectionUnionType; |
22 | 23 |
|
23 | 24 | use function array_keys;
|
24 | 25 | use function array_replace;
|
25 | 26 | use function array_values;
|
| 27 | +use function count; |
26 | 28 | use function is_array;
|
27 | 29 | use function is_int;
|
28 | 30 | use function vsprintf;
|
@@ -254,14 +256,35 @@ protected function resolveParameter(ReflectionParameter $parameter, ?ReflectionC
|
254 | 256 |
|
255 | 257 | $parameterClassName = null;
|
256 | 258 |
|
257 |
| - if ($parameterType instanceof ReflectionNamedType && !$parameterType->isBuiltin()) { |
258 |
| - $parameterClassName = $parameterType->getName(); |
259 |
| - } |
260 |
| - elseif ($parameterType instanceof ReflectionIntersectionType) { |
261 |
| - $parameterClassName = (string) $parameterType; |
| 259 | + if ($parameterType !== null) { |
| 260 | + if ($parameterType instanceof ReflectionNamedType) { |
| 261 | + if (!$parameterType->isBuiltin()) { |
| 262 | + $parameterClassName = $parameterType->getName(); |
| 263 | + } |
| 264 | + } |
| 265 | + else { |
| 266 | + // Checking if we have a intersection type |
| 267 | + |
| 268 | + if ($parameterType instanceof ReflectionIntersectionType) { |
| 269 | + $parameterClassName = (string) $parameterType; |
| 270 | + } |
| 271 | + |
| 272 | + // Nullable intersection types will be detected as a union type so we'll have to dig deeper |
262 | 273 |
|
263 |
| - if (!$this->has($parameterClassName)) { |
264 |
| - $parameterClassName = null; |
| 274 | + elseif ($parameterType instanceof ReflectionUnionType && $parameterType->allowsNull()) { |
| 275 | + $intersection = array_filter($parameterType->getTypes(), fn ($type) => $type instanceof ReflectionIntersectionType); |
| 276 | + |
| 277 | + if (count($intersection) === 1) { |
| 278 | + $parameterClassName = (string) $intersection[0]; |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + // If the intersection type isn't registered in the container |
| 283 | + // then we'll just set the classname back to null |
| 284 | + |
| 285 | + if ($parameterClassName !== null && !$this->has($parameterClassName)) { |
| 286 | + $parameterClassName = null; |
| 287 | + } |
265 | 288 | }
|
266 | 289 | }
|
267 | 290 |
|
|
0 commit comments