1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Infrastructure\PhpMD; |
4
|
|
|
|
5
|
|
|
use IgnoreFiles\IgnoreFiles; |
6
|
|
|
use PhpGitHooks\Application\Message\MessageConfigData; |
7
|
|
|
use PhpGitHooks\Command\OutputHandlerInterface; |
8
|
|
|
use PhpGitHooks\Infrastructure\Common\RecursiveToolInterface; |
9
|
|
|
use PhpGitHooks\Infrastructure\Common\ToolHandler; |
10
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PhpMDHandler. |
14
|
|
|
*/ |
15
|
|
|
class PhpMDHandler extends ToolHandler implements RecursiveToolInterface |
16
|
|
|
{ |
17
|
|
|
/** @var array */ |
18
|
|
|
private $files; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $needle; |
21
|
|
|
/** |
22
|
|
|
* @var IgnoreFiles |
23
|
|
|
*/ |
24
|
|
|
private $ignoreFiles; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PhpMDHandler constructor. |
28
|
|
|
* |
29
|
|
|
* @param OutputHandlerInterface $outputHandler |
30
|
|
|
* @param IgnoreFiles $ignoreFiles |
31
|
|
|
*/ |
32
|
|
|
public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($outputHandler); |
35
|
|
|
$this->ignoreFiles = $ignoreFiles; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $messages |
40
|
|
|
* |
41
|
|
|
* @throws PHPMDViolationsException |
42
|
|
|
*/ |
43
|
|
|
public function run(array $messages) |
44
|
|
|
{ |
45
|
|
|
$this->outputHandler->setTitle('Checking code mess with PHPMD'); |
46
|
|
|
$this->output->write($this->outputHandler->getTitle()); |
47
|
|
|
|
48
|
|
|
$errors = []; |
49
|
|
|
|
50
|
|
|
foreach ($this->files as $file) { |
51
|
|
|
if (!preg_match($this->needle, $file)) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (false === $this->ignoreFiles->isIgnored($file)) { |
|
|
|
|
56
|
|
|
$processBuilder = new ProcessBuilder( |
57
|
|
|
array( |
58
|
|
|
'php', |
59
|
|
|
'bin/phpmd', |
60
|
|
|
$file, |
61
|
|
|
'text', |
62
|
|
|
'PmdRules.xml', |
63
|
|
|
'--minimumpriority', |
64
|
|
|
1, |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
$process = $processBuilder->getProcess(); |
68
|
|
|
$process->run(); |
69
|
|
|
|
70
|
|
|
if (false === $process->isSuccessful()) { |
71
|
|
|
$errors[] = $process->getOutput(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$errors = array_filter($errors, function ($var) { |
77
|
|
|
return !is_null($var); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
if ($errors) { |
|
|
|
|
81
|
|
|
$this->writeOutputError( |
82
|
|
|
new PHPMDViolationsException(implode('', $errors)), |
83
|
|
|
MessageConfigData::KEY_ERROR_MESSAGE |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $needle |
92
|
|
|
*/ |
93
|
|
|
public function setNeedle($needle) |
94
|
|
|
{ |
95
|
|
|
$this->needle = $needle; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $files |
100
|
|
|
*/ |
101
|
|
|
public function setFiles($files) |
102
|
|
|
{ |
103
|
|
|
$this->files = $files; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.