Completed
Push — master ( 6255e6...e55918 )
by Maxime
02:20
created

Extension::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

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