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

Extension::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 83
rs 8.7468
cc 1
eloc 80
nc 1
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 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