1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpLint\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\PhpLint\Contract\Exception\PhpLintViolationsException; |
10
|
|
|
use PhpGitHooks\Module\PhpLint\Model\PhpLintToolProcessorInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class PhpLintToolHandler implements CommandHandlerInterface |
14
|
|
|
{ |
15
|
|
|
const RUNNING_PHPLINT = 'Running PHPLINT'; |
16
|
|
|
/** |
17
|
|
|
* @var PhpLintToolProcessorInterface |
18
|
|
|
*/ |
19
|
|
|
private $phpLintTool; |
20
|
|
|
/** |
21
|
|
|
* @var OutputInterface |
22
|
|
|
*/ |
23
|
|
|
private $output; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PhpLintTool constructor. |
27
|
|
|
* |
28
|
|
|
* @param PhpLintToolProcessorInterface $phpLintTool |
29
|
|
|
* @param OutputInterface $output |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(PhpLintToolProcessorInterface $phpLintTool, OutputInterface $output) |
32
|
|
|
{ |
33
|
2 |
|
$this->phpLintTool = $phpLintTool; |
34
|
2 |
|
$this->output = $output; |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $files |
39
|
|
|
* @param string $errorMessage |
40
|
|
|
* |
41
|
|
|
* @throws PhpLintViolationsException |
42
|
|
|
*/ |
43
|
2 |
|
private function execute(array $files, $errorMessage) |
44
|
|
|
{ |
45
|
2 |
|
$outputMessage = new PreCommitOutputWriter(self::RUNNING_PHPLINT); |
46
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
47
|
|
|
|
48
|
2 |
|
$errors = []; |
49
|
2 |
|
foreach ($files as $file) { |
50
|
2 |
|
$errors[] = $this->phpLintTool->process($file); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$errors = array_filter($errors); |
54
|
|
|
|
55
|
2 |
|
if (!empty($errors)) { |
56
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
57
|
1 |
|
$this->output->writeln($outputMessage->setError(implode('', $errors))); |
58
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
59
|
|
|
|
60
|
1 |
|
throw new PhpLintViolationsException(); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param CommandInterface|PhpLintTool $command |
68
|
|
|
*/ |
69
|
2 |
|
public function handle(CommandInterface $command) |
70
|
|
|
{ |
71
|
2 |
|
$this->execute($command->getFiles(), $command->getErrorMessage()); |
|
|
|
|
72
|
1 |
|
} |
73
|
|
|
} |
74
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: