Passed
Pull Request — master (#142)
by Sergei
06:52
created

ServiceLocator::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 12
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\ServiceLocator;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Definitions\Infrastructure\DefinitionValidator;
9
10
final class ServiceLocator implements ContainerInterface
11
{
12
    private DependencyResolver $dependencyResolver;
13
14
    public function __construct(
15
        ContainerInterface $container = null,
16
        array $definitions = [],
17
        bool $validate = true
18
    ) {
19
        $this->dependencyResolver = new DependencyResolver($container);
20
21
        foreach ($definitions as $id => $definition) {
22
            if ($validate) {
23
                DefinitionValidator::validate($definition, $id);
24
            }
25
            $this->dependencyResolver->set($id, $definition);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Yiisoft\Factory\ServiceL...pendencyResolver::set() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
            $this->dependencyResolver->set(/** @scrutinizer ignore-type */ $id, $definition);
Loading history...
26
        }
27
    }
28
29
    public function get($id)
30
    {
31
        return $this->dependencyResolver->getFromServiceLocator($id);
32
    }
33
34
    public function has($id): bool
35
    {
36
        return $this->dependencyResolver->has($id);
37
    }
38
}
39