GenericContainer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 79
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 19 6
A get() 0 7 2
A newInstance() 0 12 2
A getParams() 0 20 5
A has() 0 4 1
1
<?php
2
3
namespace Magium\Configuration\Container;
4
5
use Interop\Container\ContainerInterface;
6
7
class GenericContainer implements ContainerInterface
8
{
9
10
    protected $container = [];
11
12 18
    public function __construct(array $defaults = [])
13
    {
14 18
        $this->container = $defaults;
15 18
        $this->set($this);
16 18
    }
17
18 18
    public function set($value)
19
    {
20 18
        if (!is_object($value)) {
21 1
            throw new InvalidObjectException('The GenericContainer can only accept objects');
22
        }
23 18
        $class = new \ReflectionClass($value);
24 18
        $interfaces = $class->getInterfaces();
25 18
        foreach ($interfaces as $interface) {
26 18
            if (!$interface->isInternal()) {
27 18
                $this->container[$interface->getName()] = $value;
0 ignored issues
show
Bug introduced by
Consider using $interface->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28
            }
29
        }
30
        do {
31 18
            if ($class->isInternal()) {
32 6
                return; // Our work is done here.
33
            }
34 18
            $this->container[$class->getName()] = $value;
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
35 18
        } while (($class = $class->getParentClass()) instanceof \ReflectionClass);
36 18
    }
37
38 14
    public function get($id)
39
    {
40 14
        if (!isset($this->container[$id])) {
41 13
            $this->set($this->newInstance($id));
42
        }
43 11
        return $this->container[$id];
44
    }
45
46 13
    public function newInstance($type)
47
    {
48 13
        $reflection = new \ReflectionClass($type);
49 12
        $constructor = $reflection->getConstructor();
50 12
        $constructorParams = $this->getParams($constructor);
51 10
        if ($constructorParams) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $constructorParams of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52 4
            $requestedInstance = $reflection->newInstanceArgs($constructorParams);
53
        } else {
54 10
            $requestedInstance = $reflection->newInstance();
55
        }
56 10
        return $requestedInstance;
57
    }
58
59 12
    protected function getParams(\ReflectionMethod $method = null)
60
    {
61 12
        if (!$method instanceof \ReflectionMethod) {
62 9
            return [];
63
        }
64 7
        $constructorParams = [];
65 7
        $params = $method->getParameters();
66 7
        foreach ($params as $param) {
67 7
            if ($param->getClass() instanceof \ReflectionClass) {
68 4
                $class = $param->getClass()->getName();
69 4
                $instance = $this->get($class);
70 4
                $constructorParams[] = $instance;
71 3
            } else if (!$param->isOptional()) {
72 2
                throw new InvalidObjectException(
73 2
                    'The generic container will only manage constructor arguments that are objects'
74
                );
75
            }
76
        }
77 5
        return $constructorParams;
78
    }
79
80 3
    public function has($id)
81
    {
82 3
        return isset($this->container[$id]);
83
    }
84
85
}
86