|
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, \ArrayAccess |
|
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
|
732 |
|
public function __construct(array $configuration = [], $analyzersConfiguration = []) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
732 |
|
$processor = new Processor(); |
|
29
|
|
|
|
|
30
|
732 |
|
$configTree = $this->getConfigTreeBuilder($analyzersConfiguration); |
|
31
|
|
|
|
|
32
|
732 |
|
$this->configuration = $processor->process( |
|
33
|
732 |
|
$configTree->buildTree(), |
|
34
|
|
|
$configuration |
|
35
|
732 |
|
); |
|
36
|
732 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Generates the configuration tree. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $analyzersConfiguration |
|
42
|
|
|
* |
|
43
|
|
|
* @return TreeBuilder |
|
44
|
|
|
*/ |
|
45
|
732 |
|
public function getConfigTreeBuilder($analyzersConfiguration = []) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
732 |
|
$treeBuilder = new TreeBuilder(); |
|
48
|
732 |
|
$root = $treeBuilder->root('phpsa'); |
|
49
|
|
|
|
|
50
|
|
|
$root |
|
51
|
732 |
|
->children() |
|
52
|
732 |
|
->booleanNode('blame')->defaultFalse()->end() |
|
53
|
732 |
|
->end() |
|
54
|
732 |
|
->children() |
|
55
|
732 |
|
->enumNode('parser') |
|
56
|
732 |
|
->defaultValue('prefer-7') |
|
57
|
732 |
|
->attribute('label', 'Check types of Arguments.') |
|
58
|
732 |
|
->values([ |
|
59
|
732 |
|
ParserFactory::PREFER_PHP7 => 'prefer-7', |
|
60
|
732 |
|
ParserFactory::PREFER_PHP5 => 'prefer-5', |
|
61
|
732 |
|
ParserFactory::ONLY_PHP7 => 'only-7', |
|
62
|
732 |
|
ParserFactory::ONLY_PHP5 => 'only-5' |
|
63
|
732 |
|
]) |
|
64
|
732 |
|
->end() |
|
65
|
732 |
|
->end() |
|
66
|
|
|
; |
|
67
|
|
|
|
|
68
|
|
|
$passes = $root |
|
69
|
732 |
|
->children() |
|
70
|
732 |
|
->arrayNode('analyzers') |
|
71
|
732 |
|
->addDefaultsIfNotSet(); |
|
72
|
|
|
|
|
73
|
732 |
|
foreach ($analyzersConfiguration as $config) { |
|
74
|
1 |
|
$passes->append($config); |
|
75
|
732 |
|
} |
|
76
|
|
|
|
|
77
|
732 |
|
return $treeBuilder; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets a configuration setting. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setValue($key, $value) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->configuration[$key] = $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Checks if a configuration setting is set. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $key |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
19 |
|
public function valueIsTrue($key) |
|
98
|
|
|
{ |
|
99
|
19 |
|
return (bool) $this->configuration[$key]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function offsetExists($offset) |
|
106
|
|
|
{ |
|
107
|
|
|
return array_key_exists($offset, $this->configuration); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function offsetGet($offset) |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->configuration[$offset]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function offsetSet($offset, $value) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->setValue($offset, $value); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function offsetUnset($offset) |
|
130
|
|
|
{ |
|
131
|
|
|
unset($this->configuration[$offset]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.