Container::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Thruster\Component\Container;
4
5
use Interop\Container\ContainerInterface;
6
use Thruster\Component\Container\Exception\IdentifierFrozenException;
7
use Thruster\Component\Container\Exception\NotFoundException;
8
9
/**
10
 * Class Container
11
 *
12
 * @package Thruster\Component\Container
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class Container implements \ArrayAccess, ContainerInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $values;
21
22
    /**
23
     * @var \SplObjectStorage
24
     */
25
    private $factories;
26
27
    /**
28
     * @var array
29
     */
30
    private $frozen;
31
32
    /**
33
     * @var array
34
     */
35
    private $raw;
36
37 10
    public function __construct(array $values = [])
38
    {
39 10
        $this->values     = [];
40 10
        $this->frozen     = [];
41 10
        $this->raw        = [];
42 10
        $this->factories  = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<SplObjectStorage> of property $factories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
44 10
        foreach ($values as $key => $value) {
45 1
            $this->set($key, $value);
46
        }
47 10
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 9
    public function has($id) : bool
53
    {
54 9
        return isset($this->raw[$id]);
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 9
    public function get($id)
61
    {
62 9
        if (false === $this->has($id)) {
63 1
            throw new NotFoundException($id);
64
        }
65
66 8
        if (isset($this->values[$id])) {
67 3
            return $this->values[$id];
68
        }
69
70 8
        if (isset($this->factories[$id])) {
71 1
            return $this->raw[$id]($this);
72
        }
73
74 7
        $raw                        = $this->raw[$id];
75 7
        $value = $this->values[$id] = $raw($this);
76
77 7
        $this->frozen[$id] = true;
78
79 7
        return $value;
80
    }
81
82 7 View Code Duplication
    public function set($id, callable $value) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 7
        if (isset($this->frozen[$id])) {
85 1
            throw new IdentifierFrozenException($id);
86
        }
87
88 7
        $this->raw[$id]  = $value;
89
90 7
        return $this;
91
    }
92
93 2 View Code Duplication
    public function factory($id, callable $value) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95 2
        if (isset($this->frozen[$id])) {
96 1
            throw new IdentifierFrozenException($id);
97
        }
98
99 1
        $this->factories[$id] = true;
100 1
        $this->raw[$id]       = $value;
101
102 1
        return $this;
103
    }
104
105 3
    public function remove($id) : self
106
    {
107
        unset(
108 3
            $this->raw[$id],
109 3
            $this->values[$id],
110 3
            $this->frozen[$id],
111 3
            $this->factories[$id]
112
        );
113
114 3
        return $this;
115
    }
116
117 1
    public function addProvider(ContainerProviderInterface $provider) : self
118
    {
119 1
        $provider->register($this);
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 1
    public function offsetExists($offset) : bool
128
    {
129 1
        return $this->has($offset);
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135 1
    public function offsetGet($offset)
136
    {
137 1
        return $this->get($offset);
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143 1
    public function offsetSet($offset, $value)
144
    {
145 1
        $this->set($offset, $value);
0 ignored issues
show
Documentation introduced by
$value is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146 1
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151 1
    public function offsetUnset($offset)
152
    {
153 1
        $this->remove($offset);
154 1
    }
155
}
156