ExtensionPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 35 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 14
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 14 37 5

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
3
namespace Psi\Bundle\Description\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class ExtensionPass implements CompilerPassInterface
10
{
11
    public function process(ContainerBuilder $container)
12
    {
13
        if (!$container->hasDefinition('psi_description.schema')) {
14
            return;
15
        }
16
17
        $schemaDef = $container->getDefinition('psi_description.schema');
18
        $extensionIds = $container->findTaggedServiceIds('psi_description.schema_extension');
19
20
        $extensionRefs = [];
21 View Code Duplication
        foreach ($extensionIds as $extensionId => $attributes) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $attributes = $attributes[0];
23
24
            if (!isset($attributes['alias'])) {
25
                throw new \InvalidArgumentException(sprintf(
26
                    'Description schema extension "%s" has no "alias" attribute in its tag',
27
                    $extensionId
28
                ));
29
            }
30
31
            $alias = $attributes['alias'];
32
33
            $extensionRefs[$alias] = new Reference($extensionId);
34
        }
35
36
        $enabledEnhancers = $container->getParameter('psi_description.schema.extensions');
37
        $diff = array_diff($enabledEnhancers, array_keys($extensionRefs));
38
39
        if ($diff) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $diff of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
            throw new \InvalidArgumentException(sprintf(
41
                'Unknown schema extension(s) "%s" specified. Known extensions: "%s"',
42
                implode('", "', $diff), implode('", "', array_keys($extensionRefs))
43
            ));
44
        }
45
46
        $schemaDef->replaceArgument(0, array_values($extensionRefs));
47
    }
48
}
49