Completed
Pull Request — master (#107)
by
unknown
11:11 queued 08:33
created

SingleInstanceContainer::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\Container;
4
5
use Interop\Container\ContainerInterface as InteropContainerInterface;
6
7
/**
8
 * Wraps a container intercepting requests for dependencies and caching the return.
9
 *
10
 * This ensures that a single instance of any service ID is only ever returned. This
11
 * be used with a reflection container to provide zero-configuration, single instance
12
 * DI.
13
 */
14
class SingleInstanceContainer implements InteropContainerInterface
15
{
16
    /**
17
     * @var ContainerInterface
18
     */
19
    private $wrapped;
20
    
21
    /**
22
     * @var mixed[]
23
     */
24
    private $instances = [];
25
    
26
    
27
    /**
28
     * @param InteropContainerInterface $container
29
     */
30 15
    public function __construct(InteropContainerInterface $container)
31
    {
32 15
        $this->wrapped = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Interop\Container\ContainerInterface>, but the property $wrapped was declared to be of type object<League\Container\ContainerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33 15
    }
34
    
35
    /**
36
     * @inheritdoc
37
     */
38 9
    public function get($id)
39
    {
40 9
        if (!isset($this->instances[$id])) {
41 9
            $this->instances[$id] = $this->wrapped->get($id);
42 6
        }
43
        
44 6
        return $this->instances[$id];
45
    }
46
    
47
    /**
48
     * @inheritdoc
49
     */
50 6
    public function has($id)
51
    {
52 6
        return $this->wrapped->has($id);
53
    }
54
}
55