ArgumentsBuilder::buildArguments()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Phact\Container\Builder;
4
5
use Phact\Container\Exceptions\NotFoundException;
6
use Psr\Container\ContainerInterface;
7
8
class ArgumentsBuilder
9
{
10
    /**
11
     * @var ContainerInterface
12
     */
13
    protected $container;
14
15 48
    public function setContainer(ContainerInterface $container): void
16
    {
17 48
        $this->container = $container;
18 48
    }
19
20
    /**
21
     * Build arguments by dependencies and parameters
22
     *
23
     * @param DependencyInterface[] $dependencies
24
     * @param ParameterInterface[] $parameters
25
     * @return array
26
     * @throws NotFoundException
27
     */
28 41
    public function buildArguments(array $dependencies, array $parameters): array
29
    {
30 41
        $arguments = [];
31 41
        if (count($dependencies) > 0) {
32 33
            $arguments = $this->buildArgumentsFromDependencies($dependencies, $parameters);
33
        } else {
34 10
            foreach ($parameters as $parameter) {
35 1
                $arguments[] = $this->makeArgumentByParameter($parameter);
36
            }
37
        }
38 38
        return $arguments;
39
    }
40
41
    /**
42
     * @param DependencyInterface[] $dependencies
43
     * @param ParameterInterface[] $parameters
44
     * @return array
45
     * @throws NotFoundException
46
     */
47 33
    protected function buildArgumentsFromDependencies(array $dependencies, array $parameters): array
48
    {
49 33
        $arguments = [];
50 33
        $usedParameters = [];
51
52 33
        foreach ($dependencies as $key => $dependency) {
53
            /** @var ParameterInterface $parameter */
54 33
            $parameter = null;
0 ignored issues
show
Unused Code introduced by
$parameter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55 33
            if (isset($parameters[$key])) {
56 7
                $parameter = $parameters[$key];
57 7
                $usedParameters[] = $key;
58 7
                $arguments[] = $this->makeArgumentByParameter($parameter);
59 26
            } elseif (isset($parameters[$dependency->getName()])) {
60 10
                $parameter = $parameters[$dependency->getName()];
61 10
                $usedParameters[] = $dependency->getName();
62 10
                $arguments[] = $this->makeArgumentByParameter($parameter);
63
            } else {
64 25
                $arguments[] = $this->makeArgumentByDependency($dependency);
65
            }
66
        }
67
68 30
        $arguments = $this->appendUnusedParamsToArguments($parameters, $arguments, $usedParameters);
69
70 30
        return $arguments;
71
    }
72
73
74
    /**
75
     * @param array $parameters
76
     * @param array $usedParameters
77
     * @param array $arguments
78
     * @return array
79
     * @throws NotFoundException
80
     */
81 30
    protected function appendUnusedParamsToArguments(
82
        array $parameters,
83
        array $arguments,
84
        array $usedParameters = []
85
    ): array {
86 30
        foreach ($parameters as $key => $parameter) {
87 16
            if (!in_array($key, $usedParameters, true)) {
88 1
                $arguments[] = $this->makeArgumentByParameter($parameter);
89
            }
90
        }
91 30
        return $arguments;
92
    }
93
94
    /**
95
     * @param ParameterInterface $parameter
96
     * @return mixed
97
     * @throws NotFoundException
98
     */
99 18 View Code Duplication
    protected function makeArgumentByParameter(ParameterInterface $parameter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101 18
        switch ($parameter->getType()) {
102 18
            case ParameterInterface::TYPE_REFERENCE_REQUIRED:
103 3
                return $this->retrieveRequiredDependencyFromContainer($parameter->getValue());
104
105 16
            case ParameterInterface::TYPE_REFERENCE_OPTIONAL:
106 2
                return $this->retrieveOptionalDependencyFromContainer($parameter->getValue());
107
        }
108 14
        return $parameter->getValue();
109
    }
110
111
    /**
112
     * @param DependencyInterface $dependency
113
     * @return mixed
114
     * @throws NotFoundException
115
     */
116 25 View Code Duplication
    protected function makeArgumentByDependency(DependencyInterface $dependency)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118 25
        switch ($dependency->getType()) {
119 25
            case DependencyInterface::TYPE_REQUIRED:
120 20
                return $this->retrieveRequiredDependencyFromContainer($dependency->getValue());
121
122 10
            case DependencyInterface::TYPE_OPTIONAL:
123 2
                return $this->retrieveOptionalDependencyFromContainer($dependency->getValue());
124
        }
125 8
        return $dependency->getValue();
126
    }
127
128
    /**
129
     * @param $id
130
     * @return mixed
131
     * @throws NotFoundException
132
     */
133 23
    protected function retrieveRequiredDependencyFromContainer($id)
134
    {
135 23
        if ($this->container && $this->container->has($id)) {
136 21
            return $this->container->get($id);
137
        }
138 2
        throw new NotFoundException("There is no referenced classes of {$id} found");
139
    }
140
141
    /**
142
     * @param $id
143
     * @return mixed
144
     */
145 4
    protected function retrieveOptionalDependencyFromContainer($id)
146
    {
147 4
        if ($this->container && $this->container->has($id)) {
148 2
            return $this->container->get($id);
149
        }
150 2
        return null;
151
    }
152
}
153