1
|
|
|
<?php |
2
|
|
|
namespace CodeReview; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Simple configuration container handling basic options parsing nad providing convenient methods. |
6
|
|
|
* |
7
|
|
|
* @property string|null $subPath |
8
|
|
|
* @property string|null $maxVersion |
9
|
|
|
* @property bool $includeDisabledPlugins |
10
|
|
|
* @property bool $findDeprecatedFunctions |
11
|
|
|
* @property bool $findPrivateFunctions |
12
|
|
|
* @property bool $fixProblems |
13
|
|
|
*/ |
14
|
|
|
class Config { |
15
|
|
|
|
16
|
|
|
const T_PLUGINS_ALL = 0; |
17
|
|
|
const T_PLUGINS_ACTIVE = 1; |
18
|
|
|
const T_PLUGINS_INACTIVE = 2; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $options = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var callable |
27
|
|
|
*/ |
28
|
|
|
protected $versionGetter = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $options |
32
|
|
|
*/ |
33
|
19 |
|
public function __construct(array $options = array(), $versionGetter = null) { |
34
|
19 |
|
$this->options = (array)$options; |
35
|
|
|
|
36
|
19 |
|
if (is_callable($versionGetter)) { |
37
|
16 |
|
$this->versionGetter = $versionGetter; |
38
|
16 |
|
} else { |
39
|
|
|
//TODO possibly further decouple Elgg core dependency |
40
|
3 |
|
$this->versionGetter = 'elgg_get_version'; |
41
|
|
|
} |
42
|
19 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $key |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
14 |
|
public function __get($key) { |
49
|
14 |
|
return isset($this->options[$key]) ? $this->options[$key] : null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $key |
54
|
|
|
* @param null $default |
55
|
|
|
* @return null |
56
|
|
|
*/ |
57
|
16 |
|
public function getOption($key, $default = null) { |
58
|
16 |
|
return isset($this->options[$key]) ? $this->options[$key] : $default; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $key |
63
|
|
|
* @param $value |
64
|
|
|
*/ |
65
|
13 |
|
public function __set($key, $value) { |
66
|
13 |
|
$this->options[$key] = $value; |
67
|
13 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $type |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
5 |
|
public function getPluginIds($type) { |
74
|
5 |
|
$pluginsDirs = false; |
75
|
|
|
|
76
|
5 |
|
$config = \code_review::getConfig(); |
77
|
|
|
|
78
|
|
|
switch ($type) { |
79
|
5 |
|
case self::T_PLUGINS_INACTIVE: |
80
|
5 |
|
$pluginsDirs = $this->getPluginIds(self::T_PLUGINS_ALL); |
81
|
5 |
|
$actives = call_user_func($config['plugins_getter'], 'active'); |
82
|
5 |
|
foreach ($actives as $plugin) { |
83
|
5 |
|
if ($plugin instanceof \ElggPlugin) { |
|
|
|
|
84
|
|
|
$pluginsDirs = array_diff($pluginsDirs, array($plugin->getID())); |
85
|
|
|
} else { |
86
|
5 |
|
$pluginsDirs = array_diff($pluginsDirs, array($plugin)); |
87
|
|
|
} |
88
|
5 |
|
} |
89
|
5 |
|
break; |
90
|
5 |
|
case self::T_PLUGINS_ACTIVE: |
91
|
2 |
|
$pluginsDirs = call_user_func($config['plugins_getter'], 'active'); |
92
|
2 |
|
foreach ($pluginsDirs as $key => $plugin) { |
93
|
2 |
|
if ($plugin instanceof \ElggPlugin) { |
94
|
|
|
$pluginsDirs[$key] = $plugin->getID(); |
95
|
|
|
} |
96
|
2 |
|
} |
97
|
2 |
|
break; |
98
|
5 |
|
case self::T_PLUGINS_ALL: |
99
|
5 |
|
$pluginsDirs = \code_review::getPluginDirsInDir($config['pluginspath']); |
100
|
5 |
|
break; |
101
|
|
|
|
102
|
|
|
} |
103
|
5 |
|
return $pluginsDirs; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/* |
107
|
|
|
* Shorthand methods |
108
|
|
|
*/ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $key |
112
|
|
|
* @param array $array |
113
|
|
|
* @param null $default |
114
|
|
|
* @param bool $strict |
115
|
|
|
* @return null |
116
|
|
|
* |
117
|
|
|
* Function is a part of Elgg framework with following license: |
118
|
|
|
* |
119
|
|
|
* Copyright (c) 2013. See COPYRIGHT.txt |
120
|
|
|
* http://elgg.org/ |
121
|
|
|
* |
122
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
123
|
|
|
* a copy of this software and associated documentation files (the |
124
|
|
|
* "Software"), to deal in the Software without restriction, including |
125
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
126
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
127
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
128
|
|
|
* the following conditions: |
129
|
|
|
* |
130
|
|
|
* The above copyright notice and this permission notice shall be |
131
|
|
|
* included in all copies or substantial portions of the Software. |
132
|
|
|
* |
133
|
|
|
* Except as contained in this notice, the name(s) of the above copyright |
134
|
|
|
* holders shall not be used in advertising or otherwise to promote the |
135
|
|
|
* sale, use or other dealings in this Software without prior written |
136
|
|
|
* authorization. |
137
|
|
|
* |
138
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
139
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
140
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
141
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
142
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
143
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
144
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
145
|
|
|
*/ |
146
|
13 |
|
private function elggExtract($key, array $array, $default = null, $strict = true) { |
147
|
13 |
|
if (!is_array($array)) { |
148
|
|
|
return $default; |
149
|
|
|
} |
150
|
|
|
|
151
|
13 |
|
if ($strict) { |
152
|
13 |
|
return (isset($array[$key])) ? $array[$key] : $default; |
153
|
|
|
} else { |
154
|
|
|
return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param array $vars |
160
|
|
|
*/ |
161
|
13 |
|
public function parseInput(array $vars) { |
162
|
|
|
|
163
|
|
|
//sanitize provided path |
164
|
13 |
|
$subPath = $this->elggExtract('subpath', $vars, '/'); |
|
|
|
|
165
|
13 |
|
$subPath = trim($subPath, '/\\'); |
166
|
13 |
|
$subPath = str_replace('\\', '/', $subPath); |
167
|
13 |
|
$subPath = str_replace('..', '', $subPath); |
168
|
13 |
|
$subPath = $subPath . '/'; |
169
|
|
|
|
170
|
13 |
|
$this->subPath = $subPath; |
171
|
13 |
|
$this->maxVersion = $this->elggExtract('version', $vars, ''); |
|
|
|
|
172
|
13 |
|
$this->includeDisabledPlugins = $this->elggExtract('include_disabled_plugins', $vars, false); |
|
|
|
|
173
|
13 |
|
$this->findDeprecatedFunctions = $this->elggExtract('find_deprecated_functions', $vars, true); |
|
|
|
|
174
|
13 |
|
$this->findPrivateFunctions = $this->elggExtract('find_private_functions', $vars, true); |
|
|
|
|
175
|
13 |
|
$this->fixProblems = $this->elggExtract('fix_problems', $vars, false); |
|
|
|
|
176
|
13 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
14 |
|
public function isFixProblemsEnabled() { |
182
|
14 |
|
return (bool)$this->getOption('fixProblems', false); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
14 |
|
public function getMaxVersion() { |
189
|
14 |
|
if ($this->maxVersion === '' || $this->maxVersion === null) { |
190
|
3 |
|
return call_user_func($this->versionGetter, true); |
191
|
|
|
} |
192
|
12 |
|
return $this->maxVersion; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
14 |
|
public function getSubPath() { |
199
|
14 |
|
return (string)$this->subPath; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
15 |
|
public function isIncludeDisabledPluginsEnabled() { |
206
|
15 |
|
return (bool)$this->getOption('includeDisabledPlugins', false); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
15 |
|
public function isSkipInactivePluginsEnabled() { |
213
|
15 |
|
return !$this->isIncludeDisabledPluginsEnabled(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
13 |
|
public function isDeprecatedFunctionsTestEnabled() { |
220
|
13 |
|
return (bool)$this->getOption('findDeprecatedFunctions', true); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
13 |
|
public function isPrivateFunctionsTestEnabled() { |
227
|
13 |
|
return (bool)$this->getOption('findPrivateFunctions', true); |
228
|
|
|
} |
229
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.