AbstractRegistryPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B process() 0 28 5
1
<?php
2
3
namespace Psi\Bundle\ContentType\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
abstract class AbstractRegistryPass implements CompilerPassInterface
11
{
12
    private $registryId;
13
    private $serviceTag;
14
    private $registerByAlias;
15
16
    public function __construct(
17
        $registryId,
18
        $serviceTag,
19
        $registerByAlias = true
20
    ) {
21
        $this->registryId = $registryId;
22
        $this->serviceTag = $serviceTag;
23
        $this->registerByAlias = $registerByAlias;
24
    }
25
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->has($this->registryId)) {
29
            return;
30
        }
31
32
        $taggedIds = $container->findTaggedServiceIds($this->serviceTag);
33
        $registryDef = $container->getDefinition($this->registryId);
34
35
        foreach ($taggedIds as $serviceId => $attributes) {
36
            $attributes = $attributes[0];
37
38
            if ($this->registerByAlias) {
39
                if (!isset($attributes['alias'])) {
40
                    throw new InvalidArgumentException(sprintf(
41
                        $this->context . ' "%s" has no "alias" attribute in its tag',
0 ignored issues
show
Bug introduced by
The property context does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
                        $serviceId
43
                    ));
44
                }
45
46
                $alias = $attributes['alias'];
47
            } else {
48
                $alias = $container->getDefinition($serviceId)->getClass();
49
            }
50
51
            $registryDef->addMethodCall('register', [$alias, new Reference($serviceId)]);
52
        }
53
    }
54
}
55