Passed
Push — master ( e0690f...bab3ce )
by Alexander
02:28
created

CompositeContainer::getVariableType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
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