|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of PHP Mess Detector. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
7
|
|
|
* All rights reserved. |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under BSD License |
|
10
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
11
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Manuel Pichler <[email protected]> |
|
14
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
15
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
16
|
|
|
* @link http://phpmd.org/ |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace PHPMD\TextUI; |
|
20
|
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
|
22
|
|
|
use PHPMD\Baseline\BaselineMode; |
|
23
|
|
|
use PHPMD\Cache\Model\ResultCacheStrategy; |
|
24
|
|
|
use PHPMD\Console\OutputInterface; |
|
25
|
|
|
use PHPMD\Renderer\Option\Color; |
|
26
|
|
|
use PHPMD\Renderer\Option\Verbose; |
|
27
|
|
|
use PHPMD\Renderer\RendererFactory; |
|
28
|
|
|
use PHPMD\Renderer\RendererInterface; |
|
29
|
|
|
use PHPMD\Rule; |
|
30
|
|
|
use PHPMD\Utility\ArgumentsValidator; |
|
31
|
|
|
use TypeError; |
|
32
|
|
|
use ValueError; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This is a helper class that collects the specified cli arguments and puts them |
|
36
|
|
|
* into accessible properties. |
|
37
|
|
|
* |
|
38
|
|
|
* @SuppressWarnings(LongVariable) |
|
39
|
|
|
*/ |
|
40
|
|
|
class CommandLineOptions |
|
41
|
|
|
{ |
|
42
|
|
|
/** Error code for invalid input */ |
|
43
|
|
|
private const INPUT_ERROR = 23; |
|
44
|
|
|
|
|
45
|
|
|
/** The minimum rule priority. */ |
|
46
|
|
|
private int $minimumPriority = Rule::LOWEST_PRIORITY; |
|
47
|
|
|
|
|
48
|
|
|
/** The maximum rule priority. */ |
|
49
|
|
|
private int $maximumPriority = Rule::HIGHEST_PRIORITY; |
|
50
|
|
|
|
|
51
|
|
|
/** A php source code filename or directory. */ |
|
52
|
|
|
private string $inputPath; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The specified report format. |
|
56
|
|
|
* |
|
57
|
|
|
* @var ?string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $reportFormat; |
|
60
|
|
|
|
|
61
|
|
|
/** An optional filename for the generated report. */ |
|
62
|
|
|
private ?string $reportFile = null; |
|
63
|
|
|
|
|
64
|
|
|
/** An optional script to load before running analysis */ |
|
65
|
|
|
private ?string $bootstrap = null; |
|
66
|
|
|
|
|
67
|
|
|
/** An optional filename to collect errors. */ |
|
68
|
|
|
private ?string $errorFile = null; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Additional report files. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array<string, string> |
|
74
|
|
|
*/ |
|
75
|
|
|
private array $reportFiles = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* List of deprecations. |
|
79
|
|
|
* |
|
80
|
|
|
* @var list<string> |
|
|
|
|
|
|
81
|
|
|
*/ |
|
82
|
|
|
private array $deprecations = []; |
|
83
|
|
|
|
|
84
|
|
|
/** A ruleset filename or a comma-separated string of ruleset filenames. */ |
|
85
|
|
|
private string $ruleSets; |
|
86
|
|
|
|
|
87
|
|
|
/** File name of a PHPUnit code coverage report. */ |
|
88
|
|
|
private ?string $coverageReport = null; |
|
89
|
|
|
|
|
90
|
|
|
/** A string of comma-separated extensions for valid php source code filenames. */ |
|
91
|
|
|
private ?string $extensions = null; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* A string of comma-separated pattern that is used to exclude directories. |
|
95
|
|
|
* |
|
96
|
|
|
* Use asterisks to exclude by pattern. For example *src/foo/*.php or *src/foo/* |
|
97
|
|
|
*/ |
|
98
|
|
|
private ?string $ignore = null; |
|
99
|
|
|
|
|
100
|
|
|
/** Should the shell show the current phpmd version? */ |
|
101
|
|
|
private bool $version = false; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Should PHPMD run in strict mode? |
|
105
|
|
|
* |
|
106
|
|
|
* @since 1.2.0 |
|
107
|
|
|
*/ |
|
108
|
|
|
private bool $strict = false; |
|
109
|
|
|
|
|
110
|
|
|
private int $verbosity = OutputInterface::VERBOSITY_NORMAL; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Should PHPMD exit without error code even if error is found? |
|
114
|
|
|
* |
|
115
|
|
|
* @since 2.10.0 |
|
116
|
|
|
*/ |
|
117
|
|
|
private bool $ignoreErrorsOnExit = false; |
|
118
|
|
|
|
|
119
|
|
|
/** Should PHPMD exit without error code even if violation is found? */ |
|
120
|
|
|
private bool $ignoreViolationsOnExit = false; |
|
121
|
|
|
|
|
122
|
|
|
/** Should PHPMD baseline the existing violations and write them to the $baselineFile */ |
|
123
|
|
|
private BaselineMode $generateBaseline = BaselineMode::None; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* The baseline source file to read the baseline violations from. |
|
127
|
|
|
* Defaults to the path of the (first) ruleset file as phpmd.baseline.xml |
|
128
|
|
|
*/ |
|
129
|
|
|
private ?string $baselineFile = null; |
|
130
|
|
|
|
|
131
|
|
|
/** Should PHPMD read or write the result cache state from the cache file */ |
|
132
|
|
|
private bool $cacheEnabled = false; |
|
133
|
|
|
|
|
134
|
|
|
/** If set the path to read and write the result cache state from and to. */ |
|
135
|
|
|
private ?string $cacheFile; |
|
136
|
|
|
|
|
137
|
|
|
/** Determine the cache strategy. */ |
|
138
|
|
|
private ResultCacheStrategy $cacheStrategy = ResultCacheStrategy::Content; |
|
139
|
|
|
|
|
140
|
|
|
/** Either the output should be colored. */ |
|
141
|
|
|
private bool $colored = false; |
|
142
|
|
|
|
|
143
|
|
|
/** Specify how many extra lines are added to a code snippet */ |
|
144
|
|
|
private ?int $extraLineInExcerpt = null; |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Constructs a new command line options instance. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string[] $args |
|
150
|
|
|
* @param array<int, string> $availableRuleSets List of available rule-sets. |
|
151
|
|
|
* @throws InvalidArgumentException |
|
152
|
|
|
* @throws ValueError |
|
153
|
|
|
* @throws TypeError |
|
154
|
|
|
*/ |
|
155
|
74 |
|
public function __construct( |
|
156
|
|
|
array $args, |
|
157
|
|
|
private readonly array $availableRuleSets = [], |
|
158
|
|
|
) { |
|
159
|
|
|
// Remove current file name |
|
160
|
74 |
|
array_shift($args); |
|
161
|
|
|
|
|
162
|
74 |
|
$originalArguments = $args; |
|
163
|
|
|
|
|
164
|
74 |
|
$arguments = []; |
|
165
|
74 |
|
$listenOptions = true; |
|
166
|
74 |
|
$hasImplicitArguments = false; |
|
167
|
|
|
|
|
168
|
74 |
|
while (($arg = array_shift($args)) !== null) { |
|
169
|
74 |
|
if (!$listenOptions) { |
|
170
|
2 |
|
$arguments[] = $arg; |
|
171
|
|
|
|
|
172
|
2 |
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
74 |
|
$equalChunk = explode('=', $arg, 2); |
|
176
|
|
|
|
|
177
|
74 |
|
switch ($equalChunk[0]) { |
|
178
|
74 |
|
case '--': |
|
179
|
2 |
|
$this->refuseValue($equalChunk); |
|
180
|
2 |
|
$listenOptions = false; |
|
181
|
|
|
|
|
182
|
2 |
|
break; |
|
183
|
|
|
|
|
184
|
73 |
|
case '--verbose': |
|
185
|
73 |
|
case '-v': |
|
186
|
1 |
|
$this->refuseValue($equalChunk); |
|
187
|
1 |
|
$this->verbosity = OutputInterface::VERBOSITY_VERBOSE; |
|
188
|
|
|
|
|
189
|
1 |
|
break; |
|
190
|
|
|
|
|
191
|
73 |
|
case '-vv': |
|
192
|
1 |
|
$this->refuseValue($equalChunk); |
|
193
|
1 |
|
$this->verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE; |
|
194
|
|
|
|
|
195
|
1 |
|
break; |
|
196
|
|
|
|
|
197
|
73 |
|
case '-vvv': |
|
198
|
2 |
|
$this->refuseValue($equalChunk); |
|
199
|
2 |
|
$this->verbosity = OutputInterface::VERBOSITY_DEBUG; |
|
200
|
|
|
|
|
201
|
2 |
|
break; |
|
202
|
|
|
|
|
203
|
73 |
|
case '--min-priority': |
|
204
|
73 |
|
case '--minimum-priority': |
|
205
|
73 |
|
case '--minimumpriority': |
|
206
|
1 |
|
$this->minimumPriority = (int) $this->readValue($equalChunk, $args); |
|
207
|
|
|
|
|
208
|
1 |
|
break; |
|
209
|
|
|
|
|
210
|
73 |
|
case '--max-priority': |
|
211
|
73 |
|
case '--maximum-priority': |
|
212
|
73 |
|
case '--maximumpriority': |
|
213
|
1 |
|
$this->maximumPriority = (int) $this->readValue($equalChunk, $args); |
|
214
|
|
|
|
|
215
|
1 |
|
break; |
|
216
|
|
|
|
|
217
|
73 |
|
case '--report-file': |
|
218
|
73 |
|
case '--reportfile': |
|
219
|
4 |
|
$this->reportFile = (string) $this->readValue($equalChunk, $args); |
|
220
|
|
|
|
|
221
|
4 |
|
break; |
|
222
|
|
|
|
|
223
|
73 |
|
case '--bootstrap': |
|
224
|
|
|
$this->bootstrap = (string) $this->readValue($equalChunk, $args); |
|
225
|
|
|
|
|
226
|
|
|
break; |
|
227
|
|
|
|
|
228
|
73 |
|
case '--error-file': |
|
229
|
73 |
|
case '--errorfile': |
|
230
|
1 |
|
$this->errorFile = (string) $this->readValue($equalChunk, $args); |
|
231
|
|
|
|
|
232
|
1 |
|
break; |
|
233
|
|
|
|
|
234
|
73 |
|
case '--input-file': |
|
235
|
73 |
|
case '--inputfile': |
|
236
|
6 |
|
array_unshift($arguments, $this->readInputFile((string) $this->readValue($equalChunk, $args))); |
|
237
|
|
|
|
|
238
|
5 |
|
break; |
|
239
|
|
|
|
|
240
|
73 |
|
case '--coverage': |
|
241
|
1 |
|
$this->coverageReport = (string) $this->readValue($equalChunk, $args); |
|
242
|
|
|
|
|
243
|
1 |
|
break; |
|
244
|
|
|
|
|
245
|
73 |
|
case '--extensions': |
|
246
|
1 |
|
$this->logDeprecated('extensions', 'suffixes'); |
|
247
|
|
|
// Deprecated: We use the suffixes option now |
|
248
|
1 |
|
$this->extensions = (string) $this->readValue($equalChunk, $args); |
|
249
|
|
|
|
|
250
|
1 |
|
break; |
|
251
|
|
|
|
|
252
|
73 |
|
case '--suffixes': |
|
253
|
1 |
|
$this->extensions = (string) $this->readValue($equalChunk, $args); |
|
254
|
|
|
|
|
255
|
1 |
|
break; |
|
256
|
|
|
|
|
257
|
73 |
|
case '--ignore': |
|
258
|
2 |
|
$this->logDeprecated('ignore', 'exclude'); |
|
259
|
|
|
// Deprecated: We use the exclude option now |
|
260
|
2 |
|
$this->ignore = (string) $this->readValue($equalChunk, $args); |
|
261
|
|
|
|
|
262
|
2 |
|
break; |
|
263
|
|
|
|
|
264
|
73 |
|
case '--exclude': |
|
265
|
3 |
|
$this->ignore = (string) $this->readValue($equalChunk, $args); |
|
266
|
|
|
|
|
267
|
3 |
|
break; |
|
268
|
|
|
|
|
269
|
73 |
|
case '--color': |
|
270
|
2 |
|
$this->refuseValue($equalChunk); |
|
271
|
1 |
|
$this->colored = true; |
|
272
|
|
|
|
|
273
|
1 |
|
break; |
|
274
|
|
|
|
|
275
|
72 |
|
case '--version': |
|
276
|
1 |
|
$this->refuseValue($equalChunk); |
|
277
|
1 |
|
$this->version = true; |
|
278
|
|
|
|
|
279
|
1 |
|
return; |
|
280
|
|
|
|
|
281
|
71 |
|
case '--strict': |
|
282
|
1 |
|
$this->refuseValue($equalChunk); |
|
283
|
1 |
|
$this->strict = true; |
|
284
|
|
|
|
|
285
|
1 |
|
break; |
|
286
|
|
|
|
|
287
|
71 |
|
case '--not-strict': |
|
288
|
1 |
|
$this->refuseValue($equalChunk); |
|
289
|
1 |
|
$this->strict = false; |
|
290
|
|
|
|
|
291
|
1 |
|
break; |
|
292
|
|
|
|
|
293
|
71 |
|
case '--generate-baseline': |
|
294
|
1 |
|
$this->refuseValue($equalChunk); |
|
295
|
1 |
|
$this->generateBaseline = BaselineMode::Generate; |
|
296
|
|
|
|
|
297
|
1 |
|
break; |
|
298
|
|
|
|
|
299
|
71 |
|
case '--update-baseline': |
|
300
|
1 |
|
$this->refuseValue($equalChunk); |
|
301
|
1 |
|
$this->generateBaseline = BaselineMode::Update; |
|
302
|
|
|
|
|
303
|
1 |
|
break; |
|
304
|
|
|
|
|
305
|
71 |
|
case '--baseline-file': |
|
306
|
1 |
|
$this->baselineFile = $this->readValue($equalChunk, $args); |
|
307
|
|
|
|
|
308
|
1 |
|
break; |
|
309
|
|
|
|
|
310
|
71 |
|
case '--cache': |
|
311
|
1 |
|
$this->refuseValue($equalChunk); |
|
312
|
1 |
|
$this->cacheEnabled = true; |
|
313
|
|
|
|
|
314
|
1 |
|
break; |
|
315
|
|
|
|
|
316
|
71 |
|
case '--cache-file': |
|
317
|
1 |
|
$this->cacheFile = $this->readValue($equalChunk, $args); |
|
318
|
|
|
|
|
319
|
1 |
|
break; |
|
320
|
|
|
|
|
321
|
71 |
|
case '--cache-strategy': |
|
322
|
1 |
|
$strategy = (string) $this->readValue($equalChunk, $args); |
|
323
|
1 |
|
$this->cacheStrategy = ResultCacheStrategy::from($strategy); |
|
324
|
|
|
|
|
325
|
1 |
|
break; |
|
326
|
|
|
|
|
327
|
71 |
|
case '--ignore-errors-on-exit': |
|
328
|
1 |
|
$this->refuseValue($equalChunk); |
|
329
|
1 |
|
$this->ignoreErrorsOnExit = true; |
|
330
|
|
|
|
|
331
|
1 |
|
break; |
|
332
|
|
|
|
|
333
|
71 |
|
case '--ignore-violations-on-exit': |
|
334
|
1 |
|
$this->refuseValue($equalChunk); |
|
335
|
1 |
|
$this->ignoreViolationsOnExit = true; |
|
336
|
|
|
|
|
337
|
1 |
|
break; |
|
338
|
|
|
|
|
339
|
71 |
|
case '--reportfile-checkstyle': |
|
340
|
71 |
|
case '--reportfile-github': |
|
341
|
71 |
|
case '--reportfile-gitlab': |
|
342
|
71 |
|
case '--reportfile-html': |
|
343
|
71 |
|
case '--reportfile-json': |
|
344
|
71 |
|
case '--reportfile-sarif': |
|
345
|
71 |
|
case '--reportfile-text': |
|
346
|
71 |
|
case '--reportfile-xml': |
|
347
|
6 |
|
preg_match('(^\-\-reportfile\-(checkstyle|github|gitlab|html|json|sarif|text|xml)$)', $arg, $match); |
|
348
|
6 |
|
if (isset($match[1])) { |
|
349
|
6 |
|
$this->reportFiles[$match[1]] = (string) $this->readValue($equalChunk, $args); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
6 |
|
break; |
|
353
|
|
|
|
|
354
|
71 |
|
case '--extra-line-in-excerpt': |
|
355
|
1 |
|
$this->extraLineInExcerpt = (int) $this->readValue($equalChunk, $args); |
|
356
|
|
|
|
|
357
|
1 |
|
break; |
|
358
|
|
|
|
|
359
|
|
|
default: |
|
360
|
71 |
|
$hasImplicitArguments = true; |
|
361
|
71 |
|
$arguments[] = $arg; |
|
362
|
|
|
|
|
363
|
71 |
|
break; |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
71 |
|
if (count($arguments) < 3) { |
|
368
|
1 |
|
throw new InvalidArgumentException($this->usage(), self::INPUT_ERROR); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
70 |
|
$validator = new ArgumentsValidator($hasImplicitArguments, $originalArguments, $arguments); |
|
372
|
|
|
|
|
373
|
70 |
|
$this->ruleSets = (string) array_pop($arguments); |
|
374
|
70 |
|
$validator->validate('ruleset', $this->ruleSets); |
|
375
|
|
|
|
|
376
|
69 |
|
$this->reportFormat = array_pop($arguments); |
|
377
|
69 |
|
$validator->validate('report format', $this->reportFormat ?? ''); |
|
378
|
|
|
|
|
379
|
69 |
|
$this->inputPath = implode(',', $arguments); |
|
380
|
|
|
|
|
381
|
69 |
|
if ($this->inputPath === '-') { |
|
382
|
2 |
|
$this->inputPath = 'php://stdin'; |
|
383
|
|
|
|
|
384
|
2 |
|
return; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
67 |
|
foreach ($arguments as $arg) { |
|
388
|
67 |
|
$validator->validate('input path', $arg); |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Returns a php source code filename or directory. |
|
394
|
|
|
*/ |
|
395
|
9 |
|
public function getInputPath(): string |
|
396
|
|
|
{ |
|
397
|
9 |
|
return $this->inputPath; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Returns the specified report format. |
|
402
|
|
|
* |
|
403
|
|
|
* @return ?string |
|
404
|
|
|
*/ |
|
405
|
3 |
|
public function getReportFormat() |
|
406
|
|
|
{ |
|
407
|
3 |
|
return $this->reportFormat; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Returns the output filename for a generated report or <b>null</b> when |
|
412
|
|
|
* the report should be displayed in STDOUT. |
|
413
|
|
|
*/ |
|
414
|
4 |
|
public function getReportFile(): ?string |
|
415
|
|
|
{ |
|
416
|
4 |
|
return $this->reportFile; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* Returns the current bootstrap file (if available) to load extra resources before analysis. |
|
421
|
|
|
*/ |
|
422
|
4 |
|
public function getBootstrapFile(): ?string |
|
423
|
|
|
{ |
|
424
|
4 |
|
return $this->bootstrap; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* Returns the output filename for the errors or <b>null</b> when |
|
429
|
|
|
* the report should be displayed in STDERR. |
|
430
|
|
|
*/ |
|
431
|
5 |
|
public function getErrorFile(): ?string |
|
432
|
|
|
{ |
|
433
|
5 |
|
return $this->errorFile; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Return the list of deprecations raised when parsing options. |
|
438
|
|
|
* |
|
439
|
|
|
* @return list<string> |
|
440
|
|
|
*/ |
|
441
|
7 |
|
public function getDeprecations(): array |
|
442
|
|
|
{ |
|
443
|
7 |
|
return $this->deprecations; |
|
|
|
|
|
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Returns a hash with report files specified for different renderers. The |
|
448
|
|
|
* key represents the report format and the value the report file location. |
|
449
|
|
|
* |
|
450
|
|
|
* @return array<string, string> |
|
451
|
|
|
*/ |
|
452
|
10 |
|
public function getReportFiles(): array |
|
453
|
|
|
{ |
|
454
|
10 |
|
return $this->reportFiles; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
/** |
|
458
|
|
|
* Returns a ruleset filename or a comma-separated string of ruleset |
|
459
|
|
|
*/ |
|
460
|
7 |
|
public function getRuleSets(): string |
|
461
|
|
|
{ |
|
462
|
7 |
|
return $this->ruleSets; |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
/** |
|
466
|
|
|
* Returns the minimum rule priority. |
|
467
|
|
|
*/ |
|
468
|
6 |
|
public function getMinimumPriority(): int |
|
469
|
|
|
{ |
|
470
|
6 |
|
return $this->minimumPriority; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
/** |
|
474
|
|
|
* Returns the maximum rule priority. |
|
475
|
|
|
*/ |
|
476
|
5 |
|
public function getMaximumPriority(): int |
|
477
|
|
|
{ |
|
478
|
5 |
|
return $this->maximumPriority; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* Returns the file name of a supplied code coverage report or <b>NULL</b> |
|
483
|
|
|
* if the user has not supplied the --coverage option. |
|
484
|
|
|
*/ |
|
485
|
6 |
|
public function getCoverageReport(): ?string |
|
486
|
|
|
{ |
|
487
|
6 |
|
return $this->coverageReport; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
/** |
|
491
|
|
|
* Returns a string of comma-separated extensions for valid php source code |
|
492
|
|
|
* filenames or <b>null</b> when this argument was not set. |
|
493
|
|
|
*/ |
|
494
|
5 |
|
public function getExtensions(): ?string |
|
495
|
|
|
{ |
|
496
|
5 |
|
return $this->extensions; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
/** |
|
500
|
|
|
* Returns string of comma-separated pattern that is used to exclude |
|
501
|
|
|
* directories or <b>null</b> when this argument was not set. |
|
502
|
|
|
*/ |
|
503
|
7 |
|
public function getIgnore(): ?string |
|
504
|
|
|
{ |
|
505
|
7 |
|
return $this->ignore; |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
/** |
|
509
|
|
|
* Was the <b>--version</b> passed to PHPMD's command line interface? |
|
510
|
|
|
*/ |
|
511
|
6 |
|
public function hasVersion(): bool |
|
512
|
|
|
{ |
|
513
|
6 |
|
return $this->version; |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* Was the <b>--strict</b> option passed to PHPMD's command line interface? |
|
518
|
|
|
* |
|
519
|
|
|
* @since 1.2.0 |
|
520
|
|
|
*/ |
|
521
|
6 |
|
public function hasStrict(): bool |
|
522
|
|
|
{ |
|
523
|
6 |
|
return $this->strict; |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
8 |
|
public function getVerbosity(): int |
|
527
|
|
|
{ |
|
528
|
8 |
|
return $this->verbosity; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
/** |
|
532
|
|
|
* Should the current violations be baselined |
|
533
|
|
|
*/ |
|
534
|
7 |
|
public function generateBaseline(): BaselineMode |
|
535
|
|
|
{ |
|
536
|
7 |
|
return $this->generateBaseline; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* The filepath of the baseline violations xml |
|
541
|
|
|
*/ |
|
542
|
6 |
|
public function baselineFile(): ?string |
|
543
|
|
|
{ |
|
544
|
6 |
|
return $this->baselineFile; |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
5 |
|
public function isCacheEnabled(): bool |
|
548
|
|
|
{ |
|
549
|
5 |
|
return $this->cacheEnabled; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
/** |
|
553
|
|
|
* The filepath to the result cache state file |
|
554
|
|
|
*/ |
|
555
|
1 |
|
public function cacheFile(): string |
|
556
|
|
|
{ |
|
557
|
1 |
|
return $this->cacheFile ?? '.phpmd.result-cache.php'; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
/** |
|
561
|
|
|
* The caching strategy to determine if a file should be (re)inspected. |
|
562
|
|
|
*/ |
|
563
|
1 |
|
public function cacheStrategy(): ResultCacheStrategy |
|
564
|
|
|
{ |
|
565
|
1 |
|
return $this->cacheStrategy; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Was the <b>--ignore-errors-on-exit</b> passed to PHPMD's command line interface? |
|
570
|
|
|
* |
|
571
|
|
|
* @since 2.10.0 |
|
572
|
|
|
*/ |
|
573
|
2 |
|
public function ignoreErrorsOnExit(): bool |
|
574
|
|
|
{ |
|
575
|
2 |
|
return $this->ignoreErrorsOnExit; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
/** |
|
579
|
|
|
* Was the <b>--ignore-violations-on-exit</b> passed to PHPMD's command line interface? |
|
580
|
|
|
*/ |
|
581
|
6 |
|
public function ignoreViolationsOnExit(): bool |
|
582
|
|
|
{ |
|
583
|
6 |
|
return $this->ignoreViolationsOnExit; |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
/** |
|
587
|
|
|
* Specify how many extra lines are added to a code snippet |
|
588
|
|
|
*/ |
|
589
|
1 |
|
public function extraLineInExcerpt(): ?int |
|
590
|
|
|
{ |
|
591
|
1 |
|
return $this->extraLineInExcerpt; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
/** |
|
595
|
|
|
* Creates a report renderer instance based on the user's command line |
|
596
|
|
|
* argument. |
|
597
|
|
|
* |
|
598
|
|
|
* @throws InvalidArgumentException When the specified renderer does not exist. |
|
599
|
|
|
*/ |
|
600
|
21 |
|
public function createRenderer(?string $reportFormat = null): RendererInterface |
|
601
|
|
|
{ |
|
602
|
21 |
|
$renderer = $this->createRendererWithoutOptions($reportFormat); |
|
603
|
|
|
|
|
604
|
18 |
|
if ($renderer instanceof Verbose) { |
|
605
|
7 |
|
$renderer->setVerbosityLevel($this->verbosity); |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
18 |
|
if ($renderer instanceof Color) { |
|
609
|
7 |
|
$renderer->setColored($this->colored); |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
18 |
|
return $renderer; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
/** |
|
616
|
|
|
* @throws InvalidArgumentException When the specified renderer does not exist. |
|
617
|
|
|
*/ |
|
618
|
21 |
|
private function createRendererWithoutOptions(?string $reportFormat = null): RendererInterface |
|
619
|
|
|
{ |
|
620
|
21 |
|
$reportFormat = $reportFormat ?: $this->reportFormat ?: ''; |
|
621
|
|
|
|
|
622
|
21 |
|
return (new RendererFactory())->getRenderer($reportFormat); |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
/** |
|
626
|
|
|
* Returns usage information for the PHPMD command line interface. |
|
627
|
|
|
* |
|
628
|
|
|
* @throws InvalidArgumentException |
|
629
|
|
|
*/ |
|
630
|
5 |
|
public function usage(): string |
|
631
|
|
|
{ |
|
632
|
5 |
|
$availableRenderers = $this->getListOfAvailableRenderers(); |
|
633
|
5 |
|
$noRenderers = ($availableRenderers === null); |
|
634
|
|
|
|
|
635
|
5 |
|
return 'Mandatory arguments:' . \PHP_EOL . |
|
636
|
5 |
|
'1) A php source code filename or directory. Can be a comma-' . |
|
637
|
5 |
|
'separated string, glob pattern, or "-" to scan stdin' . \PHP_EOL . |
|
638
|
5 |
|
'2) A report format' . \PHP_EOL . |
|
639
|
5 |
|
'3) A ruleset filename or a comma-separated string of ruleset' . |
|
640
|
5 |
|
'filenames' . \PHP_EOL . \PHP_EOL . |
|
641
|
5 |
|
'Example: phpmd /path/to/source format ruleset' . \PHP_EOL . \PHP_EOL . |
|
642
|
5 |
|
($noRenderers ? 'No available formats' : 'Available formats: ' . $availableRenderers) . '.' . \PHP_EOL . |
|
643
|
5 |
|
'Available rulesets: ' . implode(', ', $this->availableRuleSets) . '.' . \PHP_EOL . \PHP_EOL . |
|
644
|
5 |
|
'Optional arguments that may be put after the mandatory arguments:' . |
|
645
|
5 |
|
\PHP_EOL . |
|
646
|
5 |
|
'--verbose, -v, -vv, -vvv: Show debug information.' . \PHP_EOL . |
|
647
|
5 |
|
'--minimum-priority: rule priority threshold; rules with lower ' . |
|
648
|
5 |
|
'priority than this will not be used' . \PHP_EOL . |
|
649
|
5 |
|
'--report-file: send report output to a file; default to STDOUT' . |
|
650
|
5 |
|
\PHP_EOL . |
|
651
|
5 |
|
'--error-file: send errors (other than reported violations) ' . |
|
652
|
5 |
|
'output to a file; default to STDERR' . |
|
653
|
5 |
|
\PHP_EOL . |
|
654
|
5 |
|
'--suffixes: comma-separated string of valid source code ' . |
|
655
|
5 |
|
'filename extensions, e.g. php,phtml' . \PHP_EOL . |
|
656
|
5 |
|
'--exclude: comma-separated string of patterns that are used to ' . |
|
657
|
5 |
|
'ignore directories. Use asterisks to exclude by pattern. ' . |
|
658
|
5 |
|
'For example *src/foo/*.php or *src/foo/*' . \PHP_EOL . |
|
659
|
5 |
|
'--strict: also report those nodes with a @SuppressWarnings ' . |
|
660
|
5 |
|
'annotation' . \PHP_EOL . |
|
661
|
5 |
|
'--ignore-errors-on-exit: will exit with a zero code, ' . |
|
662
|
5 |
|
'even on error' . \PHP_EOL . |
|
663
|
5 |
|
'--ignore-violations-on-exit: will exit with a zero code, ' . |
|
664
|
5 |
|
'even if any violations are found' . \PHP_EOL . |
|
665
|
5 |
|
'--cache: will enable the result cache.' . \PHP_EOL . |
|
666
|
5 |
|
'--cache-file: instead of the default .phpmd.result-cache.php' . |
|
667
|
5 |
|
' will use this file as result cache file path.' . \PHP_EOL . |
|
668
|
5 |
|
'--cache-strategy: sets the caching strategy to determine if' . |
|
669
|
5 |
|
' a file is still fresh. Either `content` to base it on the ' . |
|
670
|
5 |
|
'file contents, or `timestamp` to base it on the file modified ' . |
|
671
|
5 |
|
'timestamp' . \PHP_EOL . |
|
672
|
5 |
|
'--generate-baseline: will generate a phpmd.baseline.xml next ' . |
|
673
|
5 |
|
'to the first ruleset file location' . \PHP_EOL . |
|
674
|
5 |
|
'--update-baseline: will remove any non-existing violations from the phpmd.baseline.xml' . \PHP_EOL . |
|
675
|
5 |
|
'--baseline-file: a custom location of the baseline file' . \PHP_EOL . |
|
676
|
5 |
|
'--color: enable color in output' . \PHP_EOL . |
|
677
|
5 |
|
'--extra-line-in-excerpt: Specify how many extra lines are added ' . |
|
678
|
5 |
|
'to a code snippet in html format' . \PHP_EOL . |
|
679
|
5 |
|
'--: Explicit argument separator: Anything after "--" will be read as an argument even if ' . |
|
680
|
5 |
|
'it starts with "-" or matches the name of an option' . \PHP_EOL; |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/** |
|
684
|
|
|
* Get a list of available renderers |
|
685
|
|
|
* |
|
686
|
|
|
* @return string|null The list of renderers found separated by comma, or null if none. |
|
687
|
|
|
* @throws InvalidArgumentException |
|
688
|
|
|
*/ |
|
689
|
5 |
|
private function getListOfAvailableRenderers(): ?string |
|
690
|
|
|
{ |
|
691
|
5 |
|
$renderersDirPathName = __DIR__ . '/../Renderer'; |
|
692
|
5 |
|
$renderers = []; |
|
693
|
|
|
|
|
694
|
5 |
|
$filesPaths = scandir($renderersDirPathName); |
|
695
|
5 |
|
if ($filesPaths === false) { |
|
696
|
|
|
throw new InvalidArgumentException("Unable to access directory: '{$renderersDirPathName}'."); |
|
697
|
|
|
} |
|
698
|
|
|
|
|
699
|
5 |
|
foreach ($filesPaths as $rendererFileName) { |
|
700
|
5 |
|
$rendererName = []; |
|
701
|
5 |
|
if (preg_match('/^(\w+)Renderer.php$/i', $rendererFileName, $rendererName)) { |
|
702
|
5 |
|
$renderers[] = strtolower($rendererName[1]); |
|
703
|
|
|
} |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
5 |
|
sort($renderers); |
|
707
|
|
|
|
|
708
|
5 |
|
return implode(', ', $renderers) ?: null; |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
|
|
/** |
|
712
|
|
|
* Logs a deprecated option to the current user interface. |
|
713
|
|
|
*/ |
|
714
|
3 |
|
private function logDeprecated(string $deprecatedName, string $newName): void |
|
715
|
|
|
{ |
|
716
|
3 |
|
$this->deprecations[] = sprintf( |
|
717
|
3 |
|
'The --%s option is deprecated, please use --%s instead.', |
|
718
|
3 |
|
$deprecatedName, |
|
719
|
3 |
|
$newName |
|
720
|
3 |
|
); |
|
721
|
|
|
} |
|
722
|
|
|
|
|
723
|
|
|
/** |
|
724
|
|
|
* This method takes the given input file, reads the newline separated paths |
|
725
|
|
|
* from that file and creates a comma separated string of the file paths. If |
|
726
|
|
|
* the given <b>$inputFile</b> not exists, this method will throw an |
|
727
|
|
|
* exception. |
|
728
|
|
|
* |
|
729
|
|
|
* @param string $inputFile Specified input file name. |
|
730
|
|
|
* @throws InvalidArgumentException If the specified input file does not exist. |
|
731
|
|
|
* @since 1.1.0 |
|
732
|
|
|
*/ |
|
733
|
6 |
|
private function readInputFile(string $inputFile): string |
|
734
|
|
|
{ |
|
735
|
6 |
|
$content = @file($inputFile); |
|
736
|
6 |
|
if ($content === false) { |
|
737
|
1 |
|
throw new InvalidArgumentException("Unable to load '{$inputFile}'."); |
|
738
|
|
|
} |
|
739
|
|
|
|
|
740
|
5 |
|
return implode(',', array_map(trim(...), $content)); |
|
|
|
|
|
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
/** |
|
744
|
|
|
* Throw an exception if a boolean option has a value (is followed by equal). |
|
745
|
|
|
* |
|
746
|
|
|
* @param string[] $equalChunk The CLI parameter split in 2 by "=" sign |
|
747
|
|
|
* |
|
748
|
|
|
* @throws InvalidArgumentException if a boolean option has a value (is followed by equal) |
|
749
|
|
|
*/ |
|
750
|
15 |
|
private function refuseValue(array $equalChunk): void |
|
751
|
|
|
{ |
|
752
|
15 |
|
if (count($equalChunk) > 1) { |
|
753
|
1 |
|
throw new InvalidArgumentException($equalChunk[0] . ' option does not accept a value'); |
|
754
|
|
|
} |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
/** |
|
758
|
|
|
* Return value for an option either what is after "=" sign if present, else take the next CLI parameter. |
|
759
|
|
|
* |
|
760
|
|
|
* @param list<string> $equalChunk The CLI parameter split in 2 by "=" sign |
|
761
|
|
|
* @param string[] &$args The remaining CLI parameters not yet parsed |
|
762
|
|
|
*/ |
|
763
|
24 |
|
private function readValue(array $equalChunk, array &$args): ?string |
|
764
|
|
|
{ |
|
765
|
24 |
|
if (count($equalChunk) > 1) { |
|
766
|
1 |
|
return $equalChunk[1]; |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
23 |
|
return array_shift($args); |
|
770
|
|
|
} |
|
771
|
|
|
} |
|
772
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths