Completed
Push — 4.x ( a2ec53...8fa263 )
by Phil
13:00 queued 10:07
created

Container::get()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.0107

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 20
cts 21
cp 0.9524
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 2
crap 10.0107

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 League\Container;
6
7
use League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface};
8
use League\Container\Exception\{NotFoundException, ContainerException};
9
use League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface};
10
use League\Container\ServiceProvider\{ServiceProviderAggregate,
11
    ServiceProviderAggregateInterface,
12
    ServiceProviderInterface};
13
use Psr\Container\ContainerInterface;
14
15
class Container implements DefinitionContainerInterface
16
{
17
    /**
18
     * @var boolean
19
     */
20
    protected $defaultToShared = false;
21
22
    /**
23
     * @var DefinitionAggregateInterface
24
     */
25
    protected $definitions;
26
27
    /**
28
     * @var ServiceProviderAggregateInterface
29
     */
30
    protected $providers;
31
32
    /**
33
     * @var InflectorAggregateInterface
34
     */
35
    protected $inflectors;
36
37
    /**
38
     * @var ContainerInterface[]
39
     */
40
    protected $delegates = [];
41
42 108
    public function __construct(
43
        DefinitionAggregateInterface $definitions = null,
44
        ServiceProviderAggregateInterface $providers = null,
45
        InflectorAggregateInterface $inflectors = null
46
    ) {
47 108
        $this->definitions = $definitions ?? new DefinitionAggregate();
48 108
        $this->providers   = $providers   ?? new ServiceProviderAggregate();
49 108
        $this->inflectors  = $inflectors  ?? new InflectorAggregate();
50
51 108
        if ($this->definitions instanceof ContainerAwareInterface) {
52 108
            $this->definitions->setContainer($this);
53
        }
54
55 108
        if ($this->providers instanceof ContainerAwareInterface) {
56 108
            $this->providers->setContainer($this);
57
        }
58
59 108
        if ($this->inflectors instanceof ContainerAwareInterface) {
60 108
            $this->inflectors->setContainer($this);
61
        }
62 108
    }
63
64 24
    public function add(string $id, $concrete = null): DefinitionInterface
65
    {
66 24
        $concrete = $concrete ?? $id;
67
68 24
        if (true === $this->defaultToShared) {
69 3
            return $this->addShared($id, $concrete);
70
        }
71
72 21
        return $this->definitions->add($id, $concrete);
73
    }
74
75 6
    public function addShared(string $id, $concrete = null): DefinitionInterface
76
    {
77 6
        $concrete = $concrete ?? $id;
78 6
        return $this->definitions->addShared($id, $concrete);
79
    }
80
81 3
    public function defaultToShared(bool $shared = true): ContainerInterface
82
    {
83 3
        $this->defaultToShared = $shared;
84 3
        return $this;
85
    }
86
87 9
    public function extend(string $id): DefinitionInterface
88
    {
89 9
        if ($this->providers->provides($id)) {
90 3
            $this->providers->register($id);
91
        }
92
93 9
        if ($this->definitions->has($id)) {
94 6
            return $this->definitions->getDefinition($id);
95
        }
96
97 3
        throw new NotFoundException(sprintf(
98 3
            'Unable to extend alias (%s) as it is not being managed as a definition',
99 1
            $id
100
        ));
101
    }
102
103 9
    public function addServiceProvider($provider): DefinitionContainerInterface
104
    {
105 9
        $this->providers->add($provider);
106 9
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (League\Container\Container) is incompatible with the return type declared by the interface League\Container\Definit...ace::addServiceProvider of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
    }
108
109 30
    public function get($id, bool $new = false)
110
    {
111 30
        if ($this->definitions->has($id)) {
112 18
            $resolved = (true === $new) ? $this->definitions->resolveNew($id) : $this->definitions->resolve($id);
113 18
            return $this->inflectors->inflect($resolved);
114
        }
115
116 15
        if ($this->definitions->hasTag($id)) {
117 3
            $arrayOf = (true === $new)
118
                ? $this->definitions->resolveTaggedNew($id)
119 3
                : $this->definitions->resolveTagged($id);
120
121
            array_walk($arrayOf, function (&$resolved) {
122 3
                $resolved = $this->inflectors->inflect($resolved);
123 3
            });
124
125 3
            return $arrayOf;
126
        }
127
128 12
        if ($this->providers->provides($id)) {
129 6
            $this->providers->register($id);
130
131 6
            if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
132 3
                throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
133
            }
134
135 3
            return $this->get($id, $new);
136
        }
137
138 6
        foreach ($this->delegates as $delegate) {
139 3
            if ($delegate->has($id)) {
140 3
                $resolved = $delegate->get($id);
141 3
                return $this->inflectors->inflect($resolved);
142
            }
143
        }
144
145 3
        throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id));
146
    }
147
148 33
    public function has($id): bool
149
    {
150 33
        if ($this->definitions->has($id)) {
151 18
            return true;
152
        }
153
154 18
        if ($this->definitions->hasTag($id)) {
155 3
            return true;
156
        }
157
158 15
        if ($this->providers->provides($id)) {
159 6
            return true;
160
        }
161
162 9
        foreach ($this->delegates as $delegate) {
163 3
            if ($delegate->has($id)) {
164 3
                return true;
165
            }
166
        }
167
168 6
        return false;
169
    }
170
171 3
    public function inflector(string $type, callable $callback = null): InflectorInterface
172
    {
173 3
        return $this->inflectors->add($type, $callback);
174
    }
175
176 3
    public function delegate(ContainerInterface $container): self
177
    {
178 3
        $this->delegates[] = $container;
179
180 3
        if ($container instanceof ContainerAwareInterface) {
181 3
            $container->setContainer($this);
182
        }
183
184 3
        return $this;
185
    }
186
}
187