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

SingleInstanceContainer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 41
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A has() 0 4 1
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