Completed
Pull Request — master (#139)
by Kévin
06:24
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 864
    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 864
        $processor = new Processor();
29
30 864
        $configTree = $this->getConfigTreeBuilder($analyzersConfiguration);
31
32 864
        $this->configuration = $processor->process(
33 864
            $configTree->buildTree(),
34
            $configuration
35 864
        );
36 864
    }
37
38
    /**
39
     * Generates the configuration tree.
40
     *
41
     * @param array $analyzersConfiguration
42
     *
43
     * @return TreeBuilder
44
     */
45 864
    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 864
        $treeBuilder = new TreeBuilder();
48 864
        $root = $treeBuilder->root('phpsa');
49
50
        $root
51 864
            ->children()
52 864
                ->booleanNode('blame')->defaultFalse()->end()
53 864
                ->scalarNode('language_level')
54 864
                    ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION)
55 864
                    ->attribute('example', '5.3')
56 864
                    ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.')
57 864
                ->end()
58 864
                ->enumNode('parser')
59 864
                    ->defaultValue('prefer-7')
60 864
                    ->attribute('label', 'Check types of Arguments.')
61 864
                    ->values([
62 864
                        ParserFactory::PREFER_PHP7 => 'prefer-7',
63 864
                        ParserFactory::PREFER_PHP5 => 'prefer-5',
64 864
                        ParserFactory::ONLY_PHP7 => 'only-7',
65 864
                        ParserFactory::ONLY_PHP5 => 'only-5'
66 864
                    ])
67 864
                ->end()
68 864
            ->end()
69
        ;
70
71
        $analyzersConfigRoot = $root
72 864
            ->children()
73 864
                ->arrayNode('analyzers')
74 864
                ->addDefaultsIfNotSet();
75
76 864
        foreach ($analyzersConfiguration as $config) {
77 1
            $analyzersConfigRoot->append($config);
78 864
        }
79
80 864
        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 37
    public function valueIsTrue($key)
118
    {
119 37
        return (bool) $this->configuration[$key];
120
    }
121
}
122