Completed
Push — develop ( 7f6ad2...eb155e )
by Baptiste
02:38
created

RegistrableServicePass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 14
loc 35
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 14 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Neo4jBundle\DependencyInjection\Compiler;
5
6
use Innmind\Neo4jBundle\Exception\RuntimeException;
7
use Symfony\Component\DependencyInjection\{
8
    ContainerBuilder,
9
    Compiler\CompilerPassInterface,
10
    Reference
11
};
12
13
class RegistrableServicePass implements CompilerPassInterface
14
{
15
    private $service;
16
    private $tag;
17
18
    public function __construct(string $service, string $tag)
19
    {
20
        $this->service = $service;
21
        $this->tag = $tag;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $definition = $container->getDefinition($this->service);
30
        $ids = $container->findTaggedServiceIds($this->tag);
31
32 View Code Duplication
        foreach ($ids as $id => $tags) {
33
            foreach ($tags as $tag) {
34
                if (!isset($tag['class'])) {
35
                    throw new RuntimeException(
36
                        'The class attribute must be defined'
37
                    );
38
                }
39
40
                $definition->addMethodCall(
41
                    'register',
42
                    [$tag['class'], new Reference($id)]
43
                );
44
            }
45
        }
46
    }
47
}
48