Completed
Pull Request — master (#190)
by
unknown
11:45
created

Extension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 6.82 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 12
c 8
b 1
f 2
lcom 0
cbo 10
dl 9
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A load() 0 16 1
B configure() 0 77 1
A process() 0 3 1
A getConfigKey() 0 4 1
A buildParameters() 9 9 4
A arrayHasStringKeys() 0 11 3

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 Knp\FriendlyContexts;
4
5
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
6
use Behat\Testwork\ServiceContainer\ExtensionManager;
7
use Knp\FriendlyContexts\DependencyInjection\Compiler;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
13
class Extension implements ExtensionInterface
14
{
15
    public function initialize(ExtensionManager $extensionManager)
16
    {
17
    }
18
19
    public function load(ContainerBuilder $container, array $config)
20
    {
21
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/services'));
22
        $loader->load('core.yml');
23
        $loader->load('fakers.yml');
24
        $loader->load('guessers.yml');
25
        $loader->load('builder.yml');
26
27
        $container->setParameter('friendly.parameters', $config);
28
29
        $container->addCompilerPass(new Compiler\ConfigPass);
30
        $container->addCompilerPass(new Compiler\FormatGuesserPass);
31
        $container->addCompilerPass(new Compiler\FakerProviderPass);
32
        $container->addCompilerPass(new Compiler\ApiUrlPass);
33
        $container->addCompilerPass(new Compiler\KernelPass);
34
    }
35
36
    public function configure(ArrayNodeDefinition $builder)
37
    {
38
        $builder
39
            ->children()
40
                ->arrayNode('symfony_kernel')
41
                    ->addDefaultsIfNotSet()
42
                    ->children()
43
                        ->scalarNode('bootstrap')
44
                            ->defaultValue('app/autoload.php')
45
                        ->end()
46
                        ->scalarNode('path')
47
                            ->defaultValue('app/AppKernel.php')
48
                        ->end()
49
                        ->scalarNode('class')
50
                            ->defaultValue('AppKernel')
51
                        ->end()
52
                        ->scalarNode('env')
53
                            ->defaultValue('test')
54
                        ->end()
55
                        ->booleanNode('debug')
56
                            ->defaultTrue()
57
                        ->end()
58
                    ->end()
59
                ->end()
60
                ->arrayNode('alice')
61
                    ->addDefaultsIfNotSet()
62
                    ->children()
63
                        ->scalarNode('locale')
64
                            ->defaultValue('en_US')
65
                        ->end()
66
                        ->scalarNode('doctrine_service')
67
                            ->defaultValue('doctrine')
68
                        ->end()
69
                        ->arrayNode('fixtures')
70
                            ->prototype('scalar')->end()
71
                        ->end()
72
                        ->arrayNode('dependencies')
73
                            ->useAttributeAsKey('name')
74
                            ->prototype('array')
75
                                ->prototype('scalar')->end()
76
                            ->end()
77
                        ->end()
78
                        ->arrayNode('providers')
79
                            ->prototype('scalar')->end()
80
                        ->end()
81
                    ->end()
82
                ->end()
83
                ->arrayNode('entities')
84
                    ->addDefaultsIfNotSet()
85
                    ->children()
86
                        ->arrayNode('namespaces')
87
                            ->prototype('scalar')->end()
88
                        ->end()
89
                    ->end()
90
                ->end()
91
                ->arrayNode('page')
92
                    ->addDefaultsIfNotSet()
93
                    ->children()
94
                        ->scalarNode('namespace')
95
                            ->defaultValue('Page')
96
                        ->end()
97
                    ->end()
98
                ->end()
99
                ->arrayNode('api')
100
                    ->addDefaultsIfNotSet()
101
                    ->children()
102
                        ->scalarNode('base_url')
103
                            ->defaultValue('')
104
                        ->end()
105
                    ->end()
106
                ->end()
107
                ->scalarNode('smartTag')
108
                    ->defaultValue('smartStep')
109
                ->end()
110
            ->end()
111
        ;
112
    }
113
114
    public function process(ContainerBuilder $container)
115
    {
116
    }
117
118
    public function getConfigKey()
119
    {
120
        return 'friendly';
121
    }
122
123 View Code Duplication
    protected function buildParameters($name, &$parameters, $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
124
    {
125
        foreach ($config as $key => $element) {
126
            if (is_array($element) && $this->arrayHasStringKeys($element)) {
127
                $this->buildParameters(sprintf('%s.%s', $name, $key), $parameters, $element);
128
            }
129
            $parameters[sprintf('%s.%s', $name, $key)] = $element;
130
        }
131
    }
132
133
    protected function arrayHasStringKeys(array $array)
134
    {
135
        foreach ($array as $key => $value) {
136
            if (is_string($key)) {
137
138
                return true;
139
            }
140
        }
141
142
        return false;
143
    }
144
}
145