DescriptionFactoryPass::process()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 61
Code Lines 38

Duplication

Lines 15
Ratio 24.59 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 61
rs 7.399
cc 7
eloc 38
nc 9
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 DescriptionFactoryPass implements CompilerPassInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function process(ContainerBuilder $container)
15
    {
16
        if (!$container->hasDefinition('psi_description.factory')) {
17
            return;
18
        }
19
20
        $factoryDef = $container->getDefinition('psi_description.factory');
21
        $enhancerIds = $container->findTaggedServiceIds('psi_description.enhancer');
22
        $resolverIds = $container->findTaggedServiceIds('psi_description.subject_resolver');
23
24
        $refs = [
25
            'enhancer' => [],
26
            'subject_resolver' => [],
27
        ];
28
29
        foreach ([
30
            'enhancer' => $enhancerIds,
31
            'subject_resolver' => $resolverIds,
32
        ] as $key => $serviceIds) {
33
            $humanKey = str_replace('_', ' ', $key);
34 View Code Duplication
            foreach ($serviceIds as $serviceId => $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...
35
                $attributes = $attributes[0];
36
37
                if (!isset($attributes['alias'])) {
38
                    throw new \InvalidArgumentException(sprintf(
39
                        'Description %s "%s" has no "alias" attribute in its tag',
40
                        $humanKey,
41
                        $serviceId
42
                    ));
43
                }
44
45
                $alias = $attributes['alias'];
46
47
                $refs[$key][$alias] = new Reference($serviceId);
48
            }
49
50
            $enabled = $container->getParameter(sprintf('psi_description.%ss', $key));
51
            $diff = array_diff($enabled, array_keys($refs[$key]));
52
53
            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...
54
                throw new \InvalidArgumentException(sprintf(
55
                    'Unknown %s(s) "%s" specified. Known %s(s): "%s"',
56
                    $humanKey,
57
                    implode('", "', $diff),
58
                    $humanKey,
59
                    implode('", "', array_keys($refs[$key]))
60
                ));
61
            }
62
63
            $orderedRefs = [];
64
65
            foreach ($enabled as $enabledAlias) {
66
                $orderedRefs[$enabledAlias] = $refs[$key][$enabledAlias];
67
            }
68
69
            $refs[$key] = $orderedRefs;
70
        }
71
72
        $factoryDef->replaceArgument(0, array_values($refs['enhancer']));
73
        $factoryDef->replaceArgument(2, array_values($refs['subject_resolver']));
74
    }
75
}
76