Completed
Pull Request — master (#354)
by Strahinja
03:19
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 38
nc 2
nop 1
dl 0
loc 50
ccs 30
cts 30
cp 1
crap 2
rs 9.3333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA;
7
8
use PhpParser\ParserFactory;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
use Symfony\Component\Config\Definition\Processor;
12
13
/**
14
 * PHPSA configuration
15
 */
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $configuration;
22
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * Create a configuration from array.
30
     *
31
     * @param array $configuration
32
     * @param array $analyzersConfiguration
33
     */
34 297
    public function __construct(array $configuration = [], array $analyzersConfiguration = [], $path = "")
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $analyzersConfiguration exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
35
    {
36 297
        $this->path = $path;
37
38 297
        $processor = new Processor();
39
40 297
        $configTree = $this->getConfigTreeBuilder($analyzersConfiguration);
41
42 297
        $this->configuration = $processor->process(
43 297
            $configTree->buildTree(),
44 297
            $configuration
45
        );
46 297
    }
47
48
    /**
49
     * Generates the configuration tree.
50
     *
51
     * @param array $analyzersConfiguration
52
     *
53
     * @return TreeBuilder
54
     */
55 297
    public function getConfigTreeBuilder(array $analyzersConfiguration = [])
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $analyzersConfiguration exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
    {
57 297
        $treeBuilder = new TreeBuilder();
58 297
        $root = $treeBuilder->root('phpsa');
59
60
        $root
61 297
            ->children()
62 297
                ->booleanNode('blame')->defaultFalse()->end()
63 297
                ->scalarNode('language_level')
64 297
                    ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION)
65 297
                    ->attribute('example', '5.3')
66 297
                    ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.')
67 297
                ->end()
68 297
                ->enumNode('parser')
69 297
                    ->defaultValue('prefer-7')
70 297
                    ->attribute('label', 'Check types of Arguments.')
71 297
                    ->values([
72 297
                        ParserFactory::PREFER_PHP7 => 'prefer-7',
73 297
                        ParserFactory::PREFER_PHP5 => 'prefer-5',
74 297
                        ParserFactory::ONLY_PHP7 => 'only-7',
75 297
                        ParserFactory::ONLY_PHP5 => 'only-5'
76
                    ])
77 297
                ->end()
78 297
            ->end()
79
        ;
80
81
        $ignoredFilesAndDirs = $root
0 ignored issues
show
Unused Code introduced by
$ignoredFilesAndDirs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82 297
            ->children()
83 297
                ->arrayNode('ignore')
84 297
                ->scalarPrototype()->end()
85
                ->defaultValue(['/vendor'])
86 297
            ->end();
87 297
88 297
        $analyzersConfigRoot = $root
89
            ->children()
90 297
                ->arrayNode('analyzers')
91
                ->addDefaultsIfNotSet();
92 297
93 56
        $language_error = (new TreeBuilder())->root('language_error')
94
            ->info("Contains all compiler notices. Those are raised when PHP with strict error reporting would create at least a Notice message. (mostly experimental)")
95
            ->canBeDisabled();
96 297
97
        $analyzersConfigRoot->append($language_error);
98
99
        foreach ($analyzersConfiguration as $config) {
100
            $analyzersConfigRoot->append($config);
101
        }
102
103
        return $treeBuilder;
104
    }
105 53
106
    /**
107 53
     * Sets a configuration setting.
108 53
     *
109
     * @param string $key
110
     * @param mixed $value
111
     */
112
    public function setValue($key, $value)
113
    {
114
        $this->configuration[$key] = $value;
115
    }
116
117
    /**
118 55
     * Gets a configuration setting.
119
     *
120 55
     * @param string $key
121 55
     * @param mixed $default
122
     *
123
     * @return mixed
124
     */
125
    public function getValue($key, $default = null)
126
    {
127
        if (array_key_exists($key, $this->configuration)) {
128
            return $this->configuration[$key];
129
        }
130
131
        return $default;
132
    }
133 55
134
    /**
135 55
     * Checks if a configuration setting is set.
136
     *
137
     * @param string $key
138
     * @return bool
139
     */
140
    public function valueIsTrue($key)
141 2
    {
142
        return (bool) $this->configuration[$key];
143 2
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function getPath()
149
    {
150
        return $this->path;
151
    }
152
}
153