ArgumentResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveArguments() 0 19 4
1
<?php
2
3
/**
4
 * Bridge Spiral-Core
5
 *
6
 * @author Vlad Shashkov <[email protected]>
7
 * @copyright Copyright (c) 2021, The Myaza Software
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bridge\Core;
13
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use ReflectionFunctionAbstract as ContextFunction;
17
use Spiral\Core\Exception\Container\ContainerException;
18
use Spiral\Core\ResolverInterface;
19
20
final class ArgumentResolver implements ResolverInterface
21
{
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * ArgumentResolver constructor.
29
     */
30 3
    public function __construct(ContainerInterface $container)
31
    {
32 3
        $this->container = $container;
33 3
    }
34
35
    /**
36
     * @param array<string,mixed> $parameters
37
     *
38
     * @throws ContainerExceptionInterface
39
     *
40
     * @return array<string,mixed>
41
     */
42 3
    public function resolveArguments(ContextFunction $reflection, array $parameters = []): array
43
    {
44 3
        $arguments = [];
45
46 3
        foreach ($reflection->getParameters() as $parameter) {
47 3
            $type = $parameter->getType();
48
49 3
            if (null === $type) {
50 1
                throw new ContainerException(sprintf('Please set type param: %s', $parameter->getName()));
51
            }
52
53 2
            if (!$type instanceof \ReflectionNamedType) {
54 1
                throw new ContainerException(sprintf('Not supported union type, param:%s', $parameter->getName()));
55
            }
56
57 1
            $arguments[$parameter->getName()] = $this->container->get($type->getName());
58
        }
59
60 1
        return $arguments;
61
    }
62
}
63