ArgumentResolverTrait   A
last analyzed

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 13
lcom 0
cbo 2
ccs 35
cts 35
cp 1
rs 10

3 Methods

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