|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP Mess Detector. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under BSD License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Manuel Pichler <[email protected]> |
|
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
15
|
|
|
* @link http://phpmd.org/ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PHPMD\TextUI; |
|
19
|
|
|
|
|
20
|
|
|
use PHPMD\PHPMD; |
|
21
|
|
|
use PHPMD\RuleSetFactory; |
|
22
|
|
|
use PHPMD\Writer\StreamWriter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This class provides a command line interface for PHPMD |
|
26
|
|
|
*/ |
|
27
|
|
|
class Command |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Exit codes used by the phpmd command line tool. |
|
31
|
|
|
*/ |
|
32
|
|
|
const EXIT_SUCCESS = 0, |
|
33
|
|
|
EXIT_EXCEPTION = 1, |
|
34
|
|
|
EXIT_VIOLATION = 2; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* This method creates a PHPMD instance and configures this object based |
|
38
|
|
|
* on the user's input, then it starts the source analysis. |
|
39
|
|
|
* |
|
40
|
|
|
* The return value of this method can be used as an exit code. A value |
|
41
|
|
|
* equal to <b>EXIT_SUCCESS</b> means that no violations or errors were |
|
42
|
|
|
* found in the analyzed code. Otherwise this method will return a value |
|
43
|
|
|
* equal to <b>EXIT_VIOLATION</b>. |
|
44
|
|
|
* |
|
45
|
|
|
* The use of flag <b>--ignore-violations-on-exit</b> will result to a |
|
46
|
|
|
* <b>EXIT_SUCCESS</b> even if any violation is found. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \PHPMD\TextUI\CommandLineOptions $opts |
|
49
|
|
|
* @param \PHPMD\RuleSetFactory $ruleSetFactory |
|
50
|
|
|
* @return integer |
|
51
|
|
|
*/ |
|
52
|
14 |
|
public function run(CommandLineOptions $opts, RuleSetFactory $ruleSetFactory) |
|
53
|
|
|
{ |
|
54
|
14 |
|
if ($opts->hasVersion()) { |
|
55
|
1 |
|
fwrite(STDOUT, sprintf('PHPMD %s', $this->getVersion()) . PHP_EOL); |
|
56
|
1 |
|
return self::EXIT_SUCCESS; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Create a report stream |
|
60
|
13 |
|
$stream = $opts->getReportFile() ? $opts->getReportFile() : STDOUT; |
|
61
|
|
|
|
|
62
|
|
|
// Create renderer and configure output |
|
63
|
13 |
|
$renderer = $opts->createRenderer(); |
|
64
|
12 |
|
$renderer->setWriter(new StreamWriter($stream)); |
|
65
|
|
|
|
|
66
|
12 |
|
$renderers = array($renderer); |
|
67
|
|
|
|
|
68
|
12 |
|
foreach ($opts->getReportFiles() as $reportFormat => $reportFile) { |
|
69
|
1 |
|
$reportRenderer = $opts->createRenderer($reportFormat); |
|
70
|
1 |
|
$reportRenderer->setWriter(new StreamWriter($reportFile)); |
|
71
|
|
|
|
|
72
|
1 |
|
$renderers[] = $reportRenderer; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Configure a rule set factory |
|
76
|
12 |
|
$ruleSetFactory->setMinimumPriority($opts->getMinimumPriority()); |
|
77
|
12 |
|
$ruleSetFactory->setMaximumPriority($opts->getMaximumPriority()); |
|
78
|
12 |
|
if ($opts->hasStrict()) { |
|
79
|
1 |
|
$ruleSetFactory->setStrict(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
$phpmd = new PHPMD(); |
|
83
|
12 |
|
$phpmd->setOptions( |
|
84
|
12 |
|
array_filter( |
|
85
|
|
|
array( |
|
86
|
12 |
|
'coverage' => $opts->getCoverageReport() |
|
87
|
|
|
) |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
12 |
|
$extensions = $opts->getExtensions(); |
|
92
|
12 |
|
if ($extensions !== null) { |
|
93
|
1 |
|
$phpmd->setFileExtensions(explode(',', $extensions)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
12 |
|
$ignore = $opts->getIgnore(); |
|
97
|
12 |
|
if ($ignore !== null) { |
|
98
|
1 |
|
$phpmd->setIgnorePattern(explode(',', $ignore)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
$phpmd->processFiles( |
|
102
|
12 |
|
$opts->getInputPath(), |
|
103
|
12 |
|
$opts->getRuleSets(), |
|
104
|
12 |
|
$renderers, |
|
105
|
12 |
|
$ruleSetFactory |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
12 |
|
if ($phpmd->hasViolations() && !$opts->ignoreViolationsOnExit()) { |
|
109
|
6 |
|
return self::EXIT_VIOLATION; |
|
110
|
|
|
} |
|
111
|
6 |
|
return self::EXIT_SUCCESS; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the current version number. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
1 |
|
private function getVersion() |
|
120
|
|
|
{ |
|
121
|
1 |
|
$build = __DIR__ . '/../../../../../build.properties'; |
|
122
|
|
|
|
|
123
|
1 |
|
$version = '@package_version@'; |
|
124
|
1 |
|
if (file_exists($build)) { |
|
125
|
1 |
|
$data = @parse_ini_file($build); |
|
126
|
1 |
|
$version = $data['project.version']; |
|
127
|
|
|
} |
|
128
|
1 |
|
return $version; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* The main method that can be used by a calling shell script, the return |
|
133
|
|
|
* value can be used as exit code. |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $args The raw command line arguments array. |
|
136
|
|
|
* @return integer |
|
137
|
|
|
*/ |
|
138
|
14 |
|
public static function main(array $args) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
14 |
|
$ruleSetFactory = new RuleSetFactory(); |
|
142
|
14 |
|
$options = new CommandLineOptions($args, $ruleSetFactory->listAvailableRuleSets()); |
|
143
|
14 |
|
$command = new Command(); |
|
144
|
|
|
|
|
145
|
14 |
|
$exitCode = $command->run($options, $ruleSetFactory); |
|
146
|
1 |
|
} catch (\Exception $e) { |
|
147
|
1 |
|
fwrite(STDERR, $e->getMessage() . PHP_EOL); |
|
148
|
1 |
|
$exitCode = self::EXIT_EXCEPTION; |
|
149
|
|
|
} |
|
150
|
14 |
|
return $exitCode; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|