1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpCs\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\PhpCs\Contract\Exception\PhpCsViolationException; |
10
|
|
|
use PhpGitHooks\Module\PhpCs\Model\PhpCsToolProcessorInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
View Code Duplication |
class PhpCsToolHandler implements CommandHandlerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
const EXECUTE_MESSAGE = 'Checking code style with PHPCS'; |
16
|
|
|
/** |
17
|
|
|
* @var OutputInterface |
18
|
|
|
*/ |
19
|
|
|
private $output; |
20
|
|
|
/** |
21
|
|
|
* @var PhpCsToolProcessorInterface |
22
|
|
|
*/ |
23
|
|
|
private $phpCsToolProcessor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PhpCsTool constructor. |
27
|
|
|
* |
28
|
|
|
* @param OutputInterface $output |
29
|
|
|
* @param PhpCsToolProcessorInterface $phpCsToolProcessor |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(OutputInterface $output, PhpCsToolProcessorInterface $phpCsToolProcessor) |
32
|
|
|
{ |
33
|
2 |
|
$this->output = $output; |
34
|
2 |
|
$this->phpCsToolProcessor = $phpCsToolProcessor; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $files |
39
|
|
|
* @param string $standard |
40
|
|
|
* @param string $errorMessage |
41
|
|
|
* @param string $ignore |
42
|
|
|
* |
43
|
|
|
* @throws PhpCsViolationException |
44
|
|
|
*/ |
45
|
2 |
|
private function execute(array $files, $standard, $errorMessage, $ignore) |
46
|
|
|
{ |
47
|
2 |
|
$outputMessage = new PreCommitOutputWriter(self::EXECUTE_MESSAGE); |
48
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
49
|
|
|
|
50
|
2 |
|
$errors = []; |
51
|
2 |
|
foreach ($files as $file) { |
52
|
2 |
|
$errors[] = $this->phpCsToolProcessor->process($file, $standard, $ignore); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$errors = array_filter($errors); |
56
|
|
|
|
57
|
2 |
|
if (!empty($errors)) { |
58
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
59
|
1 |
|
$this->output->writeln($outputMessage->setError(implode('', $errors))); |
60
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
61
|
1 |
|
throw new PhpCsViolationException(); |
62
|
|
|
} |
63
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param CommandInterface|PhpCsTool $command |
68
|
|
|
*/ |
69
|
2 |
|
public function handle(CommandInterface $command) |
70
|
|
|
{ |
71
|
2 |
|
$this->execute( |
72
|
2 |
|
$command->getFiles(), |
|
|
|
|
73
|
2 |
|
$command->getStandard(), |
|
|
|
|
74
|
2 |
|
$command->getErrorMessage(), |
|
|
|
|
75
|
2 |
|
$command->getIgnore() |
|
|
|
|
76
|
|
|
); |
77
|
1 |
|
} |
78
|
|
|
} |
79
|
|
|
|
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.