Completed
Pull Request — master (#56)
by Robin
07:16
created

src/StyleCI/Configuration.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SLLH\StyleCIBridge\StyleCI;
4
5
use SLLH\StyleCIFixers\Fixers;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
/**
12
 * @author Sullivan Senechal <[email protected]>
13
 */
14
final class Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getConfigTreeBuilder()
20
    {
21
        $treeBuilder = new TreeBuilder();
22
        $rootNode = $treeBuilder->root('styleci');
23
24
        $validFixers = array_merge(Fixers::$valid, array_keys(Fixers::$aliases));
25
26
        $rootNode
27
            ->children()
28
                ->enumNode('preset')
29
                    ->isRequired()
30
                    ->values(array_keys(Fixers::getPresets()))
31
                ->end()
32
                ->booleanNode('linting')
33
                    ->defaultTrue()
34
                ->end()
35
                ->arrayNode('enabled')
36
                    ->beforeNormalization()
37
                        ->ifString()
38
                        ->then(function ($v) { return array($v); })
39
                    ->end()
40
                    ->prototype('scalar')
41
                        ->validate()
42
                            ->ifNotInArray($validFixers)
43
                            ->thenInvalid('Invalid enabled fixer %s.')
44
                        ->end()
45
                    ->end()
46
                ->end()
47
                ->arrayNode('disabled')
48
                    ->beforeNormalization()
49
                        ->ifString()
50
                        ->then(function ($v) { return array($v); })
51
                    ->end()
52
                    ->prototype('scalar')
53
                        ->validate()
54
                            ->ifNotInArray($validFixers)
55
                            ->thenInvalid('Invalid disabled fixer %s.')
56
                        ->end()
57
                    ->end()
58
                ->end()
59
                ->append($this->getFinderConfigurationNode())
60
                ->booleanNode('risky')
61
                    ->defaultTrue()
62
                ->end()
63
            ->end()
64
            ->validate()
65
                ->ifTrue(function ($config) {
66
                    $presets = Fixers::getPresets();
67
                    $enabledFixers = array_merge($presets[$config['preset']], $config['enabled']);
68
                    $disabledFixers = $config['disabled'];
69
                    $fixers = array_diff($enabledFixers, $disabledFixers);
70
71
                    // See: https://github.com/StyleCI/Config/blob/f9747aba632aa4d272f212b5b9c9942234f4f074/src/Config.php#L549-L553
0 ignored issues
show
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...
72
                    foreach (Fixers::$conflicts as $first => $second) {
73
                        if (in_array($first, $fixers, true) && in_array($second, $fixers, true)) {
74
                            return true;
75
                        }
76
                    }
77
78
                    return false;
79
                })
80
                ->thenInvalid('Conflicted fixers. Check conflicts definition.')
81
            ->end()
82
        ;
83
84
        return $treeBuilder;
85
    }
86
87
    /**
88
     * @return ArrayNodeDefinition|NodeDefinition
89
     */
90
    private function getFinderConfigurationNode()
91
    {
92
        $treeBuilder = new TreeBuilder();
93
        $node = $treeBuilder->root('finder');
94
95
        $node
96
            ->beforeNormalization()
97
                ->always(function ($v) {
98
                    foreach ($v as $option => $value) {
99
                        $v[$option] = (array) $value;
100
                    }
101
102
                    return $v;
103
                })
104
            ->end()
105
            ->children()
106
                ->arrayNode('exclude')
107
                    ->prototype('scalar')->end()
108
                ->end()
109
                ->arrayNode('name')
110
                    ->prototype('scalar')->end()
111
                ->end()
112
                ->arrayNode('not_name')
113
                    ->prototype('scalar')->end()
114
                ->end()
115
                ->arrayNode('contains')
116
                    ->prototype('scalar')->end()
117
                ->end()
118
                ->arrayNode('not_contains')
119
                    ->prototype('scalar')->end()
120
                ->end()
121
                ->arrayNode('path')
122
                    ->prototype('scalar')->end()
123
                ->end()
124
                ->arrayNode('not_path')
125
                    ->prototype('scalar')->end()
126
                ->end()
127
                ->arrayNode('depth')
128
                    ->prototype('scalar')->end()
129
                ->end()
130
            ->end()
131
        ;
132
133
        return $node;
134
    }
135
}
136