-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathPhpMessDetector.php
More file actions
225 lines (186 loc) · 5.69 KB
/
Copy pathPhpMessDetector.php
File metadata and controls
225 lines (186 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace PHPCensor\Plugin;
use Exception;
use PHPCensor;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
use PHPCensor\ZeroConfigPluginInterface;
use PHPCensor\Common\Exception\RuntimeException;
/**
* PHP Mess Detector Plugin - Allows PHP Mess Detector testing.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class PhpMessDetector extends Plugin implements ZeroConfigPluginInterface
{
/**
* @var array
*/
protected $suffixes = ['php'];
/**
* Array of PHPMD rules. Can be one of the builtins (codesize, unusedcode, naming, design, controversial)
* or a filename (detected by checking for a / in it), either absolute or relative to the project root.
* @var array
*/
protected $rules = ['codesize', 'unusedcode', 'naming'];
protected $allowedWarnings = 0;
/**
* @return string
*/
public static function pluginName()
{
return 'php_mess_detector';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
if (isset($options['zero_config']) && $options['zero_config']) {
$this->allowedWarnings = -1;
}
if (\array_key_exists('allowed_warnings', $options)) {
$this->allowedWarnings = (int)$options['allowed_warnings'];
}
$this->executable = $this->findBinary(['phpmd', 'phpmd.phar']);
foreach (['rules', 'suffixes'] as $key) {
$this->overrideSetting($options, $key);
}
}
/**
* {@inheritDoc}
*/
public static function canExecuteOnStage($stage, Build $build)
{
if (Build::STAGE_TEST === $stage) {
return true;
}
return false;
}
/**
* Runs PHP Mess Detector in a specified directory.
*/
public function execute()
{
if (!$this->tryAndProcessRules()) {
return false;
}
$phpmdBinaryPath = $this->executable;
$this->executePhpMd($phpmdBinaryPath);
$errorCount = $this->processReport(\trim($this->builder->getLastOutput()));
$this->build->storeMeta((self::pluginName() . '-warnings'), $errorCount);
return $this->wasLastExecSuccessful($errorCount);
}
/**
* Override a default setting.
*/
protected function overrideSetting($options, $key)
{
if (isset($options[$key]) && \is_array($options[$key])) {
$this->{$key} = $options[$key];
}
}
/**
* Process PHPMD's XML output report.
*
*
* @return int
*
* @throws Exception
*/
protected function processReport($xmlString)
{
$xml = \simplexml_load_string($xmlString);
if (false === $xml) {
$this->builder->log($xmlString);
throw new RuntimeException('Could not process PHPMD report XML.');
}
$warnings = 0;
foreach ($xml->file as $file) {
$fileName = (string)$file['name'];
$fileName = \str_replace($this->builder->buildPath, '', $fileName);
foreach ($file->violation as $violation) {
$warnings++;
$this->build->reportError(
$this->builder,
self::pluginName(),
(string)$violation,
PHPCensor\Model\BuildError::SEVERITY_HIGH,
$fileName,
(int)$violation['beginline'],
(int)$violation['endline']
);
}
}
return $warnings;
}
/**
* Try and process the rules parameter from .php-censor.yml.
* @return bool
*/
protected function tryAndProcessRules()
{
if (!empty($this->rules) && !\is_array($this->rules)) {
$this->builder->logFailure('The "rules" option must be an array.');
return false;
}
foreach ($this->rules as &$rule) {
if (\strpos($rule, '/') !== false) {
$rule = $this->builder->buildPath . $rule;
}
}
return true;
}
/**
* Execute PHP Mess Detector.
*/
protected function executePhpMd($binaryPath)
{
$cmd = 'cd "%s" && ' . $binaryPath . ' "%s" xml %s %s %s';
$ignore = '';
if (\is_array($this->ignore) && \count($this->ignore) > 0) {
$ignoreArray = [];
foreach ($this->ignore as $ignoreItem) {
$ignoreArray[] = /*$this->builder->buildPath .*/ $ignoreItem;
}
$ignore = \sprintf(' --exclude "%s"', \implode(',', $ignoreArray));
}
$suffixes = '';
if (\is_array($this->suffixes) && \count($this->suffixes) > 0) {
$suffixes = ' --suffixes ' . \implode(',', $this->suffixes);
}
if (!$this->build->isDebug()) {
$this->builder->logExecOutput(false);
}
// Run PHPMD:
$this->builder->executeCommand(
$cmd,
$this->builder->buildPath,
$this->directory,
\implode(',', $this->rules),
$ignore,
$suffixes
);
$this->builder->logExecOutput(true);
}
/**
* Returns a bool indicating if the error count can be considered a success.
*
* @param int $errorCount
*
* @return bool
*/
protected function wasLastExecSuccessful($errorCount)
{
if (-1 !== $this->allowedWarnings && $errorCount > $this->allowedWarnings) {
return false;
}
return true;
}
}