Passed
Pull Request — master (#3)
by Joe
01:50
created

InjectProcessor::process()   F

Complexity

Conditions 18
Paths 1931

Size

Total Lines 74
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 18.0063

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 74
ccs 36
cts 37
cp 0.973
rs 0.7
c 0
b 0
f 0
cc 18
nc 1931
nop 3
crap 18.0063

How to fix   Long Method    Complexity   

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 declare(strict_types=1);
2
/**
3
 * This file belongs to Bandit. All rights reserved
4
 */
5
6
namespace Incompass\InjectionBundle;
7
8
use Incompass\InjectionBundle\Annotation\Argument;
9
use Incompass\InjectionBundle\Annotation\Factory;
10
use Incompass\InjectionBundle\Annotation\Inject;
11
use Incompass\InjectionBundle\Annotation\MethodCall;
12
use Incompass\InjectionBundle\Annotation\Tag;
13
use Symfony\Component\DependencyInjection\Alias;
14
use Symfony\Component\DependencyInjection\ChildDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * Class InjectProcessor
21
 * @package InjectionBundle
22
 * @author  Joe Mizzi <[email protected]>
23
 */
24
class InjectProcessor
25
{
26 13
    public function process($annotation, $class, ContainerBuilder $container): void
27
    {
28 13
        if (!($annotation instanceof Inject)) {
29 1
            return;
30
        }
31
32 12
        $environmentGroups = $container->getParameter('injection.environment_groups');
33
34 12
        foreach ($annotation->environmentGroups as $group) {
35 2
            if (isset($environmentGroups[$group]['environments'])) {
36 2
                foreach ($environmentGroups[$group]['environments'] as $environment) {
37 2
                    if (!\in_array($environment, $annotation->environments, true)) {
38 2
                        $annotation->environments[] = $environment;
39
                    }
40
                }
41
            }
42
        }
43
44 12
        if ($annotation->environmentStrategy === 'exclude') {
45 10
            if (\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) {
46 10
                return;
47
            }
48
        } else {
49 2
            if (!\in_array($container->getParameter('kernel.environment'), $annotation->environments, true)) {
50
                return;
51
            }
52
        }
53
54 10
        if ($annotation->parent) {
55 3
            $definition = new ChildDefinition($annotation->parent);
56
        } else {
57 7
            $definition = new Definition($class);
58
        }
59
60 10
        if ($annotation->factory) {
61 1
            $definition->setFactory([$annotation->factory->class, $annotation->factory->method]);
62
        }
63
64 10
        foreach ($annotation->aliases as $alias) {
65 1
            $container->setAlias($alias, new Alias($class));
66
        }
67
68
        /** @var Argument $argument */
69 10
        foreach ($annotation->arguments as $argument) {
70 2
            if ($argument->value[0] === '@') {
71 1
                $definition->setArgument('$'.$argument->name, new Reference(substr($argument->value, 1)));
72
            } else {
73 2
                $definition->setArgument('$'.$argument->name, $argument->value);
74
            }
75
        }
76
77
        /** @var MethodCall $methodCall */
78 10
        foreach ($annotation->methodCalls as $methodCall) {
79 1
            $definition->addMethodCall($methodCall->method, $methodCall->arguments);
80
        }
81
82
        /**
83
         * @var Tag $tag
84
         */
85 10
        foreach ($annotation->tags as $tag) {
86 1
            $definition->addTag($tag->name, $tag->attributes);
87
        }
88
89 10
        if (!$annotation->parent) {
90 7
            $definition->setAutoconfigured($annotation->autoconfigured);
91
        }
92
93 10
        $definition->setAutowired($annotation->autowired);
94 10
        $definition->setPublic($annotation->public);
95 10
        $definition->setLazy($annotation->lazy);
96 10
        $definition->setAbstract($annotation->abstract);
97 10
        $definition->setShared($annotation->shared);
98
99 10
        $container->setDefinition($annotation->id ?: $class, $definition);
100
    }
101
}