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

ServiceLocator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 10
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A get() 0 3 1
A has() 0 3 1
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