|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\PhpCsFixer\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\PhpCsFixer\Contract\Exception\PhpCsFixerViolationsException; |
|
10
|
|
|
use PhpGitHooks\Module\PhpCsFixer\Model\PhpCsFixerToolProcessorInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class PhpCsFixerToolHandler implements CommandHandlerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var OutputInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $output; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var PhpCsFixerToolProcessorInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $phpCsFixerToolProcessor; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* PhpCsFixerTool constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param OutputInterface $output |
|
28
|
|
|
* @param PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function __construct(OutputInterface $output, PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->output = $output; |
|
33
|
2 |
|
$this->phpCsFixerToolProcessor = $phpCsFixerToolProcessor; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $files |
|
38
|
|
|
* @param bool $psr0 |
|
39
|
|
|
* @param bool $psr1 |
|
40
|
|
|
* @param bool $psr2 |
|
41
|
|
|
* @param bool $symfony |
|
42
|
|
|
* @param string $options |
|
43
|
|
|
* @param string $errorMessage |
|
44
|
|
|
*/ |
|
45
|
2 |
|
private function execute(array $files, $psr0, $psr1, $psr2, $symfony, $options, $errorMessage) |
|
46
|
|
|
{ |
|
47
|
2 |
|
if (true === $psr0) { |
|
48
|
2 |
|
$this->executeTool($files, 'PSR0', $options, $errorMessage); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
if (true === $psr1) { |
|
52
|
1 |
|
$this->executeTool($files, 'PSR1', $options, $errorMessage); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if (true === $psr2) { |
|
56
|
1 |
|
$this->executeTool($files, 'PSR2', $options, $errorMessage); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
if (true === $symfony) { |
|
60
|
1 |
|
$this->executeTool($files, 'SYMFONY', $options, $errorMessage); |
|
61
|
|
|
} |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $files |
|
66
|
|
|
* @param string $level |
|
67
|
|
|
* @param string $options |
|
68
|
|
|
* @param string $errorMessage |
|
69
|
|
|
* |
|
70
|
|
|
* @throws PhpCsFixerViolationsException |
|
71
|
|
|
*/ |
|
72
|
2 |
|
private function executeTool(array $files, $level, $options, $errorMessage) |
|
73
|
|
|
{ |
|
74
|
2 |
|
$outputMessage = new PreCommitOutputWriter(sprintf('Checking %s code style with PHP-CS-FIXER', $level)); |
|
75
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
|
76
|
|
|
|
|
77
|
2 |
|
$errors = []; |
|
78
|
2 |
|
foreach ($files as $file) { |
|
79
|
2 |
|
$errors[] = $this->phpCsFixerToolProcessor->process($file, $level, $options); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$errors = array_filter($errors); |
|
83
|
|
|
|
|
84
|
2 |
|
if (!empty($errors)) { |
|
85
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
|
86
|
1 |
|
$errorsText = $outputMessage->setError(implode('', $errors)); |
|
87
|
1 |
|
$this->output->writeln($errorsText); |
|
88
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
|
89
|
1 |
|
throw new PhpCsFixerViolationsException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param CommandInterface|PhpCsFixerTool $command |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function handle(CommandInterface $command) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$this->execute( |
|
101
|
2 |
|
$command->getFiles(), |
|
|
|
|
|
|
102
|
2 |
|
$command->isPsr0(), |
|
|
|
|
|
|
103
|
2 |
|
$command->isPsr1(), |
|
|
|
|
|
|
104
|
2 |
|
$command->isPsr2(), |
|
|
|
|
|
|
105
|
2 |
|
$command->isSymfony(), |
|
|
|
|
|
|
106
|
2 |
|
$command->getOptions(), |
|
|
|
|
|
|
107
|
2 |
|
$command->getErrorMessage() |
|
|
|
|
|
|
108
|
|
|
); |
|
109
|
1 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
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: