Passed
Pull Request — master (#248)
by Dmitriy
02:20
created

CompositeContainer::get()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.0795

Importance

Changes 0
Metric Value
cc 11
eloc 22
c 0
b 0
f 0
nc 11
nop 1
dl 0
loc 41
ccs 21
cts 23
cp 0.913
crap 11.0795
rs 7.3166

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
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Exception\NotFoundException;
9
10
/**
11
 * This class implements a composite container for use with containers that support the delegate lookup feature.
12
 * The goal of the implementation is simplicity.
13
 */
14
final class CompositeContainer implements ContainerInterface
15
{
16
    /**
17
     * Containers to look into starting from the beginning of the array.
18
     *
19
     * @var ContainerInterface[] The list of containers
20
     */
21
    private array $containers = [];
22
23 25
    public function get($id)
24
    {
25
        /** @psalm-suppress TypeDoesNotContainType */
26 25
        if (!is_string($id)) {
27
            throw new \InvalidArgumentException("Id must be a string, {$this->getVariableType($id)} given.");
28
        }
29
30 25
        if ($id === StateResetter::class) {
31 2
            $resetters = [];
32 2
            foreach ($this->containers as $container) {
33 2
                if ($container->has(StateResetter::class)) {
34 2
                    $resetters[] = $container->get(StateResetter::class);
35
                }
36
            }
37 2
            $stateResetter = new StateResetter($this);
38 2
            $stateResetter->setResetters($resetters);
39
40 2
            return $stateResetter;
41
        }
42
43 25
        if ($this->isTagAlias($id)) {
44 2
            $tags = [];
45 2
            foreach ($this->containers as $container) {
46 2
                if (!$container instanceof Container) {
47
                    continue;
48
                }
49 2
                if ($container->has($id)) {
50 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

50
                    $tags = array_merge(/** @scrutinizer ignore-type */ $container->get($id), $tags);
Loading history...
51
                }
52
            }
53
54 2
            return $tags;
55
        }
56
57 23
        foreach ($this->containers as $container) {
58 23
            if ($container->has($id)) {
59 20
                return $container->get($id);
60
            }
61
        }
62
63 5
        throw new NotFoundException($id);
64
    }
65
66 14
    public function has($id): bool
67
    {
68 14
        foreach ($this->containers as $container) {
69 5
            if ($container->has($id)) {
70 5
                return true;
71
            }
72
        }
73 9
        return false;
74
    }
75
76
    /**
77
     * Attaches a container to the composite container.
78
     *
79
     * @param ContainerInterface $container
80
     */
81 27
    public function attach(ContainerInterface $container): void
82
    {
83 27
        $this->containers[] = $container;
84 27
    }
85
86
    /**
87
     * Removes a container from the list of containers.
88
     *
89
     * @param ContainerInterface $container
90
     */
91 2
    public function detach(ContainerInterface $container): void
92
    {
93 2
        foreach ($this->containers as $i => $c) {
94 2
            if ($container === $c) {
95 2
                unset($this->containers[$i]);
96
            }
97
        }
98 2
    }
99
100 25
    private function isTagAlias(string $id): bool
101
    {
102 25
        return strpos($id, 'tag@') === 0;
103
    }
104
105
    /**
106
     * @param mixed $variable
107
     */
108
    private function getVariableType($variable): string
109
    {
110
        return is_object($variable) ? get_class($variable) : gettype($variable);
111
    }
112
}
113