1 | <?php |
||
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() |
|
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) |
|
152 | } |
||
153 |