Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
24 / 24 |
|
12.33% |
9 / 73 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ArgumentResolver | |
100.00% |
33 / 33 |
|
100.00% |
24 / 24 |
|
12.33% |
9 / 73 |
|
100.00% |
3 / 3 |
109.04 | |
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% |
20 / 20 |
|
100.00% |
17 / 17 |
|
7.35% |
5 / 68 |
|
100.00% |
1 / 1 |
58.89 | |||
| resolveInjectedArgs | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
3.14 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Wire; |
| 6 | |
| 7 | use Celemas\Wire\Exception\WireException; |
| 8 | use ReflectionFunctionAbstract; |
| 9 | |
| 10 | final class ArgumentResolver |
| 11 | { |
| 12 | public function __construct( |
| 13 | private readonly CreatorInterface $creator, |
| 14 | ) {} |
| 15 | |
| 16 | public function resolve( |
| 17 | ReflectionFunctionAbstract $rfn, |
| 18 | array $predefinedArgs, |
| 19 | array $predefinedTypes, |
| 20 | ?callable $injectCallback, |
| 21 | ): array { |
| 22 | $injectedArgs = $this->resolveInjectedArgs($rfn, $predefinedTypes, $injectCallback); |
| 23 | |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 25 | throw new WireException('When using Inject attributes, predefined args must be named'); |
| 26 | } |
| 27 | |
| 28 | $combinedArgs = array_merge( |
| 29 | $injectedArgs, |
| 30 | $predefinedArgs, |
| 31 | ); |
| 32 | |
| 33 | $args = []; |
| 34 | $parameters = $rfn->getParameters(); |
| 35 | $countPredefined = count($combinedArgs); |
| 36 | $parameterResolver = new ParameterResolver($this->creator); |
| 37 | |
| 38 | if (array_is_list($combinedArgs) && $countPredefined > 0) { |
| 39 | // predefined args are not named, use them as they are |
| 40 | $args = $combinedArgs; |
| 41 | $parameters = array_slice($parameters, $countPredefined); |
| 42 | } |
| 43 | |
| 44 | foreach ($parameters as $param) { |
| 45 | $name = $param->getName(); |
| 46 | |
| 47 | if (isset($combinedArgs[$name])) { |
| 48 | /** @var list<mixed> */ |
| 49 | $args[] = $combinedArgs[$name]; |
| 50 | } else { |
| 51 | /** @var list<mixed> */ |
| 52 | $args[] = $parameterResolver->resolve($param, $predefinedTypes, $injectCallback); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $args; |
| 57 | } |
| 58 | |
| 59 | /** @return array<non-empty-string, mixed> */ |
| 60 | public function resolveInjectedArgs( |
| 61 | ReflectionFunctionAbstract $rfn, |
| 62 | array $predefinedTypes, |
| 63 | ?callable $injectCallback, |
| 64 | ): array { |
| 65 | $result = []; |
| 66 | |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 68 | $injectAttr = $param->getAttributes(Inject::class)[0] ?? null; |
| 69 | |
| 70 | if ($injectAttr) { |
| 71 | $instance = $injectAttr->newInstance(); |
| 72 | /** @psalm-suppress MixedAssignment */ |
| 73 | $result[$param->name] = Injected::value( |
| 74 | $instance, |
| 75 | $this->creator, |
| 76 | $predefinedTypes, |
| 77 | $injectCallback, |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $result; |
| 83 | } |
| 84 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 13 | private readonly CreatorInterface $creator, |
| 14 | ) {} |
| 17 | ReflectionFunctionAbstract $rfn, |
| 18 | array $predefinedArgs, |
| 19 | array $predefinedTypes, |
| 20 | ?callable $injectCallback, |
| 21 | ): array { |
| 22 | $injectedArgs = $this->resolveInjectedArgs($rfn, $predefinedTypes, $injectCallback); |
| 23 | |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 24 | if ($injectedArgs !== [] && array_is_list($predefinedArgs) && $predefinedArgs !== []) { |
| 25 | throw new WireException('When using Inject attributes, predefined args must be named'); |
| 28 | $combinedArgs = array_merge( |
| 29 | $injectedArgs, |
| 30 | $predefinedArgs, |
| 31 | ); |
| 32 | |
| 33 | $args = []; |
| 34 | $parameters = $rfn->getParameters(); |
| 35 | $countPredefined = count($combinedArgs); |
| 36 | $parameterResolver = new ParameterResolver($this->creator); |
| 37 | |
| 38 | if (array_is_list($combinedArgs) && $countPredefined > 0) { |
| 38 | if (array_is_list($combinedArgs) && $countPredefined > 0) { |
| 38 | if (array_is_list($combinedArgs) && $countPredefined > 0) { |
| 40 | $args = $combinedArgs; |
| 41 | $parameters = array_slice($parameters, $countPredefined); |
| 42 | } |
| 43 | |
| 44 | foreach ($parameters as $param) { |
| 44 | foreach ($parameters as $param) { |
| 44 | foreach ($parameters as $param) { |
| 45 | $name = $param->getName(); |
| 46 | |
| 47 | if (isset($combinedArgs[$name])) { |
| 47 | if (isset($combinedArgs[$name])) { |
| 48 | /** @var list<mixed> */ |
| 49 | $args[] = $combinedArgs[$name]; |
| 44 | foreach ($parameters as $param) { |
| 45 | $name = $param->getName(); |
| 46 | |
| 47 | if (isset($combinedArgs[$name])) { |
| 48 | /** @var list<mixed> */ |
| 49 | $args[] = $combinedArgs[$name]; |
| 50 | } else { |
| 51 | /** @var list<mixed> */ |
| 52 | $args[] = $parameterResolver->resolve($param, $predefinedTypes, $injectCallback); |
| 44 | foreach ($parameters as $param) { |
| 44 | foreach ($parameters as $param) { |
| 45 | $name = $param->getName(); |
| 46 | |
| 47 | if (isset($combinedArgs[$name])) { |
| 48 | /** @var list<mixed> */ |
| 49 | $args[] = $combinedArgs[$name]; |
| 50 | } else { |
| 51 | /** @var list<mixed> */ |
| 52 | $args[] = $parameterResolver->resolve($param, $predefinedTypes, $injectCallback); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $args; |
| 57 | } |
| 61 | ReflectionFunctionAbstract $rfn, |
| 62 | array $predefinedTypes, |
| 63 | ?callable $injectCallback, |
| 64 | ): array { |
| 65 | $result = []; |
| 66 | |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 68 | $injectAttr = $param->getAttributes(Inject::class)[0] ?? null; |
| 69 | |
| 70 | if ($injectAttr) { |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 68 | $injectAttr = $param->getAttributes(Inject::class)[0] ?? null; |
| 69 | |
| 70 | if ($injectAttr) { |
| 71 | $instance = $injectAttr->newInstance(); |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 67 | foreach ($rfn->getParameters() as $param) { |
| 68 | $injectAttr = $param->getAttributes(Inject::class)[0] ?? null; |
| 69 | |
| 70 | if ($injectAttr) { |
| 71 | $instance = $injectAttr->newInstance(); |
| 72 | /** @psalm-suppress MixedAssignment */ |
| 73 | $result[$param->name] = Injected::value( |
| 74 | $instance, |
| 75 | $this->creator, |
| 76 | $predefinedTypes, |
| 77 | $injectCallback, |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $result; |
| 83 | } |