1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpMd\Contract\Command; |
4
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
7
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
8
|
|
|
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter; |
9
|
|
|
use PhpGitHooks\Module\PhpMd\Contract\Exception\PhpMdViolationsException; |
10
|
|
|
use PhpGitHooks\Module\PhpMd\Model\PhpMdToolProcessorInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
View Code Duplication |
class PhpMdToolHandler implements CommandHandlerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
const CHECKING_MESSAGE = 'Checking code mess with PHPMD'; |
16
|
|
|
/** |
17
|
|
|
* @var OutputInterface |
18
|
|
|
*/ |
19
|
|
|
private $output; |
20
|
|
|
/** |
21
|
|
|
* @var PhpMdToolProcessorInterface |
22
|
|
|
*/ |
23
|
|
|
private $phpMdToolProcessor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PhpMdTool constructor. |
27
|
|
|
* |
28
|
|
|
* @param OutputInterface $output |
29
|
|
|
* @param PhpMdToolProcessorInterface $phpMdToolProcessor |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(OutputInterface $output, PhpMdToolProcessorInterface $phpMdToolProcessor) |
32
|
|
|
{ |
33
|
2 |
|
$this->output = $output; |
34
|
2 |
|
$this->phpMdToolProcessor = $phpMdToolProcessor; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $files |
39
|
|
|
* @param string $options |
40
|
|
|
* @param string $errorMessage |
41
|
|
|
* |
42
|
|
|
* @throws PhpMdViolationsException |
43
|
|
|
*/ |
44
|
2 |
|
private function execute(array $files, $options, $errorMessage) |
45
|
|
|
{ |
46
|
2 |
|
$outputMessage = new PreCommitOutputWriter(self::CHECKING_MESSAGE); |
47
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
48
|
|
|
|
49
|
2 |
|
$errors = []; |
50
|
2 |
|
foreach ($files as $file) { |
51
|
2 |
|
$errors[] = $this->phpMdToolProcessor->process($file, $options); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$errors = array_filter($errors); |
55
|
|
|
|
56
|
2 |
|
if (!empty($errors)) { |
57
|
1 |
|
$outputText = $outputMessage->setError(implode('', $errors)); |
58
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
59
|
1 |
|
$this->output->writeln($outputText); |
60
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
61
|
1 |
|
throw new PhpMdViolationsException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param CommandInterface|PhpMdTool $command |
69
|
|
|
*/ |
70
|
2 |
|
public function handle(CommandInterface $command) |
71
|
|
|
{ |
72
|
2 |
|
$this->execute($command->getFiles(), $command->getOptions(), $command->getErrorMessage()); |
|
|
|
|
73
|
1 |
|
} |
74
|
|
|
} |
75
|
|
|
|
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.