Passed
Pull Request — master (#240)
by Dmitriy
02:22
created

CompositeContainer::get()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.1043

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 38
ccs 19
cts 21
cp 0.9048
crap 11.1043
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 28
    public function get($id)
24
    {
25
        /** @psalm-suppress TypeDoesNotContainType */
26 28
        if (!is_string($id)) {
27
            throw new \InvalidArgumentException("Id must be a string, {$this->getVariableType($id)} given.");
28
        }
29
30 28
        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
            return new StateResetter($resetters, $this);
38
        }
39
40 28
        if ($this->isTagAlias($id)) {
41 2
            $tags = [];
42 2
            foreach ($this->containers as $container) {
43 2
                if (!$container instanceof Container) {
44
                    continue;
45
                }
46 2
                if ($container->has($id)) {
47 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

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