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