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