Passed
Pull Request — master (#78)
by Sergei
03:08 queued 43s
created

ContainerParameterResolver::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 3
eloc 5
c 3
b 2
f 0
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Injector\ParameterResolver;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use ReflectionNamedType;
11
use ReflectionParameter;
12
use ReflectionType;
13
use ReflectionUnionType;
14
15
final class ContainerParameterResolver implements ParameterResolverInterface
16
{
17
    private ContainerInterface $container;
18
19 54
    public function __construct(ContainerInterface $container)
20
    {
21 54
        $this->container = $container;
22
    }
23
24
    /**
25
     * @throws ParameterNotResolvedException
26
     * @throws NotFoundExceptionInterface
27
     * @throws ContainerExceptionInterface
28
     */
29 26
    public function resolve(ReflectionParameter $parameter)
30
    {
31 26
        if ($parameter->isVariadic()) {
32 1
            throw new ParameterNotResolvedException();
33
        }
34
35 25
        if ($parameter->hasType()) {
36 24
            return $this->resolveType($parameter->getType());
37
        }
38
39 2
        throw new ParameterNotResolvedException();
40
    }
41
42
    /**
43
     * @throws ParameterNotResolvedException
44
     * @throws ContainerExceptionInterface
45
     *
46
     * @return mixed
47
     */
48 24
    private function resolveType(ReflectionType $type)
49
    {
50 24
        if ($type instanceof ReflectionNamedType) {
51 23
            return $this->resolveNamedType($type);
52
        }
53
54 1
        if ($type instanceof ReflectionUnionType) {
55
            /** @var ReflectionNamedType $namedType */
56 1
            foreach ($type->getTypes() as $namedType) {
57
                try {
58 1
                    return $this->resolveNamedType($namedType);
59
                } catch (ParameterNotResolvedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
60
                }
61
            }
62
        }
63
64
        throw new ParameterNotResolvedException();
65
    }
66
67
    /**
68
     * @throws ParameterNotResolvedException
69
     * @throws ContainerExceptionInterface
70
     *
71
     * @return mixed
72
     */
73 24
    private function resolveNamedType(ReflectionNamedType $parameter)
74
    {
75 24
        if ($parameter->isBuiltin()) {
76 9
            throw new ParameterNotResolvedException();
77
        }
78
79
        try {
80 18
            return $this->container->get($parameter->getName());
81 5
        } catch (NotFoundExceptionInterface $e) {
82 5
            throw new ParameterNotResolvedException();
83
        }
84
    }
85
}
86