ServicesByTypeMap::getMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\ActionAutowire\DependencyInjection\Container;
11
12
use ReflectionClass;
13
14
/**
15
 * Inspired by ClassMultiMap
16
 * https://github.com/skrz/autowiring-bundle/blob/master/DependencyInjection/ClassMultiMap.php.
17
 */
18
final class ServicesByTypeMap
19
{
20
    /**
21
     * @var string[]
22
     */
23
    private $classes = [];
24
25 2
    public function addService(string $serviceClass, string $serviceId)
26
    {
27 2
        $reflectionClass = new ReflectionClass($serviceClass);
28 2
        foreach ($reflectionClass->getInterfaceNames() as $interfaceName) {
29 2
            if (! isset($this->classes[$interfaceName])) {
30 2
                $this->classes[$interfaceName] = [];
31
            }
32 2
            $this->classes[$interfaceName][] = $serviceId;
33
        }
34
        do {
35 2
            if (! isset($this->classes[$reflectionClass->getName()])) {
36 2
                $this->classes[$reflectionClass->getName()] = [];
37
            }
38 2
            $this->classes[$reflectionClass->getName()][] = $serviceId;
39 2
        } while ($reflectionClass = $reflectionClass->getParentClass());
40 2
    }
41
42 2
    public function getMap() : array
43
    {
44 2
        return $this->classes;
45
    }
46
}
47