ArgumentResolverTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 4
dl 0
loc 82
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getContainer() 0 1 ?
B resolveArguments() 0 40 10
A reflectArguments() 0 27 4
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Argument;
4
5
use League\Container\Exception\{ContainerException, NotFoundException};
6
use League\Container\ReflectionContainer;
7
use Psr\Container\ContainerInterface;
8
use ReflectionFunctionAbstract;
9
use ReflectionParameter;
10
11
trait ArgumentResolverTrait
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 96
    public function resolveArguments(array $arguments) : array
17
    {
18 96
        foreach ($arguments as &$arg) {
19 69
            if ($arg instanceof RawArgumentInterface) {
20 3
                $arg = $arg->getValue();
21 3
                continue;
22
            }
23
24 69
            if ($arg instanceof ClassNameInterface) {
25 3
                $arg = $arg->getValue();
26
            }
27
28 69
            if (! is_string($arg)) {
29 15
                 continue;
30
            }
31
32 57
            $container = null;
33
34
            try {
35 57
                $container = $this->getContainer();
36 15
            } catch (ContainerException $e) {
37 15
                if ($this instanceof ReflectionContainer) {
38 9
                    $container = $this;
39
                }
40
            }
41
42
43 57
            if (! is_null($container) && $container->has($arg)) {
44 48
                $arg = $container->get($arg);
45
46 48
                if ($arg instanceof RawArgumentInterface) {
47 3
                    $arg = $arg->getValue();
48
                }
49
50 57
                continue;
51
            }
52
        }
53
54 96
        return $arguments;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array
61
    {
62 36
        $arguments = array_map(function (ReflectionParameter $param) use ($method, $args) {
63 36
            $name  = $param->getName();
64 36
            $class = $param->getClass();
65
66 36
            if (array_key_exists($name, $args)) {
67 6
                return $args[$name];
68
            }
69
70 33
            if (! is_null($class)) {
71 27
                return $class->getName();
72
            }
73
74 12
            if ($param->isDefaultValueAvailable()) {
75 6
                return $param->getDefaultValue();
76
            }
77
78 6
            throw new NotFoundException(sprintf(
79 6
                'Unable to resolve a value for parameter (%s) in the function/method (%s)',
80 6
                $name,
81 6
                $method->getName()
82
            ));
83 36
        }, $method->getParameters());
84
85 30
        return $this->resolveArguments($arguments);
86
    }
87
88
    /**
89
     * @return \Psr\Container\ContainerInterface
90
     */
91
    abstract public function getContainer() : ContainerInterface;
92
}
93