|
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
|
1 |
|
} |
|
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
|
1 |
|
); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
if (true === $preCommitResponse->isJsonLint()) { |
|
110
|
1 |
|
$this->commandBus->handle( |
|
111
|
1 |
|
new JsonLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage()) |
|
112
|
1 |
|
); |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
$phpFiles = $this->getPhpFiles($committedFiles); |
|
116
|
|
|
|
|
117
|
1 |
|
if ($phpFiles) { |
|
|
|
|
|
|
118
|
1 |
|
if (true === $preCommitResponse->isPhpLint()) { |
|
119
|
1 |
|
$this->commandBus->handle( |
|
120
|
1 |
|
new PhpLintToolCommand($phpFiles, $preCommitResponse->getErrorMessage()) |
|
121
|
1 |
|
); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
$phpCsResponse = $preCommitResponse->getPhpCs(); |
|
125
|
|
|
|
|
126
|
1 |
|
if (true === $phpCsResponse->isPhpCs()) { |
|
127
|
1 |
|
$this->commandBus->handle( |
|
128
|
1 |
|
new PhpCsToolCommand( |
|
129
|
1 |
|
$phpFiles, |
|
130
|
1 |
|
$phpCsResponse->getPhpCsStandard(), |
|
131
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
132
|
1 |
|
) |
|
133
|
1 |
|
); |
|
134
|
1 |
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
$phpCsFixerResponse = $preCommitResponse->getPhpCsFixer(); |
|
137
|
|
|
|
|
138
|
1 |
|
if (true === $phpCsFixerResponse->isPhpCsFixer()) { |
|
139
|
1 |
|
$this->commandBus->handle( |
|
140
|
1 |
|
new PhpCsFixerToolCommand( |
|
141
|
1 |
|
$phpFiles, |
|
142
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr0(), |
|
143
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr1(), |
|
144
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerPsr2(), |
|
145
|
1 |
|
$phpCsFixerResponse->isPhpCsFixerSymfony(), |
|
146
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
147
|
1 |
|
) |
|
148
|
1 |
|
); |
|
149
|
1 |
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
$phpMdResponse = $preCommitResponse->getPhpMd(); |
|
152
|
|
|
|
|
153
|
1 |
|
if (true === $phpMdResponse->isPhpMd()) { |
|
154
|
1 |
|
$this->commandBus->handle( |
|
155
|
1 |
|
new PhpMdToolCommand( |
|
156
|
1 |
|
$phpFiles, |
|
157
|
1 |
|
$phpMdResponse->getPhpMdOptions(), |
|
158
|
1 |
|
$preCommitResponse->getErrorMessage() |
|
159
|
1 |
|
) |
|
160
|
1 |
|
); |
|
161
|
1 |
|
} |
|
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
|
1 |
|
) |
|
172
|
1 |
|
); |
|
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
|
1 |
|
) |
|
182
|
1 |
|
); |
|
183
|
1 |
|
} |
|
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
|
1 |
|
) |
|
192
|
1 |
|
); |
|
193
|
1 |
|
} |
|
194
|
1 |
|
} |
|
195
|
1 |
|
} |
|
196
|
1 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param array $committedFiles |
|
200
|
|
|
* |
|
201
|
|
|
* @return array |
|
202
|
|
|
*/ |
|
203
|
1 |
|
private function getPhpFiles(array $committedFiles) |
|
204
|
|
|
{ |
|
205
|
|
|
/** @var PhpFilesResponse $phpFilesResponse */ |
|
206
|
1 |
|
$phpFilesResponse = $this->queryBus->handle(new PhpFilesExtractorQuery($committedFiles)); |
|
207
|
|
|
|
|
208
|
1 |
|
return $phpFilesResponse->getFiles(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
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.