Passed
Pull Request — master (#78)
by Sergei
02:43
created

ContainerParameterResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 25
dl 0
loc 72
ccs 22
cts 25
cp 0.88
rs 10
c 3
b 2
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolveType() 0 17 5
A resolve() 0 15 4
A resolveNamedType() 0 10 3
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
            $reflectionType = $parameter->getType();
37 24
            if ($reflectionType === null) {
38
                throw new ParameterNotResolvedException();
39
            }
40 24
            return $this->resolveType($reflectionType);
41
        }
42
43 2
        throw new ParameterNotResolvedException();
44
    }
45
46
    /**
47
     * @throws ParameterNotResolvedException
48
     * @throws ContainerExceptionInterface
49
     *
50
     * @return mixed
51
     */
52 24
    private function resolveType(ReflectionType $type)
53
    {
54 24
        if ($type instanceof ReflectionNamedType) {
55 23
            return $this->resolveNamedType($type);
56
        }
57
58 1
        if ($type instanceof ReflectionUnionType) {
59
            /** @var ReflectionNamedType $namedType */
60 1
            foreach ($type->getTypes() as $namedType) {
61
                try {
62 1
                    return $this->resolveNamedType($namedType);
63
                } catch (ParameterNotResolvedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
                }
65
            }
66
        }
67
68
        throw new ParameterNotResolvedException();
69
    }
70
71
    /**
72
     * @throws ParameterNotResolvedException
73
     * @throws ContainerExceptionInterface
74
     *
75
     * @return mixed
76
     */
77 24
    private function resolveNamedType(ReflectionNamedType $parameter)
78
    {
79 24
        if ($parameter->isBuiltin()) {
80 9
            throw new ParameterNotResolvedException();
81
        }
82
83
        try {
84 18
            return $this->container->get($parameter->getName());
85 5
        } catch (NotFoundExceptionInterface $e) {
86 5
            throw new ParameterNotResolvedException();
87
        }
88
    }
89
}
90