|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\Git\Contract\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandBus; |
|
6
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface; |
|
7
|
|
|
use Bruli\EventBusBundle\CommandBus\CommandInterface; |
|
8
|
|
|
use Bruli\EventBusBundle\QueryBus\QueryBus; |
|
9
|
|
|
use PhpGitHooks\Module\Composer\Contract\Command\ComposerTool; |
|
10
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder; |
|
11
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse; |
|
12
|
|
|
use PhpGitHooks\Module\Configuration\Contract\Response\PreCommitResponse; |
|
13
|
|
|
use PhpGitHooks\Module\Files\Contract\Query\PhpFilesExtractor; |
|
14
|
|
|
use PhpGitHooks\Module\Files\Contract\Response\PhpFilesResponse; |
|
15
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse; |
|
16
|
|
|
use PhpGitHooks\Module\Git\Infrastructure\Files\FilesCommittedExtractor; |
|
17
|
|
|
use PhpGitHooks\Module\Git\Infrastructure\OutputWriter\ToolTittleOutputWriter; |
|
18
|
|
|
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintToolCommand; |
|
19
|
|
|
use PhpGitHooks\Module\PhpCs\Contract\Command\PhpCsToolCommand; |
|
20
|
|
|
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolCommand; |
|
21
|
|
|
use PhpGitHooks\Module\PhpLint\Contract\Command\PhpLintToolCommand; |
|
22
|
|
|
use PhpGitHooks\Module\PhpMd\Contract\Command\PhpMdToolCommand; |
|
23
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand; |
|
24
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand; |
|
25
|
|
|
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand; |
|
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
27
|
|
|
|
|
28
|
|
|
class PreCommitToolHandler implements CommandHandlerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
const NO_FILES_CHANGED_MESSAGE = '<comment>-\_(ö)_/- No files changed.</comment>'; |
|
31
|
|
|
const TITLE = 'Pre-Commit tool'; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var OutputInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $output; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var FilesCommittedExtractor |
|
38
|
|
|
*/ |
|
39
|
|
|
private $filesCommittedExtractor; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var CommandBus |
|
42
|
|
|
*/ |
|
43
|
|
|
private $commandBus; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var QueryBus |
|
46
|
|
|
*/ |
|
47
|
|
|
private $queryBus; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var ToolTittleOutputWriter |
|
50
|
|
|
*/ |
|
51
|
|
|
private $tittleOutputWriter; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* PreCommitTool constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param OutputInterface $output |
|
57
|
|
|
* @param FilesCommittedExtractor $filesCommittedExtractor |
|
58
|
|
|
* @param QueryBus $queryBus |
|
59
|
|
|
* @param CommandBus $commandBus |
|
60
|
|
|
* @param ToolTittleOutputWriter $tittleOutputWriter |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function __construct( |
|
63
|
|
|
OutputInterface $output, |
|
64
|
|
|
FilesCommittedExtractor $filesCommittedExtractor, |
|
65
|
|
|
QueryBus $queryBus, |
|
66
|
|
|
CommandBus $commandBus, |
|
67
|
|
|
ToolTittleOutputWriter $tittleOutputWriter |
|
68
|
|
|
) { |
|
69
|
2 |
|
$this->filesCommittedExtractor = $filesCommittedExtractor; |
|
70
|
2 |
|
$this->output = $output; |
|
71
|
2 |
|
$this->commandBus = $commandBus; |
|
72
|
2 |
|
$this->queryBus = $queryBus; |
|
73
|
2 |
|
$this->tittleOutputWriter = $tittleOutputWriter; |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
private function execute() |
|
77
|
|
|
{ |
|
78
|
2 |
|
$this->tittleOutputWriter->writeTitle(self::TITLE); |
|
79
|
2 |
|
$committedFiles = $this->filesCommittedExtractor->getFiles(); |
|
80
|
|
|
|
|
81
|
2 |
|
if (1 === count($committedFiles)) { |
|
82
|
1 |
|
$this->output->writeln(static::NO_FILES_CHANGED_MESSAGE); |
|
83
|
|
|
|
|
84
|
1 |
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var ConfigurationDataResponse |
|
89
|
|
|
*/ |
|
90
|
1 |
|
$configurationData = $this->queryBus->handle(new ConfigurationDataFinder()); |
|
91
|
1 |
|
$preCommit = $configurationData->getPreCommit(); |
|
92
|
|
|
|
|
93
|
1 |
|
if (true === $preCommit->isPreCommit()) { |
|
94
|
1 |
|
$this->executeTools($preCommit, $committedFiles); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
$this->output->writeln(GoodJobLogoResponse::paint($preCommit->getRightMessage())); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param PreCommitResponse $preCommitResponse |
|
102
|
|
|
* @param array $committedFiles |
|
103
|
|
|
*/ |
|
104
|
1 |
|
private function executeTools(PreCommitResponse $preCommitResponse, array $committedFiles) |
|
105
|
|
|
{ |
|
106
|
1 |
|
if (true === $preCommitResponse->isComposer()) { |
|
107
|
1 |
|
$this->commandBus->handle( |
|
108
|
1 |
|
new ComposerTool($committedFiles, $preCommitResponse->getErrorMessage()) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
if (true === $preCommitResponse->isJsonLint()) { |
|
113
|
1 |
|
$this->commandBus->handle( |
|
114
|
1 |
|
new JsonLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage()) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
$phpFiles = $this->getPhpFiles($committedFiles); |
|
119
|
|
|
|
|
120
|
1 |
|
if ($phpFiles) { |
|
|
|
|
|
|
121
|
1 |
|
if (true === $preCommitResponse->isPhpLint()) { |
|
122
|
1 |
|
$this->commandBus->handle( |
|
123
|
1 |
|
new PhpLintToolCommand($phpFiles, $preCommitResponse->getErrorMessage()) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
$phpCsResponse = $preCommitResponse->getPhpCs(); |
|
128
|
|
|
|
|
129
|
1 |
|
if (true === $phpCsResponse->isPhpCs()) { |
|
130
|
1 |
|
$this->commandBus->handle( |
|
131
|
1 |
|
new PhpCsToolCommand( |
|
132
|
|
|
$phpFiles, |
|
133
|
1 |
|
$phpCsResponse->getPhpCsStandard(), |
|
134
|
1 |
|
$preCommitResponse->getErrorMessage(), |
|
135
|
1 |
|
$phpCsResponse->getIgnore() |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
$phpCsFixerResponse = $preCommitResponse->getPhpCsFixer(); |
|
141
|
|
|
|
|
142
|
1 |
|
if (true === $phpCsFixerResponse->isPhpCsFixer()) { |
|
143
|
1 |
|
$this->commandBus->handle( |
|
144
|
1 |
|
new PhpCsFixerToolCommand( |
|
145
|
|
|
$phpFiles, |
|
146
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr0(), |
|
147
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr1(), |
|
148
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr2(), |
|
149
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerSymfony(), |
|
150
|
1 |
|
$phpCsFixerResponse->getPhpCsFixerOptions(), |
|
151
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
152
|
|
|
) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
$phpMdResponse = $preCommitResponse->getPhpMd(); |
|
157
|
|
|
|
|
158
|
1 |
|
if (true === $phpMdResponse->isPhpMd()) { |
|
159
|
1 |
|
$this->commandBus->handle( |
|
160
|
1 |
|
new PhpMdToolCommand( |
|
161
|
|
|
$phpFiles, |
|
162
|
1 |
|
$phpMdResponse->getPhpMdOptions(), |
|
163
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
164
|
|
|
) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
$phpunitResponse = $preCommitResponse->getPhpUnit(); |
|
169
|
|
|
|
|
170
|
1 |
View Code Duplication |
if (true === $phpunitResponse->isPhpunit()) { |
|
|
|
|
|
|
171
|
1 |
|
$this->commandBus->handle( |
|
172
|
1 |
|
new PhpUnitToolCommand( |
|
173
|
1 |
|
$phpunitResponse->isPhpunitRandomMode(), |
|
174
|
1 |
|
$phpunitResponse->getPhpunitOptions(), |
|
175
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
176
|
|
|
) |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
1 |
|
$phpunitStrictCoverageResponse = $preCommitResponse->getPhpUnitStrictCoverage(); |
|
180
|
|
|
|
|
181
|
1 |
|
if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) { |
|
182
|
1 |
|
$this->commandBus->handle( |
|
183
|
1 |
|
new StrictCoverageCommand( |
|
184
|
1 |
|
$phpunitStrictCoverageResponse->getMinimum(), |
|
185
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
186
|
|
|
) |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
$phpunitGuardCoverageResponse = $preCommitResponse->getPhpUnitGuardCoverage(); |
|
191
|
|
|
|
|
192
|
1 |
|
if (true === $phpunitGuardCoverageResponse->isEnabled()) { |
|
193
|
1 |
|
$this->commandBus->handle( |
|
194
|
1 |
|
new GuardCoverageCommand( |
|
195
|
1 |
|
$phpunitGuardCoverageResponse->getWarningMessage() |
|
196
|
|
|
) |
|
197
|
|
|
); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
1 |
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param array $committedFiles |
|
205
|
|
|
* |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
1 |
|
private function getPhpFiles(array $committedFiles) |
|
209
|
|
|
{ |
|
210
|
|
|
/** |
|
211
|
|
|
* @var PhpFilesResponse |
|
212
|
|
|
*/ |
|
213
|
1 |
|
$phpFilesResponse = $this->queryBus->handle(new PhpFilesExtractor($committedFiles)); |
|
214
|
|
|
|
|
215
|
1 |
|
return $phpFilesResponse->getFiles(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param CommandInterface $command |
|
220
|
|
|
*/ |
|
221
|
2 |
|
public function handle(CommandInterface $command) |
|
222
|
|
|
{ |
|
223
|
2 |
|
$this->execute(); |
|
224
|
2 |
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.