1 | <?php |
||
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 = "") |
|
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 | ->defaultValue(['/vendor']) |
||
86 | 297 | ->end(); |
|
87 | 297 | ||
88 | 297 | $analyzersConfigRoot = $root |
|
89 | ->children() |
||
90 | 297 | ->arrayNode('analyzers') |
|
91 | ->addDefaultsIfNotSet(); |
||
92 | 297 | ||
93 | 56 | $language_error = (new TreeBuilder())->root('language_error') |
|
94 | ->info("Contains all compiler notices. Those are raised when PHP with strict error reporting would create at least a Notice message. (mostly experimental)") |
||
95 | ->canBeDisabled(); |
||
96 | 297 | ||
97 | $analyzersConfigRoot->append($language_error); |
||
98 | |||
99 | foreach ($analyzersConfiguration as $config) { |
||
100 | $analyzersConfigRoot->append($config); |
||
101 | } |
||
102 | |||
103 | return $treeBuilder; |
||
104 | } |
||
105 | 53 | ||
106 | /** |
||
107 | 53 | * Sets a configuration setting. |
|
108 | 53 | * |
|
109 | * @param string $key |
||
110 | * @param mixed $value |
||
111 | */ |
||
112 | public function setValue($key, $value) |
||
113 | { |
||
114 | $this->configuration[$key] = $value; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | 55 | * Gets a configuration setting. |
|
119 | * |
||
120 | 55 | * @param string $key |
|
121 | 55 | * @param mixed $default |
|
122 | * |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function getValue($key, $default = null) |
||
126 | { |
||
127 | if (array_key_exists($key, $this->configuration)) { |
||
128 | return $this->configuration[$key]; |
||
129 | } |
||
130 | |||
131 | return $default; |
||
132 | } |
||
133 | 55 | ||
134 | /** |
||
135 | 55 | * Checks if a configuration setting is set. |
|
136 | * |
||
137 | * @param string $key |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function valueIsTrue($key) |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getPath() |
||
152 | } |
||
153 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.