Completed
Branch master (2f8908)
by Nikola
03:25 queued 01:05
created

Extension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
c 3
b 1
f 1
lcom 1
cbo 3
dl 12
loc 119
ccs 49
cts 63
cp 0.7778
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 1
A getAlias() 0 4 1
B setInjectionMapAsContainerParameter() 0 33 3
A setFiltersAsContainerParameter() 6 18 4
B setExclusionsAsContainerParameter() 6 25 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
 * This file is part of the  TraitorBundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\Traitor\DependencyInjection;
11
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
14
15
class Extension extends BaseExtension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 4
    public function load(array $configs, ContainerBuilder $container)
21
    {
22 4
        $configuration = new Configuration();
23 4
        $config = $this->processConfiguration($configuration, $configs);
24
25 4
        $this
26 4
            ->setInjectionMapAsContainerParameter($config, $container)
27 4
            ->setFiltersAsContainerParameter($config, $container)
28 4
            ->setExclusionsAsContainerParameter($config, $container)
29
            ;
30 4
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 4
    public function getAlias()
36
    {
37 4
        return 'run_open_code_traitor';
38
    }
39
40
    /**
41
     * @param array $config
42
     * @param ContainerBuilder $container
43
     * @return Extension $this
44
     */
45 4
    protected function setInjectionMapAsContainerParameter(array $config, ContainerBuilder $container)
46
    {
47 4
        $injection = array();
48
49 4
        foreach ($config['inject'] as $trait => $injection) {
50
            $injection[ltrim($trait, '\\')] = $injection;
51 4
        }
52
53 4
        if ($config['use_common_traits']) {
54
55 2
            $injection = array_merge($injection, array(
56 2
                'Symfony\Component\DependencyInjection\ContainerAwareTrait' => array('setContainer', array('@service_container')),
57 2
                'Psr\Log\LoggerAwareTrait' => array('setLogger', array('@logger')),
58 2
                'RunOpenCode\Bundle\Traitor\Traits\DoctrineAwareTrait' => array('setDoctrine', array('@doctrine')),
59 2
                'RunOpenCode\Bundle\Traitor\Traits\EventDispatcherAwareTrait' => array('setEventDispatcher', array('@event_dispatcher')),
60 2
                'RunOpenCode\Bundle\Traitor\Traits\FilesystemAwareTrait' => array('setFilesystem', array('@filesystem')),
61 2
                'RunOpenCode\Bundle\Traitor\Traits\KernelAwareTrait' => array('setKernel', array('@kernel')),
62 2
                'RunOpenCode\Bundle\Traitor\Traits\MailerAwareInterface' => array('setMailer', array('@mailer')),
63 2
                'RunOpenCode\Bundle\Traitor\Traits\PropertyAccessorAwareTrait' => array('setPropertyAccessor', array('@property_accessor')),
64 2
                'RunOpenCode\Bundle\Traitor\Traits\RequestStackAwareTrait' => array('setRequestStack', array('@request_stack')),
65 2
                'RunOpenCode\Bundle\Traitor\Traits\RouterAwareTrait' => array('setRouter', array('@router')),
66 2
                'RunOpenCode\Bundle\Traitor\Traits\AuthorizationCheckerAwareTrait' => array('setAuthorizationChecker', array('@security.authorization_checker')),
67 2
                'RunOpenCode\Bundle\Traitor\Traits\SessionAwareTrait' => array('setSession', array('@session')),
68 2
                'RunOpenCode\Bundle\Traitor\Traits\TwigAwareTrait' => array('setTwig', array('@twig')),
69 2
                'RunOpenCode\Bundle\Traitor\Traits\TranslatorAwareTrait' => array('setTranslator', array('@translator')),
70 2
                'RunOpenCode\Bundle\Traitor\Traits\ValidatorAwareTrait' => array('setValidator', array('@validator'))
71 2
            ));
72 2
        }
73
74 4
        $container->setParameter('roc.traitor.injection_map', $injection);
75
76 4
        return $this;
77
    }
78
79
    /**
80
     * @param array $config
81
     * @param ContainerBuilder $container
82
     * @return Extension $this
83
     */
84 4
    protected function setFiltersAsContainerParameter(array $config, ContainerBuilder $container)
85
    {
86 4
        if (isset($config['filters'])) {
87
88 2
            if (count($config['filters']['tags']) > 0) {
89 2
                $container->setParameter('roc.traitor.filter.tags', $config['filters']['tags']);
90 2
            }
91
92 2 View Code Duplication
            if (count($config['filters']['namespaces']) > 0) {
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...
93
94
                $container->setParameter('roc.traitor.filter.namespaces', array_map(function($namespace) {
95 2
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
96 2
                }, $config['filters']['namespaces']));
97 4
            }
98 2
        }
99
100 4
        return $this;
101
    }
102
103
    /**
104
     * @param array $config
105
     * @param ContainerBuilder $container
106
     * @return Extension $this
107
     */
108 4
    protected function setExclusionsAsContainerParameter(array $config, ContainerBuilder $container)
109
    {
110 4
        if (isset($config['exclude'])) {
111
112
            if (count($config['exclude']['services']) > 0) {
113
                $container->setParameter('roc.traitor.exclude.services', $config['exclude']['services']);
114
            }
115
116 View Code Duplication
            if (count($config['exclude']['namespaces']) > 0) {
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...
117
118
                $container->setParameter('roc.traitor.exclude.namespaces', array_map(function($namespace) {
119
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
120
                }, $config['exclude']['namespaces']));
121
            }
122
123
            if (count($config['exclude']['classes']) > 0) {
124
125
                $container->setParameter('roc.traitor.exclude.classes', array_map(function($fqcn) {
126
                    return ltrim($fqcn, '\\');
127
                }, $config['exclude']['classes']));
128
            }
129
        }
130
131 4
        return $this;
132
    }
133
}
134
135