Passed
Pull Request — master (#274)
by Alexander
07:59
created

CompositeContainer::get()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 13.0615

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 28
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 52
ccs 26
cts 28
cp 0.9286
crap 13.0615
rs 6.6166

How to fix   Long Method    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
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use InvalidArgumentException;
8
use Psr\Container\ContainerInterface;
9
use Throwable;
10
use function get_class;
11
use function gettype;
12
use function is_object;
13
use function is_string;
14
15
/**
16
 * A composite container for use with containers that support the delegate lookup feature.
17
 */
18
final class CompositeContainer implements ContainerInterface
19
{
20
    /**
21
     * Containers to look into starting from the beginning of the array.
22
     *
23
     * @var ContainerInterface[] The list of containers.
24
     */
25
    private array $containers = [];
26
27 27
    public function get($id)
28
    {
29
        /** @psalm-suppress TypeDoesNotContainType */
30 27
        if (!is_string($id)) {
31
            throw new InvalidArgumentException("Id must be a string, {$this->getVariableType($id)} given.");
32
        }
33
34 27
        if ($id === StateResetter::class) {
35 2
            $resetters = [];
36 2
            foreach ($this->containers as $container) {
37 2
                if ($container->has(StateResetter::class)) {
38 2
                    $resetters[] = $container->get(StateResetter::class);
39
                }
40
            }
41 2
            $stateResetter = new StateResetter($this);
42 2
            $stateResetter->setResetters($resetters);
43
44 2
            return $stateResetter;
45
        }
46
47 27
        if ($this->isTagAlias($id)) {
48 2
            $tags = [];
49 2
            foreach ($this->containers as $container) {
50 2
                if (!$container instanceof Container) {
51
                    continue;
52
                }
53 2
                if ($container->has($id)) {
54 2
                    $tags = array_merge($container->get($id), $tags);
0 ignored issues
show
Bug introduced by
It seems like $container->get($id) can also be of type object; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
                    $tags = array_merge(/** @scrutinizer ignore-type */ $container->get($id), $tags);
Loading history...
55
                }
56
            }
57
58 2
            return $tags;
59
        }
60
61 25
        foreach ($this->containers as $container) {
62 25
            if ($container->has($id)) {
63 20
                return $container->get($id);
64
            }
65
        }
66
67
68
        // Collect details from containers
69 7
        $exceptions = [];
70 7
        foreach ($this->containers as $container) {
71
            try {
72 5
                $container->get($id);
73 5
            } catch (Throwable $t) {
74 5
                $exceptions[] = $t;
75
            }
76
        }
77
78 7
        throw new CompositeNotFoundException($exceptions);
79
    }
80
81 20
    public function has($id): bool
82
    {
83 20
        foreach ($this->containers as $container) {
84 5
            if ($container->has($id)) {
85 5
                return true;
86
            }
87
        }
88 15
        return false;
89
    }
90
91
    /**
92
     * Attaches a container to the composite container.
93
     *
94
     * @param ContainerInterface $container
95
     */
96 29
    public function attach(ContainerInterface $container): void
97
    {
98 29
        $this->containers[] = $container;
99 29
    }
100
101
    /**
102
     * Removes a container from the list of containers.
103
     *
104
     * @param ContainerInterface $container
105
     */
106 2
    public function detach(ContainerInterface $container): void
107
    {
108 2
        foreach ($this->containers as $i => $c) {
109 2
            if ($container === $c) {
110 2
                unset($this->containers[$i]);
111
            }
112
        }
113 2
    }
114
115 27
    private function isTagAlias(string $id): bool
116
    {
117 27
        return strncmp($id, 'tag@', 4) === 0;
118
    }
119
120
    /**
121
     * @param mixed $variable
122
     */
123
    private function getVariableType($variable): string
124
    {
125
        return is_object($variable) ? get_class($variable) : gettype($variable);
126
    }
127
}
128