ArgumentResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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