Completed
Pull Request — master (#139)
by Kévin
29:26 queued 14:26
created

Configuration::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
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
class Configuration implements ConfigurationInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $configuration;
19
20
    /**
21
     * Create a configuration from array.
22
     *
23
     * @param array $configuration
24
     * @param array $analyzersConfiguration
25
     */
26 866
    public function __construct(array $configuration = [], 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...
27
    {
28 866
        $processor = new Processor();
29 866
30
        $configTree = $this->getConfigTreeBuilder($analyzersConfiguration);
31 866
32 866
        $this->configuration = $processor->process(
33
            $configTree->buildTree(),
34 866
            $configuration
35 866
        );
36
    }
37
38
    /**
39
     * Generates the configuration tree.
40
     *
41
     * @param array $analyzersConfiguration
42 866
     *
43
     * @return TreeBuilder
44 866
     */
45
    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...
46 866
    {
47 866
        $treeBuilder = new TreeBuilder();
48 866
        $root = $treeBuilder->root('phpsa');
49 866
50 866
        $root
51 866
            ->children()
52 866
                ->booleanNode('blame')->defaultFalse()->end()
53 866
                ->scalarNode('language_level')
54 866
                    ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION)
55 866
                    ->attribute('example', '5.3')
56 866
                    ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.')
57 866
                ->end()
58 866
                ->enumNode('parser')
59 866
                    ->defaultValue('prefer-7')
60 866
                    ->attribute('label', 'Check types of Arguments.')
61 866
                    ->values([
62
                        ParserFactory::PREFER_PHP7 => 'prefer-7',
63
                        ParserFactory::PREFER_PHP5 => 'prefer-5',
64 866
                        ParserFactory::ONLY_PHP7 => 'only-7',
65
                        ParserFactory::ONLY_PHP5 => 'only-5'
66
                    ])
67
                ->end()
68
            ->end()
69
        ;
70
71
        $analyzersConfigRoot = $root
72
            ->children()
73
                ->arrayNode('analyzers')
74
                ->addDefaultsIfNotSet();
75
76
        foreach ($analyzersConfiguration as $config) {
77
            $analyzersConfigRoot->append($config);
78
        }
79
80
        return $treeBuilder;
81
    }
82
83
    /**
84 39
     * Sets a configuration setting.
85
     *
86 39
     * @param string $key
87
     * @param string $value
88
     */
89
    public function setValue($key, $value)
90
    {
91
        $this->configuration[$key] = $value;
92
    }
93
94
    /**
95
     * Gets a configuration setting.
96
     *
97
     * @param string $key
98
     * @param mixed $default
99
     *
100
     * @return mixed
101
     */
102
    public function getValue($key, $default = null)
103
    {
104
        if (array_key_exists($key, $this->configuration)) {
105
            return $this->configuration[$key];
106
        }
107
108
        return $default;
109
    }
110
111
    /**
112
     * Checks if a configuration setting is set.
113
     *
114
     * @param string $key
115
     * @return bool
116
     */
117
    public function valueIsTrue($key)
118
    {
119
        return (bool) $this->configuration[$key];
120
    }
121
}
122