Completed
Pull Request — master (#139)
by Kévin
03:44
created

Configuration::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
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 747
    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 747
        $processor = new Processor();
29
30 747
        $configTree = $this->getConfigTreeBuilder($analyzersConfiguration);
31
32 747
        $this->configuration = $processor->process(
33 747
            $configTree->buildTree(),
34
            $configuration
35 747
        );
36 747
    }
37
38
    /**
39
     * Generates the configuration tree.
40
     *
41
     * @param array $analyzersConfiguration
42
     *
43
     * @return TreeBuilder
44
     */
45 747
    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
    {
47 747
        $treeBuilder = new TreeBuilder();
48 747
        $root = $treeBuilder->root('phpsa');
49
50
        $root
51 747
            ->children()
52 747
                ->booleanNode('blame')->defaultFalse()->end()
53 747
                ->scalarNode('language_level')
54 747
                    ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION)
55 747
                    ->attribute('example', '5.3')
56 747
                    ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.')
57 747
                ->end()
58 747
                ->enumNode('parser')
59 747
                    ->defaultValue('prefer-7')
60 747
                    ->attribute('label', 'Check types of Arguments.')
61 747
                    ->values([
62 747
                        ParserFactory::PREFER_PHP7 => 'prefer-7',
63 747
                        ParserFactory::PREFER_PHP5 => 'prefer-5',
64 747
                        ParserFactory::ONLY_PHP7 => 'only-7',
65 747
                        ParserFactory::ONLY_PHP5 => 'only-5'
66 747
                    ])
67 747
                ->end()
68 747
            ->end()
69
        ;
70
71
        $analyzersConfigRoot = $root
72 747
            ->children()
73 747
                ->arrayNode('analyzers')
74 747
                ->addDefaultsIfNotSet();
75
76 747
        foreach ($analyzersConfiguration as $config) {
77 1
            $analyzersConfigRoot->append($config);
78 747
        }
79
80 747
        return $treeBuilder;
81
    }
82
83
    /**
84
     * Sets a configuration setting.
85
     *
86
     * @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 1
    public function getValue($key, $default = null)
103
    {
104 1
        if (array_key_exists($key, $this->configuration)) {
105 1
            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 23
    public function valueIsTrue($key)
118
    {
119 23
        return (bool) $this->configuration[$key];
120
    }
121
}
122