|
1
|
|
|
<?php |
|
2
|
|
|
namespace CodeReview; |
|
3
|
|
|
|
|
4
|
|
|
class Analyzer { |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @var \CodeReview\Config |
|
8
|
|
|
*/ |
|
9
|
|
|
protected $options; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Function names seen as called |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $calledFunctions = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $stats; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var integer |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $filesAnalyzed; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $maxVersion; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Array of basic function names replacements |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $instantReplacements; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fixProblems; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \CodeReview\Config $options |
|
47
|
|
|
*/ |
|
48
|
9 |
|
public function __construct(\CodeReview\Config $options = null) { |
|
49
|
|
|
|
|
50
|
9 |
|
if ($options === null) { |
|
51
|
|
|
$options = new \CodeReview\Config(); |
|
52
|
|
|
} |
|
53
|
9 |
|
$this->options = $options; |
|
54
|
|
|
|
|
55
|
9 |
|
$this->maxVersion = $options->getMaxVersion(); |
|
56
|
9 |
|
$this->fixProblems = $options->isFixProblemsEnabled(); |
|
57
|
9 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $subPath |
|
61
|
|
|
* @throws \CodeReview\IOException |
|
62
|
|
|
* @return \CodeReview\FileFilterIterator |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function getPhpFilesIterator($subPath = 'engine/') { |
|
65
|
9 |
|
$config = \code_review::getConfig(); |
|
66
|
9 |
|
$path = $config['path'] . $subPath; |
|
67
|
9 |
|
if (!file_exists($path)) { |
|
68
|
1 |
|
throw new \CodeReview\IOException("Invalid subPath specified. $path does not exists!"); |
|
69
|
|
|
} |
|
70
|
8 |
|
$i = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS); |
|
71
|
8 |
|
$i = new \RecursiveIteratorIterator($i, \RecursiveIteratorIterator::LEAVES_ONLY); |
|
72
|
8 |
|
$i = new \RegexIterator($i, "/.*\.php/"); |
|
73
|
8 |
|
$i = new \CodeReview\FileFilterIterator($i, $config['path'], $this->options); |
|
74
|
8 |
|
return $i; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
9 |
|
public function analyze() { |
|
81
|
|
|
|
|
82
|
9 |
|
$options = $this->options; |
|
83
|
|
|
|
|
84
|
9 |
|
$i = $this->getPhpFilesIterator($options->getSubPath()); |
|
85
|
|
|
|
|
86
|
8 |
|
$fixer = new CodeFixer(); |
|
87
|
8 |
|
$this->instantReplacements = $fixer->getBasicFunctionRenames($this->maxVersion); |
|
88
|
|
|
|
|
89
|
8 |
|
$this->stats = array(); |
|
90
|
8 |
|
$this->filesAnalyzed = 0; |
|
91
|
|
|
|
|
92
|
8 |
|
$functions = array(); |
|
93
|
8 |
|
if ($options->isDeprecatedFunctionsTestEnabled()) { |
|
94
|
8 |
|
$functions = array_merge($functions, \code_review::getDeprecatedFunctionsList($options->getMaxVersion())); |
|
95
|
8 |
|
} |
|
96
|
8 |
|
if ($options->isPrivateFunctionsTestEnabled()) { |
|
97
|
5 |
|
$functions = array_merge($functions, \code_review::getPrivateFunctionsList()); |
|
98
|
5 |
|
} |
|
99
|
|
|
|
|
100
|
8 |
|
foreach ($i as $filePath => $file) { |
|
101
|
7 |
|
if ($file instanceof \SplFileInfo) { |
|
102
|
7 |
|
$result = $this->processFile($filePath, $functions); |
|
103
|
7 |
|
$this->filesAnalyzed++; |
|
104
|
7 |
|
if (!empty($result['problems'])) { |
|
105
|
7 |
|
$this->stats[$filePath] = $result; |
|
106
|
7 |
|
} |
|
107
|
7 |
|
} |
|
108
|
8 |
|
} |
|
109
|
8 |
|
return $this->stats; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
8 |
|
private function outputReportHeader() { |
|
116
|
|
|
|
|
117
|
8 |
|
$options = $this->options; |
|
118
|
|
|
|
|
119
|
8 |
|
$result = ''; |
|
120
|
|
|
|
|
121
|
8 |
|
$result .= "Subpath selected <strong>" . $options->getSubPath() . "</strong>\n"; |
|
122
|
8 |
|
$result .= "Max version: " . $options->getMaxVersion() . "\n"; |
|
123
|
8 |
|
$result .= "Skipped inactive plugins: " . ($options->isSkipInactivePluginsEnabled() ? 'yes' : 'no') . "\n"; |
|
124
|
8 |
|
$result .= "Search for deprecated functions usage: " . ($options->isDeprecatedFunctionsTestEnabled() ? 'yes' : 'no') . "\n"; |
|
125
|
8 |
|
$result .= "Search for private functions usage: " . ($options->isPrivateFunctionsTestEnabled() ? 'yes' : 'no') . "\n"; |
|
126
|
8 |
|
$result .= "Attempt to fix problems: " . ($options->isFixProblemsEnabled() ? 'yes' : 'no') . "\n"; |
|
127
|
|
|
|
|
128
|
8 |
|
foreach (array('problems', 'fixes') as $type) { |
|
129
|
8 |
|
$total = 0; |
|
130
|
8 |
|
foreach ($this->stats as $items) { |
|
131
|
7 |
|
$total += count($items[$type]); |
|
132
|
8 |
|
} |
|
133
|
8 |
|
$result .= "Found $total $type in " . count($this->stats) . " files\n"; |
|
134
|
8 |
|
} |
|
135
|
|
|
|
|
136
|
8 |
|
if ($this->filesAnalyzed === 0) { |
|
137
|
1 |
|
$result .= "*** No files were processed! *** Analysis input parameters did not resolve to any files.\n"; |
|
138
|
1 |
|
} else { |
|
139
|
7 |
|
$result .= "Processed " . $this->filesAnalyzed . " files total\n"; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
8 |
|
return $result; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
7 |
|
private function ouptutUnusedFunctionsReport() { |
|
|
|
|
|
|
149
|
|
|
//prepare unused functions report |
|
150
|
|
|
$functions = get_defined_functions(); |
|
151
|
|
|
$functions = array_filter($functions['user'], 'strtolower'); |
|
152
|
|
|
$calledFunctions = array_filter($this->calledFunctions, 'strtolower'); |
|
153
|
|
|
$deprecatedFunctions = array_filter(array_keys(\code_review::getDeprecatedFunctionsList($this->maxVersion)), 'strtolower'); |
|
154
|
|
|
$functions = array_diff($functions, $calledFunctions, $deprecatedFunctions); |
|
155
|
|
|
|
|
156
|
|
|
foreach ($functions as $key => $function) { |
|
157
|
|
|
if (function_exists($function)) { |
|
158
|
|
|
$reflectionFunction = new \ReflectionFunction($function); |
|
159
|
|
|
if (!$reflectionFunction->isInternal()) { |
|
160
|
|
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
unset($reflectionFunction); |
|
163
|
|
|
} |
|
164
|
|
|
unset($functions[$key]); |
|
165
|
|
|
} |
|
166
|
|
|
sort($functions); |
|
167
|
|
|
|
|
168
|
|
|
//unused functions report |
|
169
|
|
|
$result = "Not called but defined functions:\n"; |
|
170
|
|
|
$baseLenght = strlen(elgg_get_root_path()); |
|
171
|
|
|
foreach (array_values($functions) as $functionName) { |
|
172
|
|
|
$reflectionFunction = new \ReflectionFunction($functionName); |
|
173
|
|
|
$path = substr($reflectionFunction->getFileName(), $baseLenght); |
|
174
|
|
|
if (strpos($path, 'engine') !== 0) { |
|
175
|
7 |
|
continue; |
|
176
|
|
|
} |
|
177
|
|
|
$result .= "$functionName \t{$path}:{$reflectionFunction->getStartLine()}\n"; |
|
178
|
7 |
|
} |
|
179
|
|
|
return $result; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
9 |
|
public function outputReport() { |
|
186
|
|
|
|
|
187
|
8 |
|
$result = $this->outputReportHeader(); |
|
188
|
|
|
|
|
189
|
|
|
/* |
|
190
|
|
|
* Full report |
|
191
|
|
|
*/ |
|
192
|
9 |
|
foreach ($this->stats as $filePath => $items) { |
|
193
|
7 |
|
$result .= "\nIn file: " . $filePath . "\n"; |
|
194
|
|
|
|
|
195
|
|
|
//problems |
|
196
|
7 |
|
foreach ($items['problems'] as $row) { |
|
197
|
7 |
|
list($data, , $line) = $row; |
|
198
|
7 |
|
$result .= " Line $line:\t" . (string)$data . "\n"; |
|
199
|
9 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
//fixes |
|
202
|
7 |
|
foreach ($items['fixes'] as $row) { |
|
203
|
|
|
list($before, $after, $line) = $row; |
|
204
|
|
|
$result .= " Line $line:\tReplacing: '$before' with '$after'\n"; |
|
205
|
7 |
|
} |
|
206
|
8 |
|
} |
|
207
|
|
|
|
|
208
|
8 |
|
$result .= "\n"; |
|
209
|
|
|
|
|
210
|
|
|
// $result .= $this->ouptutUnusedFunctionsReport(); |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
8 |
|
$result .= "\n"; |
|
213
|
|
|
|
|
214
|
8 |
|
return $result; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Find function calls and extract |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $filePath |
|
221
|
|
|
* @param array $functions |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
8 |
|
public function processFile($filePath, $functions) { |
|
225
|
|
|
$result = array( |
|
226
|
7 |
|
'problems' => array(), |
|
227
|
7 |
|
'fixes' => array(), |
|
228
|
7 |
|
); |
|
229
|
7 |
|
$phpTokens = new PhpFileParser($filePath); |
|
230
|
7 |
|
$changes = 0; |
|
231
|
7 |
|
foreach ($phpTokens as $key => $row) { |
|
232
|
|
|
// get non trivial tokens |
|
233
|
7 |
|
if (is_array($row)) { |
|
234
|
7 |
|
list($token, $functionName, $lineNumber) = $row; |
|
235
|
7 |
|
$originalFunctionName = $functionName; |
|
236
|
|
|
|
|
237
|
|
|
// prepare normalized version of function name for matching |
|
238
|
7 |
|
$functionName = strtolower($functionName); |
|
239
|
|
|
|
|
240
|
|
|
// check for function call |
|
241
|
|
|
if ($token == T_STRING |
|
242
|
7 |
|
&& !$phpTokens->isEqualToToken(T_OBJECT_OPERATOR, $key-1) //not method |
|
243
|
7 |
|
&& !$phpTokens->isEqualToToken(T_DOUBLE_COLON, $key-1) //not static method |
|
244
|
7 |
|
&& !$phpTokens->isEqualToToken(T_FUNCTION, $key-2) //not definition |
|
245
|
7 |
|
) { |
|
246
|
|
|
// mark function as called |
|
247
|
8 |
|
if (function_exists($functionName) && !in_array($functionName, $this->calledFunctions)) { |
|
248
|
7 |
|
$this->calledFunctions[] = $functionName; |
|
249
|
7 |
|
} |
|
250
|
|
|
// is it function we're looking for |
|
251
|
7 |
|
if (isset($functions[$functionName])) { |
|
252
|
7 |
|
$definingFunctionName = $phpTokens->getDefiningFunctionName($key); |
|
253
|
|
|
|
|
254
|
|
|
//we're skipping deprecated calls that are in deprecated function itself |
|
255
|
7 |
|
if (!$definingFunctionName || !isset($functions[strtolower($definingFunctionName)])) { |
|
256
|
7 |
|
$result['problems'][] = array($functions[$functionName], $originalFunctionName, $lineNumber); |
|
257
|
7 |
|
} |
|
258
|
|
|
|
|
259
|
|
|
//do instant replacement |
|
260
|
8 |
|
if ($this->fixProblems && isset($this->instantReplacements[$functionName])) { |
|
261
|
|
|
$phpTokens[$key] = array(T_STRING, $this->instantReplacements[$functionName]); |
|
262
|
|
|
$result['fixes'][] = array($originalFunctionName, $this->instantReplacements[$functionName], $lineNumber); |
|
263
|
|
|
$changes++; |
|
264
|
|
|
} |
|
265
|
7 |
|
} |
|
266
|
7 |
|
} |
|
267
|
7 |
|
} |
|
268
|
7 |
|
} |
|
269
|
7 |
|
if ($changes) { |
|
270
|
|
|
try { |
|
271
|
|
|
$phpTokens->exportPhp($filePath); |
|
272
|
|
|
} catch (\CodeReview\IOException $e) { |
|
273
|
|
|
echo '*** Error: ' . $e->getMessage() . " ***\n"; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
7 |
|
unset($phpTokens); |
|
277
|
7 |
|
return $result; |
|
278
|
|
|
} |
|
279
|
|
|
} |