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

Extension   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 6.52 %

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 138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A load() 0 16 1
B configure() 0 83 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
                ->scalarNode('doctrine')
61
                    ->defaultValue('doctrine')
62
                    ->addDefaultsIfNotSet()
63
                    ->children()
64
                        ->scalarNode('service')
65
                            ->defaultValue('doctrine')
66
                        ->end()
67
                    ->end()
68
                ->end()
69
                ->arrayNode('alice')
70
                    ->addDefaultsIfNotSet()
71
                    ->children()
72
                        ->scalarNode('locale')
73
                            ->defaultValue('en_US')
74
                        ->end()
75
                        ->arrayNode('fixtures')
76
                            ->prototype('scalar')->end()
77
                        ->end()
78
                        ->arrayNode('dependencies')
79
                            ->useAttributeAsKey('name')
80
                            ->prototype('array')
81
                                ->prototype('scalar')->end()
82
                            ->end()
83
                        ->end()
84
                        ->arrayNode('providers')
85
                            ->prototype('scalar')->end()
86
                        ->end()
87
                    ->end()
88
                ->end()
89
                ->arrayNode('entities')
90
                    ->addDefaultsIfNotSet()
91
                    ->children()
92
                        ->arrayNode('namespaces')
93
                            ->prototype('scalar')->end()
94
                        ->end()
95
                    ->end()
96
                ->end()
97
                ->arrayNode('page')
98
                    ->addDefaultsIfNotSet()
99
                    ->children()
100
                        ->scalarNode('namespace')
101
                            ->defaultValue('Page')
102
                        ->end()
103
                    ->end()
104
                ->end()
105
                ->arrayNode('api')
106
                    ->addDefaultsIfNotSet()
107
                    ->children()
108
                        ->scalarNode('base_url')
109
                            ->defaultValue('')
110
                        ->end()
111
                    ->end()
112
                ->end()
113
                ->scalarNode('smartTag')
114
                    ->defaultValue('smartStep')
115
                ->end()
116
            ->end()
117
        ;
118
    }
119
120
    public function process(ContainerBuilder $container)
121
    {
122
    }
123
124
    public function getConfigKey()
125
    {
126
        return 'friendly';
127
    }
128
129 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...
130
    {
131
        foreach ($config as $key => $element) {
132
            if (is_array($element) && $this->arrayHasStringKeys($element)) {
133
                $this->buildParameters(sprintf('%s.%s', $name, $key), $parameters, $element);
134
            }
135
            $parameters[sprintf('%s.%s', $name, $key)] = $element;
136
        }
137
    }
138
139
    protected function arrayHasStringKeys(array $array)
140
    {
141
        foreach ($array as $key => $value) {
142
            if (is_string($key)) {
143
144
                return true;
145
            }
146
        }
147
148
        return false;
149
    }
150
}
151