1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\PhpMD; |
4
|
|
|
|
5
|
|
|
use IgnoreFiles\IgnoreFiles; |
6
|
|
|
use PhpGitHooks\Command\OutputHandlerInterface; |
7
|
|
|
use PhpGitHooks\Infrastructure\Common\RecursiveToolInterface; |
8
|
|
|
use PhpGitHooks\Infrastructure\Common\ToolHandler; |
9
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PhpMDHandler. |
13
|
|
|
*/ |
14
|
|
|
class PhpMDHandler extends ToolHandler implements RecursiveToolInterface |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
private $files; |
18
|
|
|
/** @var string */ |
19
|
|
|
private $needle; |
20
|
|
|
/** @var int */ |
21
|
|
|
private $minimumPriority; |
22
|
|
|
/** |
23
|
|
|
* @var IgnoreFiles |
24
|
|
|
*/ |
25
|
|
|
private $ignoreFiles; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* PhpMDHandler constructor. |
29
|
|
|
* |
30
|
|
|
* @param OutputHandlerInterface $outputHandler |
31
|
|
|
* @param IgnoreFiles $ignoreFiles |
32
|
|
|
*/ |
33
|
|
|
public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($outputHandler); |
36
|
|
|
$this->ignoreFiles = $ignoreFiles; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $messages |
41
|
|
|
* |
42
|
|
|
* @throws PHPMDViolationsException |
43
|
|
|
*/ |
44
|
|
|
public function run(array $messages) |
45
|
|
|
{ |
46
|
|
|
$this->outputHandler->setTitle('Checking code mess with PHPMD'); |
47
|
|
|
$this->output->write($this->outputHandler->getTitle()); |
48
|
|
|
|
49
|
|
|
$errors = []; |
50
|
|
|
|
51
|
|
|
foreach ($this->files as $file) { |
52
|
|
|
if (!preg_match($this->needle, $file)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$pmdRulesXml = realpath(__DIR__.self::COMPOSER_VENDOR_DIR).'/../PmdRules.xml'; |
57
|
|
|
if (!file_exists($pmdRulesXml)) { |
58
|
|
|
throw new PHPMDViolationsException('You don\'t have specific PmdRules.xml in the root of your project. See Readme.md to create it'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$command = array( |
62
|
|
|
'php', |
63
|
|
|
$this->getBinPath('phpmd'), |
64
|
|
|
$file, |
65
|
|
|
'text', |
66
|
|
|
'/PmdRules.xml', |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if ($this->getMinimumPriority() > -1) { |
70
|
|
|
array_push($command, '--minimumpriority'); |
71
|
|
|
array_push($command, $this->getMinimumPriority()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (false === $this->ignoreFiles->isIgnored($file)) { |
75
|
|
|
$processBuilder = new ProcessBuilder($command); |
76
|
|
|
$process = $processBuilder->getProcess(); |
77
|
|
|
$process->run(); |
78
|
|
|
|
79
|
|
|
if (false === $process->isSuccessful()) { |
80
|
|
|
$errors[] = $process->getOutput(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$errors = array_filter($errors, function ($var) { |
86
|
|
|
return trim($var); |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
if (!empty($errors)) { |
90
|
|
|
$this->writeOutputError( |
91
|
|
|
new PHPMDViolationsException("Check MD errors listed above"), |
92
|
|
|
implode("\n", $errors) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $needle |
101
|
|
|
*/ |
102
|
|
|
public function setNeedle($needle) |
103
|
|
|
{ |
104
|
|
|
$this->needle = $needle; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $files |
109
|
|
|
*/ |
110
|
|
|
public function setFiles($files) |
111
|
|
|
{ |
112
|
|
|
$this->files = $files; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function getMinimumPriority() |
119
|
|
|
{ |
120
|
|
|
return $this->minimumPriority; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param int $minimumPriority |
125
|
|
|
*/ |
126
|
|
|
public function setMinimumPriority($minimumPriority) |
127
|
|
|
{ |
128
|
|
|
$this->minimumPriority = $minimumPriority; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|