Completed
Push — master ( 501a81...30bafc )
by Muhammad Kashif
25:46
created

Container::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 1
crap 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\Factory\ConfigFileServiceFactory;
9
use Gr8abbasi\Container\Repository\InMemoryServiceRepository;
10
use Gr8abbasi\Container\Exception\CircularDependencyException;
11
12
/**
13
 * Container Class
14
 */
15
class Container implements ContainerInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $services;
21
22
    /**
23
     * @var ServiceRepositoryInterface
24
     */
25
    private $repository;
26
27
    /**
28
     * @var ServiceFactoryInterface
29
     */
30
    private $factory;
31
32
    /**
33
     * @var array
34
     */
35
    private $resolvedServices;
36
37
    /**
38
     * Constructor for Container
39
     *
40
     * @param array $services
41
     */
42 8
    public function __construct(
43
        array $services = [],
44
        ServiceRepositoryInterface $repository = null,
45
        ServiceFactoryInterface $factory = null
46
    ) {
47 8
        $this->services = $services;
48 8
        $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 8
        $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...
50 8
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 6
    public function get($id)
56
    {
57 6
        if (!$this->has($id)) {
58 1
            throw new NotFoundException('Service not found: ' . $id);
59
        }
60
61 5
        if (is_null($this->repository->get($id))) {
62 5
            $this->createService($id, $this);
63 2
        }
64
65 2
        return $this->repository->get($id);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 6
    public function has($name)
72
    {
73 6
        return isset($this->services[$name]);
74
    }
75
76
    /**
77
     * Create service and add to the
78
     * repository
79
     *
80
     * @param string $id
81
     * @param Container $container
82
     *
83
     * @return void
84
     */
85 5
    private function createService($id, $container)
86
    {
87 5
        $this->validate($id);
88 4
        $this->resolvedServices[$id] = true;
89 4
        $service = $this->factory->create($this->services[$id], $container);
90 2
        unset($this->resolvedServices[$id]);
91 2
        $this->repository->add($id, $service);
92 2
    }
93
94
    /**
95
     * Validate requested service before
96
     * attempt to resolve
97
     *
98
     * @param string $name
99
     *
100
     * @return void
101
     */
102 5
    private function validate($name)
103
    {
104 5
        if (!isset($this->services[$name]) || empty($this->services[$name])) {
105 1
            throw new ContainerException(
106
                'Service should be an array with key value pair: ' . $name
107 1
            );
108
        }
109
110 4
        if (isset($this->resolvedServices[$name])) {
111 1
            throw new CircularDependencyException(
112
                'Circular dependency detected: => ' . $name
113 1
            );
114
        }
115 4
    }
116
}
117