Completed
Push — master ( f9a038...cc9983 )
by Kamil
22:34
created

Configuration::addGridsSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 67
rs 9.2815
c 2
b 0
f 1
cc 1
eloc 64
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
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\GridBundle\DependencyInjection;
13
14
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver as DoctrineORMDriver;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $rootNode = $treeBuilder->root('sylius_grid');
31
32
        $this->addDriversSection($rootNode);
33
        $this->addTemplatesSection($rootNode);
34
        $this->addGridsSection($rootNode);
35
36
        return $treeBuilder;
37
    }
38
39
    private function addDriversSection(ArrayNodeDefinition $node)
40
    {
41
        // determine which drivers are distributed with this bundle
42
        $driverDir = __DIR__ . '/../Resources/config/driver';
43
        $iterator = new \RecursiveDirectoryIterator($driverDir);
44
        foreach (new \RecursiveIteratorIterator($iterator) as $file) {
45
            if ($file->getExtension() !== 'xml') {
46
                continue;
47
            }
48
49
            // we use the parent directory name in addition to the filename to
50
            // determine the name of the driver (e.g. doctrine/orm)
51
            $validDrivers[] = substr($file->getPathname(), 1 + strlen($driverDir), -4);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$validDrivers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $validDrivers = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
52
        }
53
54
        $node
55
            ->children()
56
                ->arrayNode('drivers')
57
                    ->info('Enable drivers which are distributed with this bundle')
58
                    ->validate()
59
                    ->ifTrue(function ($value) use ($validDrivers) { 
0 ignored issues
show
Bug introduced by
The variable $validDrivers does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
60
                        return 0 !== count(array_diff($value, $validDrivers)); 
61
                    })
62
                        ->thenInvalid(sprintf('Invalid driver specified in %%s, valid drivers: ["%s"]', implode('", "', $validDrivers)))
63
                    ->end()
64
                    ->defaultValue(['doctrine/orm'])
65
                    ->prototype('scalar')->end()
66
                ->end()
67
            ->end();
68
    }
69
70
    /**
71
     * @param ArrayNodeDefinition $node
72
     */
73
    private function addTemplatesSection(ArrayNodeDefinition $node)
74
    {
75
        $node
76
            ->children()
77
                ->arrayNode('templates')
78
                    ->children()
79
                        ->arrayNode('filter')
80
                            ->useAttributeAsKey('name')
81
                            ->prototype('scalar')->end()
82
                        ->end()
83
                        ->arrayNode('action')
84
                            ->useAttributeAsKey('name')
85
                            ->prototype('scalar')->end()
86
                        ->end()
87
                    ->end()
88
                ->end()
89
            ->end()
90
        ;
91
    }
92
93
    /**
94
     * @param ArrayNodeDefinition $node
95
     */
96
    private function addGridsSection(ArrayNodeDefinition $node)
97
    {
98
        $node
99
            ->children()
100
                ->arrayNode('grids')
101
                    ->useAttributeAsKey('code')
102
                    ->prototype('array')
103
                        ->children()
104
                            ->arrayNode('driver')
105
                                ->addDefaultsIfNotSet()
106
                                ->children()
107
                                    ->scalarNode('name')->defaultValue(DoctrineORMDriver::NAME)->end()
108
                                    ->arrayNode('options')
109
                                        ->prototype('variable')->end()
110
                                        ->defaultValue([])
111
                                    ->end()
112
                                ->end()
113
                            ->end()
114
                            ->arrayNode('sorting')
115
                                ->prototype('scalar')->end()
116
                            ->end()
117
                            ->arrayNode('fields')
118
                                ->useAttributeAsKey('name')
119
                                ->prototype('array')
120
                                    ->children()
121
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
122
                                        ->scalarNode('label')->cannotBeEmpty()->end()
123
                                        ->scalarNode('path')->cannotBeEmpty()->end()
124
                                        ->arrayNode('options')
125
                                            ->prototype('variable')->end()
126
                                        ->end()
127
                                    ->end()
128
                                ->end()
129
                            ->end()
130
                            ->arrayNode('filters')
131
                                ->useAttributeAsKey('name')
132
                                ->prototype('array')
133
                                    ->children()
134
                                        ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
135
                                        ->scalarNode('label')->cannotBeEmpty()->end()
136
                                        ->arrayNode('options')
137
                                            ->prototype('variable')->end()
138
                                        ->end()
139
                                    ->end()
140
                                ->end()
141
                            ->end()
142
                            ->arrayNode('actions')
143
                                ->useAttributeAsKey('name')
144
                                ->prototype('array')
145
                                    ->useAttributeAsKey('name')
146
                                    ->prototype('array')
147
                                        ->children()
148
                                            ->scalarNode('type')->isRequired()->end()
149
                                            ->scalarNode('label')->end()
150
                                            ->arrayNode('options')
151
                                                ->prototype('variable')->end()
152
                                            ->end()
153
                                        ->end()
154
                                    ->end()
155
                                ->end()
156
                            ->end()
157
                        ->end()
158
                    ->end()
159
                ->end()
160
            ->end()
161
        ;
162
    }
163
}
164