Completed
Push — 4.x ( 8fa263...613328 )
by Phil
03:02
created

Container::resolve()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

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

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 114
    public function __construct(
43
        DefinitionAggregateInterface $definitions = null,
44
        ServiceProviderAggregateInterface $providers = null,
45
        InflectorAggregateInterface $inflectors = null
46
    ) {
47 114
        $this->definitions = $definitions ?? new DefinitionAggregate();
48 114
        $this->providers   = $providers   ?? new ServiceProviderAggregate();
49 114
        $this->inflectors  = $inflectors  ?? new InflectorAggregate();
50
51 114
        if ($this->definitions instanceof ContainerAwareInterface) {
52 114
            $this->definitions->setContainer($this);
53
        }
54
55 114
        if ($this->providers instanceof ContainerAwareInterface) {
56 114
            $this->providers->setContainer($this);
57
        }
58
59 114
        if ($this->inflectors instanceof ContainerAwareInterface) {
60 114
            $this->inflectors->setContainer($this);
61
        }
62 114
    }
63
64 27
    public function add(string $id, $concrete = null): DefinitionInterface
65
    {
66 27
        $concrete = $concrete ?? $id;
67
68 27
        if (true === $this->defaultToShared) {
69 3
            return $this->addShared($id, $concrete);
70
        }
71
72 24
        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(ServiceProviderInterface $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 33
    public function get($id)
110
    {
111 33
        return $this->resolve($id);
112
    }
113
114 3
    public function getNew($id)
115
    {
116 3
        return $this->resolve($id, true);
117
    }
118
119 36
    public function has($id): bool
120
    {
121 36
        if ($this->definitions->has($id)) {
122 21
            return true;
123
        }
124
125 21
        if ($this->definitions->hasTag($id)) {
126 6
            return true;
127
        }
128
129 15
        if ($this->providers->provides($id)) {
130 6
            return true;
131
        }
132
133 9
        foreach ($this->delegates as $delegate) {
134 3
            if ($delegate->has($id)) {
135 3
                return true;
136
            }
137
        }
138
139 6
        return false;
140
    }
141
142 3
    public function inflector(string $type, callable $callback = null): InflectorInterface
143
    {
144 3
        return $this->inflectors->add($type, $callback);
145
    }
146
147 3
    public function delegate(ContainerInterface $container): self
148
    {
149 3
        $this->delegates[] = $container;
150
151 3
        if ($container instanceof ContainerAwareInterface) {
152 3
            $container->setContainer($this);
153
        }
154
155 3
        return $this;
156
    }
157
158 33
    protected function resolve($id, bool $new = false)
159
    {
160 33
        if ($this->definitions->has($id)) {
161 18
            $resolved = (true === $new) ? $this->definitions->resolveNew($id) : $this->definitions->resolve($id);
162 18
            return $this->inflectors->inflect($resolved);
163
        }
164
165 18
        if ($this->definitions->hasTag($id)) {
166 6
            $arrayOf = (true === $new)
167 3
                ? $this->definitions->resolveTaggedNew($id)
168 6
                : $this->definitions->resolveTagged($id);
169
170
            array_walk($arrayOf, function (&$resolved) {
171 6
                $resolved = $this->inflectors->inflect($resolved);
172 6
            });
173
174 6
            return $arrayOf;
175
        }
176
177 12
        if ($this->providers->provides($id)) {
178 6
            $this->providers->register($id);
179
180 6
            if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
181 3
                throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
182
            }
183
184 3
            return $this->resolve($id, $new);
185
        }
186
187 6
        foreach ($this->delegates as $delegate) {
188 3
            if ($delegate->has($id)) {
189 3
                $resolved = $delegate->get($id);
190 3
                return $this->inflectors->inflect($resolved);
191
            }
192
        }
193
194 3
        throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id));
195
    }
196
}
197