ServicesByTypeMapCompilerPass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 14 3
A isValidServiceDefinition() 0 16 4
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\Compiler;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symplify\ActionAutowire\DependencyInjection\Container\ServicesByTypeMap;
16
17
final class ServicesByTypeMapCompilerPass implements CompilerPassInterface
18
{
19
    /**
20
     * @var ServicesByTypeMap
21
     */
22
    private $serviceMap;
23
24 2
    public function __construct(ServicesByTypeMap $servicesByTypeMap)
25
    {
26 2
        $this->serviceMap = $servicesByTypeMap;
27 2
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function process(ContainerBuilder $containerBuilder)
33
    {
34 2
        $parameterBag = $containerBuilder->getParameterBag();
35 2
        foreach ($containerBuilder->getDefinitions() as $serviceId => $definition) {
36 2
            if (! $this->isValidServiceDefinition($definition)) {
37 2
                continue;
38
            }
39
40 2
            $this->serviceMap->addService(
41 2
                $parameterBag->resolveValue($definition->getClass()),
42
                $serviceId
43
            );
44
        }
45 2
    }
46
47 2
    private function isValidServiceDefinition(Definition $definition) : bool
48
    {
49 2
        if ($definition->isAbstract()) {
50 2
            return false;
51
        }
52
53 2
        if (! $definition->isPublic()) {
54 2
            return false;
55
        }
56
57 2
        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...
58 2
            return false;
59
        }
60
61 2
        return true;
62
    }
63
}
64