Passed
Pull Request — master (#144)
by Dmitriy
10:46
created

CompositeContainer::get()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.049

Importance

Changes 0
Metric Value
cc 7
eloc 12
c 0
b 0
f 0
nc 7
nop 1
dl 0
loc 22
ccs 9
cts 10
cp 0.9
crap 7.049
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Exceptions\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
     * @var ContainerInterface[] The list of containers
19
     */
20
    private array $containers = [];
21
22 25
    public function get($id)
23
    {
24 25
        if ($this->isTagAlias($id)) {
25 25
            $tags = [];
26 22
            foreach ($this->containers as $container) {
27
                if (!$container instanceof Container) {
28
                    continue;
29 5
                }
30
                if ($container->has($id)) {
31
                    $tags = array_merge($container->get($id), $tags);
0 ignored issues
show
Bug introduced by
$container->get($id) of type object is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

31
                    $tags = array_merge(/** @scrutinizer ignore-type */ $container->get($id), $tags);
Loading history...
32 9
                }
33
            }
34 9
35 9
            return $tags;
36 9
        }
37
38
        foreach ($this->containers as $container) {
39
            if ($container->has($id)) {
40
                return $container->get($id);
41
            }
42
        }
43
        throw new NotFoundException("No definition for $id");
44
    }
45
46 27
    public function has($id): bool
47
    {
48 27
        foreach ($this->containers as $container) {
49 27
            if ($container->has($id)) {
50
                return true;
51
            }
52
        }
53
        return false;
54
    }
55 2
56
    /**
57 2
     * Attaches a container to the composite container.
58 2
     * @param ContainerInterface $container
59 2
     */
60
    public function attach(ContainerInterface $container): void
61
    {
62 2
        array_unshift($this->containers, $container);
63
    }
64
65
    /**
66
     * Removes a container from the list of containers.
67
     * @param ContainerInterface $container
68
     */
69
    public function detach(ContainerInterface $container): void
70
    {
71
        foreach ($this->containers as $i => $c) {
72
            if ($container === $c) {
73
                unset($this->containers[$i]);
74
            }
75
        }
76
    }
77
78
    private function isTagAlias(string $id): bool
79
    {
80
        return strpos($id, 'tag@') === 0;
81
    }
82
}
83