Completed
Push — master ( 68250e...501a81 )
by Muhammad Kashif
23:17
created

Container::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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\Factory\ConfigFileServiceFactory;
9
use Gr8abbasi\Container\Repository\InMemoryServiceRepository;
10
11
/**
12
 * Container Class
13
 */
14
class Container implements ContainerInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $services;
20
21
    /**
22
     * @var ServiceRepositoryInterface
23
     */
24
    private $repository;
25
26
    /**
27
     * @var ServiceFactoryInterface
28
     */
29
    private $factory;
30
31
    /**
32
     * @var array
33
     */
34
    private $resolvedServices;
35
36
    /**
37
     * Constructor for Container
38
     *
39
     * @param array $services
40
     */
41 7
    public function __construct(
42
        array $services = [],
43
        ServiceRepositoryInterface $repository = null,
44
        ServiceFactoryInterface $factory = null
45
    ) {
46 7
        $this->services = $services;
47 7
        $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...
48 7
        $this->factory = $factory ?: new ConfigFileServiceFactory();
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory ?: new \Gr8abba...figFileServiceFactory() can also be of type object<Gr8abbasi\Contain...nfigFileServiceFactory>. However, the property $factory is declared as type object<Gr8abbasi\Contain...erviceFactoryInterface>. 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 7
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 5
    public function get($id)
55
    {
56 5
        if (!$this->has($id)) {
57 1
            throw new NotFoundException('Service not found: ' . $id);
58
        }
59
60 4
        if (is_null($this->repository->get($id))) {
61 4
            $this->createService($id, $this);
62 2
        }
63
64 2
        return $this->repository->get($id);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 5
    public function has($name)
71
    {
72 5
        return isset($this->services[$name]);
73
    }
74
75
    /**
76
     * Create service and add to the
77
     * repository
78
     *
79
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
80
     *
81
     * @return void
82
     */
83 4
    private function createService($id, $container)
84
    {
85 4
        $this->validate($id);
86 2
        $this->resolvedServices[$id] = true;
87 2
        $service = $this->factory->create($this->services[$id], $container);
88 2
        unset($this->resolvedServices[$id]);
89 2
        $this->repository->add($id, $service);
90 2
    }
91
92
    /**
93
     * Validate requested service before
94
     * attempt to resolve
95
     *
96
     * @param string $name
97
     *
98
     * @return void
99
     */
100 4
    private function validate($name)
101
    {
102 4
        if (!isset($this->services[$name]) || empty($this->services[$name])) {
103 1
            throw new ContainerException(
104
                'Service should be an array with key value pair: ' . $name
105 1
            );
106
        }
107
108 3
        if (!class_exists($this->services[$name]['class'])) {
109 1
            throw new ContainerException(
110 1
                'Class does not exists: ' . $this->services[$name]['class']
111 1
            );
112
        }
113
114 2
        if (isset($this->currentServices[$name])) {
0 ignored issues
show
Bug introduced by
The property currentServices does not seem to exist. Did you mean services?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
115
            throw new CircularDependencyException(
116
                'Circular dependency detected: '. key($this->resolvedServices[$name]) . '=> {$name}'
117
            );
118
        }
119 2
    }
120
}
121