Completed
Pull Request — master (#345)
by Enrico
04:40
created

Configuration::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 296
    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 296
        $this->path = $path;
37
38 296
        $processor = new Processor();
39
40 296
        $configTree = $this->getConfigTreeBuilder($analyzersConfiguration);
41
42 296
        $this->configuration = $processor->process(
43 296
            $configTree->buildTree(),
44
            $configuration
45 296
        );
46 296
    }
47
48
    /**
49
     * Generates the configuration tree.
50
     *
51
     * @param array $analyzersConfiguration
52
     *
53
     * @return TreeBuilder
54
     */
55 296
    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 296
        $treeBuilder = new TreeBuilder();
58 296
        $root = $treeBuilder->root('phpsa');
59
60
        $root
61 296
            ->children()
62 296
                ->booleanNode('blame')->defaultFalse()->end()
63 296
                ->scalarNode('language_level')
64 296
                    ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION)
65 296
                    ->attribute('example', '5.3')
66 296
                    ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.')
67 296
                ->end()
68 296
                ->enumNode('parser')
69 296
                    ->defaultValue('prefer-7')
70 296
                    ->attribute('label', 'Check types of Arguments.')
71 296
                    ->values([
72 296
                        ParserFactory::PREFER_PHP7 => 'prefer-7',
73 296
                        ParserFactory::PREFER_PHP5 => 'prefer-5',
74 296
                        ParserFactory::ONLY_PHP7 => 'only-7',
75 296
                        ParserFactory::ONLY_PHP5 => 'only-5'
76 296
                    ])
77 296
                ->end()
78 296
            ->end()
79
        ;
80
81
        $analyzersConfigRoot = $root
82 296
            ->children()
83 296
                ->arrayNode('analyzers')
84 296
                ->addDefaultsIfNotSet();
85
86 296
        $language_error = (new TreeBuilder())->root('language_error')
87 296
            ->info("Contains all compiler notices. Those are raised when PHP with strict error reporting would create at least a Notice message. (mostly experimental)")
88 296
            ->canBeDisabled();
89
90 296
        $analyzersConfigRoot->append($language_error);
91
92 296
        foreach ($analyzersConfiguration as $config) {
93 55
            $analyzersConfigRoot->append($config);
94 296
        }
95
96 296
        return $treeBuilder;
97
    }
98
99
    /**
100
     * Sets a configuration setting.
101
     *
102
     * @param string $key
103
     * @param mixed $value
104
     */
105 52
    public function setValue($key, $value)
106
    {
107 52
        $this->configuration[$key] = $value;
108 52
    }
109
110
    /**
111
     * Gets a configuration setting.
112
     *
113
     * @param string $key
114
     * @param mixed $default
115
     *
116
     * @return mixed
117
     */
118 54
    public function getValue($key, $default = null)
119
    {
120 54
        if (array_key_exists($key, $this->configuration)) {
121 54
            return $this->configuration[$key];
122
        }
123
124
        return $default;
125
    }
126
127
    /**
128
     * Checks if a configuration setting is set.
129
     *
130
     * @param string $key
131
     * @return bool
132
     */
133 54
    public function valueIsTrue($key)
134
    {
135 54
        return (bool) $this->configuration[$key];
136
    }
137
138
    /**
139
     * @return string
140
     */
141 2
    public function getPath()
142
    {
143 2
        return $this->path;
144
    }
145
}
146