ClassMapBuildCompilerPass::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 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