ParameterResolverChain::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Autowiring\Parameter;
5
6
use Habemus\Autowiring\Attributes\AttributesInjection;
7
use Habemus\Autowiring\Reflector;
8
use Psr\Container\ContainerInterface;
9
use ReflectionParameter;
10
11
class ParameterResolverChain implements ParameterResolver
12
{
13
    /**
14
     * @var ParameterResolver[]
15
     */
16
    protected $chain;
17
18
    public function __construct(ParameterResolver ...$chain)
19
    {
20
        $this->chain = $chain;
21
    }
22
23
    /**
24
     * Stops processing when any of the chain resolves the parameter
25
     * @param ReflectionParameter $parameter
26
     * @param array $arguments
27
     * @param array $result
28
     * @return bool
29
     */
30
    public function resolve(ReflectionParameter $parameter, array $arguments, array &$result): bool
31
    {
32
        foreach ($this->chain as $resolver) {
33
            if ($resolver->resolve($parameter, $arguments, $result)) {
34
                return true;
35
            }
36
        }
37
38
        return false;
39
    }
40
41
    public static function default(
42
        ContainerInterface $container,
43
        AttributesInjection $injection,
44
        Reflector $reflector
45
    ): self {
46
        return new self(
47
            new UserDefinedParameterResolver(),
48
            new InjectionParameterResolver($container, $injection),
49
            new DefaultValueParameterResolver(),
50
            new NullableParameterResolver(),
51
            new OptionalParameterResolver(),
52
            new TypeHintParameterResolver($container, $reflector)
53
        );
54
    }
55
}
56