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

CompositeContainer::get()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.0145

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 32
ccs 18
cts 19
cp 0.9474
crap 10.0145
rs 7.6666

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 28
        if ($id === StateResetter::class) {
26 2
            $resetters = [];
27 2
            foreach ($this->containers as $container) {
28 2
                if ($container->has(StateResetter::class)) {
29 2
                    $resetters[] = $container->get(StateResetter::class);
30
                }
31
            }
32 2
            return new StateResetter($resetters, $this);
33
        }
34
35 28
        if ($this->isTagAlias($id)) {
36 2
            $tags = [];
37 2
            foreach ($this->containers as $container) {
38 2
                if (!$container instanceof Container) {
39
                    continue;
40
                }
41 2
                if ($container->has($id)) {
42 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

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