Skip to content

Commit 738bd67

Browse files
committed
Refactor
1 parent d2ce191 commit 738bd67

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

src/PhpGitHooks/Application/Message/MessageConfigData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function config(array $data)
5252
private function configMessages()
5353
{
5454
if (!isset($this->configData[self::TOOL])) {
55-
$this->configData = null;
55+
$this->configData = [];
5656
$rightMessage = $this->setMessage(
5757
sprintf('Write a right message for %s hook:', $this->hook),
5858
self::DEFAULT_RIGHT_MESSAGE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpGitHooks\Infrastructure\Common;
4+
5+
use PhpGitHooks\Command\BadJobLogo;
6+
7+
class ErrorOutput
8+
{
9+
/**
10+
* @param $errorText
11+
*
12+
* @return string
13+
*/
14+
public static function write($errorText)
15+
{
16+
return BadJobLogo::paint($errorText);
17+
}
18+
}

src/PhpGitHooks/Infrastructure/Common/ToolHandler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ public function setOutput(OutputInterface $outputInterface)
3030
{
3131
$this->output = $outputInterface;
3232
}
33+
34+
/**
35+
* @param \Exception $exceptionClass
36+
* @param string $errorText
37+
*/
38+
protected function writeOutputError(\Exception $exceptionClass, $errorText)
39+
{
40+
ErrorOutput::write($errorText);
41+
throw new $exceptionClass();
42+
}
3343
}

src/PhpGitHooks/Infrastructure/JsonLint/JsonLintHandler.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
namespace PhpGitHooks\Infrastructure\JsonLint;
44

55
use PhpGitHooks\Application\Message\MessageConfigData;
6-
use PhpGitHooks\Command\BadJobLogo;
76
use PhpGitHooks\Infrastructure\Common\RecursiveToolInterface;
87
use PhpGitHooks\Infrastructure\Common\ToolHandler;
98
use Symfony\Component\Process\ProcessBuilder;
109

1110
final class JsonLintHandler extends ToolHandler implements RecursiveToolInterface
1211
{
13-
/** @var array */
12+
/** @var array */
1413
private $files = [];
1514
/** @var string */
1615
private $needle;
@@ -52,8 +51,10 @@ public function run(array $messages)
5251
});
5352

5453
if ($errors) {
55-
$this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE]));
56-
throw new JsonLintViolationsException(implode('', $errors));
54+
$this->writeOutputError(
55+
new JsonLintViolationsException(implode('', $errors)),
56+
$messages[MessageConfigData::KEY_ERROR_MESSAGE]
57+
);
5758
}
5859

5960
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());

src/PhpGitHooks/Infrastructure/PhpCsFixer/PhpCsFixerHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PhpGitHooks\Infrastructure\PhpCsFixer;
44

55
use PhpGitHooks\Application\Message\MessageConfigData;
6-
use PhpGitHooks\Command\BadJobLogo;
76
use PhpGitHooks\Infrastructure\Common\InteractiveToolInterface;
87
use PhpGitHooks\Infrastructure\Common\ToolHandler;
98
use Symfony\Component\Process\ProcessBuilder;
@@ -56,8 +55,10 @@ public function run(array $messages)
5655
}
5756

5857
if ($errors) {
59-
$this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE]));
60-
throw new PhpCsFixerException(implode('', $errors));
58+
$this->writeOutputError(
59+
new PhpCsFixerException(implode('', $errors)),
60+
$messages[MessageConfigData::KEY_ERROR_MESSAGE]
61+
);
6162
}
6263

6364
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());

src/PhpGitHooks/Infrastructure/PhpMD/PhpMDHandler.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PhpGitHooks\Infrastructure\PhpMD;
44

55
use PhpGitHooks\Application\Message\MessageConfigData;
6-
use PhpGitHooks\Command\BadJobLogo;
76
use PhpGitHooks\Infrastructure\Common\RecursiveToolInterface;
87
use PhpGitHooks\Infrastructure\Common\ToolHandler;
98
use Symfony\Component\Process\ProcessBuilder;
@@ -59,8 +58,10 @@ public function run(array $messages)
5958
});
6059

6160
if ($errors) {
62-
$this->output->writeln(BadJobLogo::paint(MessageConfigData::KEY_ERROR_MESSAGE));
63-
throw new PHPMDViolationsException(implode('', $errors));
61+
$this->writeOutputError(
62+
new PHPMDViolationsException(implode('', $errors)),
63+
MessageConfigData::KEY_ERROR_MESSAGE
64+
);
6465
}
6566

6667
$this->output->writeln($this->outputHandler->getSuccessfulStepMessage());

0 commit comments

Comments
 (0)