Configuration::getFinderConfigurationNode()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 45
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 37
nc 1
nop 0
1
<?php
2
3
namespace SLLH\StyleCIBridge\StyleCI;
4
5
use SLLH\StyleCIBridge\ConfigBridge;
6
use SLLH\StyleCIFixers\Fixers;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
12
/**
13
 * @author Sullivan Senechal <[email protected]>
14
 */
15
final class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getConfigTreeBuilder()
21
    {
22
        $treeBuilder = new TreeBuilder();
23
        $rootNode = $treeBuilder->root('styleci');
24
25
        $validFixers = array_merge(Fixers::$valid, array_keys(Fixers::$aliases));
26
27
        $rootNode
28
            ->children()
29
                ->enumNode('preset')
30
                    ->isRequired()
31
                    ->values(array_merge(array_keys(Fixers::getPresets()), array(ConfigBridge::PRESET_NONE)))
32
                ->end()
33
                ->booleanNode('linting')
34
                    ->defaultTrue()
35
                ->end()
36
                ->arrayNode('enabled')
37
                    ->beforeNormalization()
38
                        ->ifString()
39
                        ->then(function ($v) {
40
                            return array($v);
41
                        })
42
                    ->end()
43
                    ->prototype('scalar')
44
                        ->validate()
45
                            ->ifNotInArray($validFixers)
46
                            ->thenInvalid('Invalid enabled fixer %s.')
47
                        ->end()
48
                    ->end()
49
                ->end()
50
                ->arrayNode('disabled')
51
                    ->beforeNormalization()
52
                        ->ifString()
53
                        ->then(function ($v) {
54
                            return array($v);
55
                        })
56
                    ->end()
57
                    ->prototype('scalar')
58
                        ->validate()
59
                            ->ifNotInArray($validFixers)
60
                            ->thenInvalid('Invalid disabled fixer %s.')
61
                        ->end()
62
                    ->end()
63
                ->end()
64
                ->append($this->getFinderConfigurationNode())
65
                ->booleanNode('risky')
66
                    ->defaultTrue()
67
                ->end()
68
            ->end()
69
            ->validate()
70
                ->ifTrue(function ($config) {
71
                    $presets = Fixers::getPresets();
72
                    $enabledFixers = ConfigBridge::PRESET_NONE === $config['preset']
73
                        ? $config['enabled']
74
                        : array_merge($presets[$config['preset']], $config['enabled']);
75
                    $disabledFixers = $config['disabled'];
76
                    $fixers = array_diff($enabledFixers, $disabledFixers);
77
78
                    // See: https://github.com/StyleCI/Config/blob/f9747aba632aa4d272f212b5b9c9942234f4f074/src/Config.php#L549-L553
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
79
                    foreach (Fixers::$conflicts as $first => $second) {
80
                        if (in_array($first, $fixers, true) && in_array($second, $fixers, true)) {
81
                            return true;
82
                        }
83
                    }
84
85
                    return false;
86
                })
87
                ->thenInvalid('Conflicted fixers. Check conflicts definition.')
88
            ->end()
89
        ;
90
91
        return $treeBuilder;
92
    }
93
94
    /**
95
     * @return ArrayNodeDefinition|NodeDefinition
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ArrayNodeDefinition.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
96
     */
97
    private function getFinderConfigurationNode()
98
    {
99
        $treeBuilder = new TreeBuilder();
100
        $node = $treeBuilder->root('finder');
101
102
        $node
103
            ->beforeNormalization()
104
                ->always(function ($v) {
105
                    foreach ($v as $option => $value) {
106
                        $v[$option] = (array) $value;
107
                    }
108
109
                    return $v;
110
                })
111
            ->end()
112
            ->children()
113
                ->arrayNode('exclude')
114
                    ->prototype('scalar')->end()
115
                ->end()
116
                ->arrayNode('name')
117
                    ->prototype('scalar')->end()
118
                ->end()
119
                ->arrayNode('not_name')
120
                    ->prototype('scalar')->end()
121
                ->end()
122
                ->arrayNode('contains')
123
                    ->prototype('scalar')->end()
124
                ->end()
125
                ->arrayNode('not_contains')
126
                    ->prototype('scalar')->end()
127
                ->end()
128
                ->arrayNode('path')
129
                    ->prototype('scalar')->end()
130
                ->end()
131
                ->arrayNode('not_path')
132
                    ->prototype('scalar')->end()
133
                ->end()
134
                ->arrayNode('depth')
135
                    ->prototype('scalar')->end()
136
                ->end()
137
            ->end()
138
        ;
139
140
        return $node;
141
    }
142
}
143