|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the LGPL. For more information please see |
|
18
|
|
|
* <http://phing.info>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Phing\Task\Ext\Analyzer\Phpmd; |
|
22
|
|
|
|
|
23
|
|
|
use Phing\Exception\BuildException; |
|
24
|
|
|
use Phing\Io\File; |
|
25
|
|
|
use Phing\Task; |
|
26
|
|
|
use Phing\Type\Element\FileSetAware; |
|
27
|
|
|
use Phing\Util\DataStore; |
|
28
|
|
|
use PHPMD\AbstractRule; |
|
29
|
|
|
use PHPMD\PHPMD; |
|
30
|
|
|
use PHPMD\Report; |
|
31
|
|
|
use PHPMD\RuleSetFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Runs PHP Mess Detector. Checking PHP files for several potential problems |
|
35
|
|
|
* based on rulesets. |
|
36
|
|
|
* |
|
37
|
|
|
* @package phing.tasks.ext.phpmd |
|
38
|
|
|
* @author Benjamin Schultz <[email protected]> |
|
39
|
|
|
* @since 2.4.1 |
|
40
|
|
|
*/ |
|
41
|
|
|
class PHPMDTask extends Task |
|
42
|
|
|
{ |
|
43
|
|
|
use FileSetAware; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* A php source code filename or directory |
|
47
|
|
|
* |
|
48
|
|
|
* @var File |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $file = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The rule-set filenames or identifier. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $rulesets = 'codesize,unusedcode'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The minimum priority for rules to load. |
|
61
|
|
|
* |
|
62
|
|
|
* @var integer |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $minimumPriority = 0; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* List of valid file extensions for analyzed files. |
|
68
|
|
|
* |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $allowedFileExtensions = ['php']; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* List of exclude directory patterns. |
|
75
|
|
|
* |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $ignorePatterns = ['.git', '.svn', 'CVS', '.bzr', '.hg']; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The format for the report |
|
82
|
|
|
* |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $format = 'text'; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Formatter elements. |
|
89
|
|
|
* |
|
90
|
|
|
* @var PHPMDFormatterElement[] |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $formatters = []; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected $pharLocation = ""; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Cache data storage |
|
101
|
|
|
* |
|
102
|
|
|
* @var DataStore |
|
103
|
|
|
*/ |
|
104
|
|
|
protected $cache; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set the input source file or directory. |
|
108
|
|
|
* |
|
109
|
|
|
* @param File $file The input source file or directory. |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setFile(File $file) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->file = $file; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets the minimum rule priority. |
|
118
|
|
|
* |
|
119
|
|
|
* @param integer $minimumPriority Minimum rule priority. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setMinimumPriority($minimumPriority) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->minimumPriority = $minimumPriority; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Sets the rule-sets. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $ruleSetFileNames Comma-separated string of rule-set filenames or identifier. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setRulesets($ruleSetFileNames) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->rulesets = $ruleSetFileNames; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Sets a list of filename extensions for valid php source code files. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $fileExtensions List of valid file extensions without leading dot. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setAllowedFileExtensions($fileExtensions) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->allowedFileExtensions = []; |
|
144
|
|
|
|
|
145
|
|
|
$token = ' ,;'; |
|
146
|
|
|
$ext = strtok($fileExtensions, $token); |
|
147
|
|
|
|
|
148
|
|
|
while ($ext !== false) { |
|
149
|
|
|
$this->allowedFileExtensions[] = $ext; |
|
150
|
|
|
$ext = strtok($token); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Sets a list of ignore patterns that is used to exclude directories from the source analysis. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $ignorePatterns List of ignore patterns. |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setIgnorePatterns($ignorePatterns) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->ignorePatterns = []; |
|
162
|
|
|
|
|
163
|
|
|
$token = ' ,;'; |
|
164
|
|
|
$pattern = strtok($ignorePatterns, $token); |
|
165
|
|
|
|
|
166
|
|
|
while ($pattern !== false) { |
|
167
|
|
|
$this->ignorePatterns[] = $pattern; |
|
168
|
|
|
$pattern = strtok($token); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Create object for nested formatter element. |
|
174
|
|
|
* |
|
175
|
|
|
* @return PHPMDFormatterElement |
|
176
|
|
|
*/ |
|
177
|
3 |
|
public function createFormatter() |
|
178
|
|
|
{ |
|
179
|
3 |
|
$num = array_push($this->formatters, new PHPMDFormatterElement()); |
|
180
|
|
|
|
|
181
|
3 |
|
return $this->formatters[$num - 1]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $format |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setFormat($format) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->format = $format; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $pharLocation |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setPharLocation($pharLocation) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->pharLocation = $pharLocation; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Whether to store last-modified times in cache |
|
202
|
|
|
* |
|
203
|
|
|
* @param File $file |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setCacheFile(File $file) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->cache = new DataStore($file); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Find PHPMD |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
* @throws BuildException |
|
215
|
|
|
*/ |
|
216
|
3 |
|
protected function loadDependencies() |
|
217
|
|
|
{ |
|
218
|
3 |
|
if (!empty($this->pharLocation)) { |
|
219
|
|
|
// nasty but necessary: reorder the autoloaders so the one in the PHAR gets priority |
|
220
|
|
|
$autoloadFunctions = spl_autoload_functions(); |
|
221
|
|
|
$composerAutoloader = null; |
|
222
|
|
|
if (get_class($autoloadFunctions[0][0]) === 'Composer\Autoload\ClassLoader') { |
|
223
|
|
|
$composerAutoloader = $autoloadFunctions[0]; |
|
224
|
|
|
spl_autoload_unregister($composerAutoloader); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$GLOBALS['_SERVER']['SCRIPT_NAME'] = '-'; |
|
228
|
|
|
ob_start(); |
|
229
|
|
|
include_once 'phar://' . $this->pharLocation . '/vendor/autoload.php'; |
|
230
|
|
|
ob_end_clean(); |
|
231
|
|
|
|
|
232
|
|
|
if ($composerAutoloader !== null) { |
|
233
|
|
|
spl_autoload_register($composerAutoloader); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
3 |
|
$className = '\PHPMD\PHPMD'; |
|
238
|
|
|
|
|
239
|
3 |
|
if (!class_exists($className)) { |
|
240
|
|
|
throw new BuildException( |
|
241
|
|
|
'PHPMDTask depends on PHPMD being installed and on include_path or listed in pharLocation.', |
|
242
|
|
|
$this->getLocation() |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
3 |
|
$minPriority = AbstractRule::LOWEST_PRIORITY; |
|
247
|
|
|
|
|
248
|
3 |
|
if (!$this->minimumPriority) { |
|
249
|
3 |
|
$this->minimumPriority = $minPriority; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
3 |
|
return $className; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Return the list of files to parse |
|
257
|
|
|
* |
|
258
|
|
|
* @return string[] list of absolute files to parse |
|
259
|
|
|
*/ |
|
260
|
3 |
|
protected function getFilesToParse() |
|
261
|
|
|
{ |
|
262
|
3 |
|
$filesToParse = []; |
|
263
|
|
|
|
|
264
|
3 |
|
if ($this->file instanceof File) { |
|
|
|
|
|
|
265
|
|
|
$filesToParse[] = $this->file->getPath(); |
|
266
|
|
|
} else { |
|
267
|
|
|
// append any files in filesets |
|
268
|
3 |
|
foreach ($this->filesets as $fs) { |
|
269
|
3 |
|
$dir = $fs->getDir($this->project)->getAbsolutePath(); |
|
270
|
3 |
|
foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) { |
|
271
|
3 |
|
$fileAbsolutePath = $dir . DIRECTORY_SEPARATOR . $filename; |
|
272
|
3 |
|
if ($this->cache) { |
|
273
|
|
|
$lastMTime = $this->cache->get($fileAbsolutePath); |
|
274
|
|
|
$currentMTime = filemtime($fileAbsolutePath); |
|
275
|
|
|
if ($lastMTime >= $currentMTime) { |
|
276
|
|
|
continue; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$this->cache->put($fileAbsolutePath, $currentMTime); |
|
280
|
|
|
} |
|
281
|
3 |
|
$filesToParse[] = $fileAbsolutePath; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
3 |
|
return $filesToParse; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Executes PHPMD against PhingFile or a FileSet |
|
290
|
|
|
* |
|
291
|
|
|
* @throws BuildException - if the phpmd classes can't be loaded. |
|
292
|
|
|
*/ |
|
293
|
3 |
|
public function main() |
|
294
|
|
|
{ |
|
295
|
3 |
|
$className = $this->loadDependencies(); |
|
296
|
|
|
|
|
297
|
3 |
|
if (!isset($this->file) and count($this->filesets) == 0) { |
|
298
|
|
|
throw new BuildException('Missing either a nested fileset or attribute "file" set'); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
3 |
|
if (count($this->formatters) == 0) { |
|
302
|
|
|
// turn legacy format attribute into formatter |
|
303
|
|
|
$fmt = new PHPMDFormatterElement(); |
|
304
|
|
|
$fmt->setType($this->format); |
|
305
|
|
|
$fmt->setUseFile(false); |
|
306
|
|
|
|
|
307
|
|
|
$this->formatters[] = $fmt; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
3 |
|
$reportRenderers = []; |
|
311
|
|
|
|
|
312
|
3 |
|
foreach ($this->formatters as $fe) { |
|
313
|
3 |
|
if ($fe->getType() == '') { |
|
314
|
|
|
throw new BuildException('Formatter missing required "type" attribute.'); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
3 |
|
if ($fe->getUsefile() && $fe->getOutfile() === null) { |
|
318
|
|
|
throw new BuildException('Formatter requires "outfile" attribute when "useFile" is true.'); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
3 |
|
$reportRenderers[] = $fe->getRenderer(); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
3 |
|
if ($this->cache) { |
|
325
|
|
|
$reportRenderers[] = new PHPMDRendererRemoveFromCache($this->cache); |
|
326
|
|
|
} else { |
|
327
|
3 |
|
$this->cache = null; // cache not compatible to old version |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
// Create a rule set factory |
|
331
|
3 |
|
$ruleSetFactory = new RuleSetFactory(); |
|
332
|
3 |
|
$ruleSetFactory->setMinimumPriority($this->minimumPriority); |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @var PHPMD $phpmd |
|
336
|
|
|
*/ |
|
337
|
3 |
|
$phpmd = new $className(); |
|
338
|
3 |
|
$phpmd->setFileExtensions($this->allowedFileExtensions); |
|
339
|
3 |
|
$phpmd->addIgnorePatterns($this->ignorePatterns); |
|
340
|
|
|
|
|
341
|
3 |
|
$filesToParse = $this->getFilesToParse(); |
|
342
|
|
|
|
|
343
|
3 |
|
if (count($filesToParse) > 0) { |
|
344
|
3 |
|
$inputPath = implode(',', $filesToParse); |
|
345
|
|
|
|
|
346
|
3 |
|
$this->log('Processing files...'); |
|
347
|
|
|
|
|
348
|
3 |
|
$report = new Report(); |
|
349
|
3 |
|
$phpmd->processFiles($inputPath, $this->rulesets, $reportRenderers, $ruleSetFactory, $report); |
|
350
|
|
|
|
|
351
|
3 |
|
if ($this->cache) { |
|
352
|
|
|
$this->cache->commit(); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
3 |
|
$this->log('Finished processing files'); |
|
356
|
|
|
} else { |
|
357
|
|
|
$this->log('No files to process'); |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|