1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Application\Composer; |
4
|
|
|
|
5
|
|
|
use PhpGitHooks\Application\CodeSniffer\PhpCsConfigData; |
6
|
|
|
use PhpGitHooks\Application\Message\MessageConfigData; |
7
|
|
|
use PhpGitHooks\Application\PhpCsFixer\PhpCsFixerConfigData; |
8
|
|
|
use PhpGitHooks\Application\PhpUnit\PhpUnitConfigData; |
9
|
|
|
|
10
|
|
|
final class PreCommitProcessor extends Processor |
11
|
|
|
{ |
12
|
|
|
private $simpleTools = ['composer', 'jsonlint', 'phplint', 'phpmd']; |
13
|
|
|
const HOOK_NAME = 'pre-commit'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $configData |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
10 |
|
public function execute(array $configData) |
21
|
|
|
{ |
22
|
10 |
|
if (true === $this->enableHook($configData)) { |
23
|
9 |
|
$preCommit = $this->configData[self::HOOK_NAME]; |
24
|
9 |
|
$this->configMessage($preCommit); |
25
|
|
|
|
26
|
9 |
|
$execute = $preCommit['execute']; |
27
|
9 |
|
$this->configSimpleTools($execute); |
28
|
9 |
|
$this->configComplexTools($execute); |
29
|
5 |
|
} |
30
|
|
|
|
31
|
6 |
|
return $this->configData; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $configData |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
10 |
View Code Duplication |
private function enableHook(array $configData) |
|
|
|
|
40
|
|
|
{ |
41
|
10 |
|
if (!isset($configData[self::HOOK_NAME])) { |
42
|
3 |
|
$enable = $this->setQuestion('Do you want enable pre-commit hook?', '[Y/n]', 'Y'); |
43
|
|
|
|
44
|
3 |
|
$enabled = 'Y' === strtoupper($enable) ? true : false; |
45
|
|
|
|
46
|
3 |
|
$this->configData[self::HOOK_NAME] = [ |
47
|
3 |
|
'enabled' => $enabled, |
48
|
3 |
|
'execute' => [], |
49
|
|
|
]; |
50
|
|
|
|
51
|
3 |
|
return $enabled; |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
$this->configData = $configData; |
55
|
|
|
|
56
|
7 |
|
return $configData[self::HOOK_NAME]['enabled']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $execute |
61
|
|
|
*/ |
62
|
9 |
|
private function configSimpleTools(array $execute) |
63
|
|
|
{ |
64
|
9 |
|
foreach ($this->simpleTools as $tool) { |
65
|
9 |
|
if (!isset($execute[$tool])) { |
66
|
8 |
|
$answer = $this->setQuestionTool($tool); |
67
|
8 |
|
$this->configData[self::HOOK_NAME]['execute'][$tool] = 'Y' === strtoupper($answer) ? true : false; |
68
|
8 |
|
} |
69
|
9 |
|
} |
70
|
9 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $execute |
74
|
|
|
*/ |
75
|
9 |
|
private function configComplexTools(array $execute) |
76
|
|
|
{ |
77
|
9 |
|
$this->configPhpCs($execute); |
78
|
9 |
|
$this->configPhpCsFixer($execute); |
79
|
7 |
|
$this->configPhpUnit($execute); |
80
|
5 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $execute |
84
|
|
|
*/ |
85
|
9 |
|
private function configPhpCs(array $execute) |
86
|
|
|
{ |
87
|
9 |
|
$phpCsConfig = new PhpCsConfigData($this->io); |
88
|
9 |
|
$this->configData[self::HOOK_NAME]['execute'][PhpCsConfigData::TOOL] = $phpCsConfig |
89
|
9 |
|
->createConfigData($execute); |
90
|
9 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $execute |
94
|
|
|
*/ |
95
|
9 |
|
private function configPhpCsFixer(array $execute) |
96
|
|
|
{ |
97
|
9 |
|
$phpCsFixerConfig = new PhpCsFixerConfigData($this->io); |
98
|
9 |
|
$this->configData[self::HOOK_NAME]['execute'][PhpCsFixerConfigData::TOOL] = $phpCsFixerConfig |
99
|
9 |
|
->createConfigData($execute); |
100
|
7 |
|
} |
101
|
|
|
|
102
|
7 |
|
private function configPhpUnit(array $execute) |
103
|
|
|
{ |
104
|
7 |
|
$phpUnitConfig = new PhpUnitConfigData($this->io); |
105
|
7 |
|
$this->configData[self::HOOK_NAME]['execute'][PhpUnitConfigData::TOOL] = $phpUnitConfig |
106
|
7 |
|
->createConfigData($execute); |
107
|
5 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $tool |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
8 |
|
private function setQuestionTool($tool) |
115
|
|
|
{ |
116
|
8 |
|
return $this->setQuestion(sprintf('Do you want enable %s tool?', strtoupper($tool)), '[Y/n]', 'Y'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $hookData |
121
|
|
|
*/ |
122
|
9 |
|
private function configMessage(array $hookData) |
123
|
|
|
{ |
124
|
9 |
|
$message = new MessageConfigData($this->io, self::HOOK_NAME); |
125
|
9 |
|
$this->configData[self::HOOK_NAME][MessageConfigData::TOOL] = $message->config($hookData); |
126
|
9 |
|
} |
127
|
|
|
} |
128
|
|
|
|
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.