@@ 13-78 (lines=66) @@ | ||
10 | use PhpGitHooks\Module\PhpCs\Model\PhpCsToolProcessorInterface; |
|
11 | use Symfony\Component\Console\Output\OutputInterface; |
|
12 | ||
13 | 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 | public function __construct(OutputInterface $output, PhpCsToolProcessorInterface $phpCsToolProcessor) |
|
32 | { |
|
33 | $this->output = $output; |
|
34 | $this->phpCsToolProcessor = $phpCsToolProcessor; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * @param array $files |
|
39 | * @param string $standard |
|
40 | * @param string $errorMessage |
|
41 | * @param string $ignore |
|
42 | * |
|
43 | * @throws PhpCsViolationException |
|
44 | */ |
|
45 | private function execute(array $files, $standard, $errorMessage, $ignore) |
|
46 | { |
|
47 | $outputMessage = new PreCommitOutputWriter(self::EXECUTE_MESSAGE); |
|
48 | $this->output->write($outputMessage->getMessage()); |
|
49 | ||
50 | $errors = []; |
|
51 | foreach ($files as $file) { |
|
52 | $errors[] = $this->phpCsToolProcessor->process($file, $standard, $ignore); |
|
53 | } |
|
54 | ||
55 | $errors = array_filter($errors); |
|
56 | ||
57 | if (!empty($errors)) { |
|
58 | $this->output->writeln($outputMessage->getFailMessage()); |
|
59 | $this->output->writeln($outputMessage->setError(implode('', $errors))); |
|
60 | $this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
|
61 | throw new PhpCsViolationException(); |
|
62 | } |
|
63 | $this->output->writeln($outputMessage->getSuccessfulMessage()); |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * @param CommandInterface|PhpCsTool $command |
|
68 | */ |
|
69 | public function handle(CommandInterface $command) |
|
70 | { |
|
71 | $this->execute( |
|
72 | $command->getFiles(), |
|
73 | $command->getStandard(), |
|
74 | $command->getErrorMessage(), |
|
75 | $command->getIgnore() |
|
76 | ); |
|
77 | } |
|
78 | } |
|
79 |
@@ 13-74 (lines=62) @@ | ||
10 | use PhpGitHooks\Module\PhpMd\Model\PhpMdToolProcessorInterface; |
|
11 | use Symfony\Component\Console\Output\OutputInterface; |
|
12 | ||
13 | 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 | public function __construct(OutputInterface $output, PhpMdToolProcessorInterface $phpMdToolProcessor) |
|
32 | { |
|
33 | $this->output = $output; |
|
34 | $this->phpMdToolProcessor = $phpMdToolProcessor; |
|
35 | } |
|
36 | ||
37 | /** |
|
38 | * @param array $files |
|
39 | * @param string $options |
|
40 | * @param string $errorMessage |
|
41 | * |
|
42 | * @throws PhpMdViolationsException |
|
43 | */ |
|
44 | private function execute(array $files, $options, $errorMessage) |
|
45 | { |
|
46 | $outputMessage = new PreCommitOutputWriter(self::CHECKING_MESSAGE); |
|
47 | $this->output->write($outputMessage->getMessage()); |
|
48 | ||
49 | $errors = []; |
|
50 | foreach ($files as $file) { |
|
51 | $errors[] = $this->phpMdToolProcessor->process($file, $options); |
|
52 | } |
|
53 | ||
54 | $errors = array_filter($errors); |
|
55 | ||
56 | if (!empty($errors)) { |
|
57 | $outputText = $outputMessage->setError(implode('', $errors)); |
|
58 | $this->output->writeln($outputMessage->getFailMessage()); |
|
59 | $this->output->writeln($outputText); |
|
60 | $this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
|
61 | throw new PhpMdViolationsException(); |
|
62 | } |
|
63 | ||
64 | $this->output->writeln($outputMessage->getSuccessfulMessage()); |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * @param CommandInterface|PhpMdTool $command |
|
69 | */ |
|
70 | public function handle(CommandInterface $command) |
|
71 | { |
|
72 | $this->execute($command->getFiles(), $command->getOptions(), $command->getErrorMessage()); |
|
73 | } |
|
74 | } |
|
75 |