Completed
Push — 3.x ( cf16f4...dd3fa1 )
by Phil
50:52 queued 49:27
created

ArgumentResolverTrait::resolveArguments()   B

Complexity

Conditions 11
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 11.0055

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 27
cts 28
cp 0.9643
rs 7.3166
c 0
b 0
f 0
cc 11
nc 1
nop 1
crap 11.0055

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace League\Container\Argument;
4
5
use League\Container\Container;
6
use League\Container\Exception\{ContainerException, NotFoundException};
7
use League\Container\ReflectionContainer;
8
use Psr\Container\ContainerInterface;
9
use ReflectionFunctionAbstract;
10
use ReflectionParameter;
11
12
trait ArgumentResolverTrait
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17 34
    public function resolveArguments(array $arguments) : array
18
    {
19 68
        return array_map(function ($argument) {
20 75
            $justStringValue = false;
21
22 75
            if ($argument instanceof RawArgumentInterface) {
23 12
                return $argument->getValue();
24 72
            } elseif ($argument instanceof ClassNameInterface) {
25 36
                $id = $argument->getClassName();
26 36
            } elseif (!is_string($argument)) {
27 12
                return $argument;
28
            } else {
29 27
                $justStringValue = true;
30 27
                $id = $argument;
31
            }
32
33 63
            $container = null;
34
35
            try {
36 63
                $container = $this->getLeagueContainer();
37 27
            } catch (ContainerException $e) {
38 27
                if ($this instanceof ReflectionContainer) {
39 18
                    $container = $this;
40
                }
41
            }
42
43 63
            if ($container !== null) {
44
                try {
45 57
                    return $container->get($id);
46 9
                } catch (NotFoundException $exception) {
47 9
                    if ($argument instanceof ClassNameWithOptionalValue) {
48
                        return $argument->getOptionalValue();
49
                    }
50
51 9
                    if ($justStringValue) {
52 6
                        return $id;
53
                    }
54
55 3
                    throw $exception;
56
                }
57
            }
58
59 9
            if ($argument instanceof ClassNameWithOptionalValue) {
60 3
                return $argument->getOptionalValue();
61
            }
62
63
            // Just a string value.
64 6
            return $id;
65 102
        }, $arguments);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 14
    public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []) : array
72
    {
73 28
        $arguments = array_map(function (ReflectionParameter $param) use ($method, $args) {
74 42
            $name = $param->getName();
75 42
            $type = $param->getType();
76
77 42
            if (array_key_exists($name, $args)) {
78 6
                return new RawArgument($args[$name]);
79
            }
80
81 39
            if ($type) {
82 33
                if (PHP_VERSION_ID >= 70200) {
83 11
                    $typeName = $type->getName();
84
                } else {
85 22
                    $typeName = (string) $type;
86
                }
87
88 33
                $typeName = ltrim($typeName, '?');
89
90 33
                if ($param->isDefaultValueAvailable()) {
91 21
                    return new ClassNameWithOptionalValue($typeName, $param->getDefaultValue());
92
                }
93
94 18
                return new ClassName($typeName);
95
            }
96
97 12
            if ($param->isDefaultValueAvailable()) {
98 6
                return new RawArgument($param->getDefaultValue());
99
            }
100
101 6
            throw new NotFoundException(sprintf(
102 6
                'Unable to resolve a value for parameter (%s) in the function/method (%s)',
103 4
                $name,
104 6
                $method->getName()
105
            ));
106 42
        }, $method->getParameters());
107
108 36
        return $this->resolveArguments($arguments);
109
    }
110
111
    /**
112
     * @return ContainerInterface
113
     */
114
    abstract public function getContainer() : ContainerInterface;
115
116
    /**
117
     * @return Container
118
     */
119
    abstract public function getLeagueContainer() : Container;
120
}
121