Completed
Push — master ( e1f08c...557060 )
by Muhammad Kashif
24:26
created

Container   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A get() 0 12 3
A has() 0 4 1
A createService() 0 17 4
1
<?php
2
3
namespace Gr8abbasi\Container;
4
5
use Interop\Container\ContainerInterface;
6
use Gr8abbasi\Container\Exception\NotFoundException;
7
use Gr8abbasi\Container\Exception\ContainerException;
8
use Gr8abbasi\Container\Repository\InMemoryServiceRepository;
9
use Gr8abbasi\Container\Factory\ConfigFileServiceFactory;
10
11
/**
12
 * Container Class
13
 */
14
class Container implements ContainerInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $services;
20
21
    /**
22
     * @var array
23
     */
24
    private $serviceStore;
25
26
    /**
27
     * @var ServiceRepositoryInterface
28
     */
29
    private $repository;
30
31
    /**
32
     * @var ServiceFactoryInterface
33
     */
34
    private $factory;
0 ignored issues
show
Unused Code introduced by
The property $factory is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * Constructor for Container
38
     *
39
     * @param array $services
40
     */
41 5
    public function __construct(
42
        array $services = [],
43
        ServiceRepositoryInterface $repository = null,
44
        ServiceFactoryInterface $factory = null
0 ignored issues
show
Unused Code introduced by
The parameter $factory is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    ) {
46 5
        $this->services = $services;
47 5
        $this->serviceStore = [];
48 5
        $this->repository = $repository ?: new InMemoryServiceRepository();
0 ignored issues
show
Documentation Bug introduced by
It seems like $repository ?: new \Gr8a...moryServiceRepository() can also be of type object<Gr8abbasi\Contain...emoryServiceRepository>. However, the property $repository is declared as type object<Gr8abbasi\Contain...iceRepositoryInterface>. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
        // $this->factory = $factory ?: new ConfigFileServiceFactory();
50 5
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    public function get($name)
56
    {
57 4
        if (!$this->has($name)) {
58 1
            throw new NotFoundException('Service not found: ' . $name);
59
        }
60
61 3
        if (!isset($this->serviceStore[$name])) {
62 3
            $this->serviceStore[$name] = $this->createService($name);
63 1
        }
64
65 1
        return $this->serviceStore[$name];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function has($name)
72
    {
73 4
        return isset($this->services[$name]);
74
    }
75
76
    /**
77
     * Create service instance
78
     *
79
     * @param string $name
80
     *
81
     * @return mixed created service
82
     */
83 3
    private function createService($name)
84
    {
85 3
        if (!isset($this->services[$name]) || empty($this->services[$name])) {
86 1
            throw new ContainerException(
87
                'Service should be an array with key value pair: ' . $name
88 1
            );
89
        }
90
91 2
        if (!class_exists($this->services[$name]['class'])) {
92 1
            throw new ContainerException(
93 1
                'Class does not exists: ' . $this->services[$name]['class']
94 1
            );
95
        }
96
97 1
        $service = new \ReflectionClass($this->services[$name]['class']);
98 1
        return $service->newInstance();
99
    }
100
}
101