Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
28 / 28 |
|
94.12% |
16 / 17 |
|
72.73% |
8 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ParameterResolver | |
100.00% |
28 / 28 |
|
94.12% |
16 / 17 |
|
72.73% |
8 / 11 |
|
100.00% |
2 / 2 |
9.30 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
100.00% |
27 / 27 |
|
93.75% |
15 / 16 |
|
70.00% |
7 / 10 |
|
100.00% |
1 / 1 |
8.32 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Wire; |
| 6 | |
| 7 | use Celemas\Wire\Exception\WireException; |
| 8 | use ReflectionNamedType; |
| 9 | use ReflectionParameter; |
| 10 | |
| 11 | final class ParameterResolver |
| 12 | { |
| 13 | public function __construct( |
| 14 | private readonly CreatorInterface $creator, |
| 15 | ) {} |
| 16 | |
| 17 | public function resolve( |
| 18 | ReflectionParameter $param, |
| 19 | array $predefinedTypes, |
| 20 | ?callable $injectCallback, |
| 21 | ): mixed { |
| 22 | $type = $param->getType(); |
| 23 | |
| 24 | if ($type instanceof ReflectionNamedType) { |
| 25 | $container = $this->creator->container(); |
| 26 | $typeName = ltrim($type->getName(), '?'); |
| 27 | |
| 28 | if (isset($predefinedTypes[$typeName])) { |
| 29 | return $predefinedTypes[$typeName]; |
| 30 | } |
| 31 | |
| 32 | if ($container?->has($typeName)) { |
| 33 | return $container->get($typeName); |
| 34 | } |
| 35 | |
| 36 | if (class_exists($typeName)) { |
| 37 | return $this->creator->create( |
| 38 | $typeName, |
| 39 | predefinedTypes: $predefinedTypes, |
| 40 | injectCallback: $injectCallback, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | if ($param->isDefaultValueAvailable()) { |
| 45 | return $param->getDefaultValue(); |
| 46 | } |
| 47 | |
| 48 | throw new WireException( |
| 49 | "Unresolvable parameter. Source: \n" . ParameterInfo::info($param), |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | if ($type) { |
| 54 | throw new WireException( |
| 55 | "Cannot resolve union or intersection types. Source: \n" . ParameterInfo::info($param), |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | throw new WireException( |
| 60 | "To be resolvable, classes must have fully typed constructor parameters. Source: \n" |
| 61 | . ParameterInfo::info($param), |
| 62 | ); |
| 63 | } |
| 64 | } |