ClassMapBuildCompilerPass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 38
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 12 3
A canBeAdded() 0 12 3
1
<?php
2
namespace Skrz\Bundle\AutowiringBundle\DependencyInjection\Compiler;
3
4
use Skrz\Bundle\AutowiringBundle\DependencyInjection\ClassMultiMap;
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
9
class ClassMapBuildCompilerPass implements CompilerPassInterface
10
{
11
12
	/** @var ClassMultiMap */
13
	private $classMap;
14
15 10
	public function __construct(ClassMultiMap $typeMap)
16
	{
17 10
		$this->classMap = $typeMap;
18 10
	}
19
20 9
	public function process(ContainerBuilder $container)
21
	{
22 9
		$parameterBag = $container->getParameterBag();
23
24 9
		foreach ($container->getDefinitions() as $serviceId => $definition) {
25 9
			if (!$this->canBeAdded($definition)) {
26 2
				continue;
27
			}
28
29 9
			$this->classMap->put($parameterBag->resolveValue($definition->getClass()), $serviceId);
30
		}
31 9
	}
32
33 9
	private function canBeAdded(Definition $definition): bool
34
	{
35 9
		if ($definition->isAbstract()) {
36 1
			return false;
37
		}
38
39 9
		if (!$definition->getClass()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $definition->getClass() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
40 1
			return false;
41
		}
42
43 9
		return true;
44
	}
45
46
}
47