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

ServiceLocator::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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