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
|
|
|
use PhpGitHooks\Infrastructure\Composer\ProcessorInterface; |
10
|
|
|
|
11
|
|
|
class PreQualityToolProcessor extends Processor implements ProcessorInterface |
12
|
|
|
{ |
13
|
|
|
protected $simpleTools = ['composer', 'jsonlint', 'phplint', 'phpmd']; |
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 |
|
$hookData = $this->configData[$this->hookName()]; |
24
|
9 |
|
$this->configMessage($hookData); |
25
|
|
|
|
26
|
9 |
|
$execute = $hookData['execute']; |
27
|
9 |
|
$this->configSimpleTools($execute); |
28
|
9 |
|
$this->configComplexTools($execute); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
return $this->configData; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $configData |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
10 |
View Code Duplication |
protected function enableHook(array $configData) |
|
|
|
|
40
|
|
|
{ |
41
|
10 |
|
if (!isset($configData[$this->hookName()])) { |
42
|
3 |
|
$enable = $this->setQuestion(sprintf('Do you want enable %s hook?', $this->hookName()), '[Y/n]', 'Y'); |
43
|
|
|
|
44
|
3 |
|
$enabled = 'Y' === strtoupper($enable) ? true : false; |
45
|
|
|
|
46
|
3 |
|
$this->configData[$this->hookName()] = [ |
47
|
3 |
|
'enabled' => $enabled, |
48
|
|
|
'execute' => [], |
49
|
|
|
]; |
50
|
|
|
|
51
|
3 |
|
return $enabled; |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
$this->configData = $configData; |
55
|
|
|
|
56
|
7 |
|
return $configData[$this->hookName()]['enabled']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $execute |
61
|
|
|
*/ |
62
|
9 |
|
protected 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
|
9 |
|
$this->configData[$this->hookName()]['execute'][$tool] = 'Y' === strtoupper($answer) ? true : false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
9 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $execute |
74
|
|
|
*/ |
75
|
9 |
|
protected 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 |
|
protected function configPhpCs(array $execute) |
86
|
|
|
{ |
87
|
9 |
|
$phpCsConfig = new PhpCsConfigData($this->io); |
88
|
9 |
|
$this->configData[$this->hookName()]['execute'][PhpCsConfigData::TOOL] = $phpCsConfig |
89
|
9 |
|
->createConfigData($execute); |
90
|
9 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $execute |
94
|
|
|
*/ |
95
|
9 |
|
protected function configPhpCsFixer(array $execute) |
96
|
|
|
{ |
97
|
9 |
|
$phpCsFixerConfig = new PhpCsFixerConfigData($this->io); |
98
|
9 |
|
$this->configData[$this->hookName()]['execute'][PhpCsFixerConfigData::TOOL] = $phpCsFixerConfig |
99
|
9 |
|
->createConfigData($execute); |
100
|
7 |
|
} |
101
|
|
|
|
102
|
7 |
|
protected function configPhpUnit(array $execute) |
103
|
|
|
{ |
104
|
7 |
|
$phpUnitConfig = new PhpUnitConfigData($this->io); |
105
|
7 |
|
$this->configData[$this->hookName()]['execute'][PhpUnitConfigData::TOOL] = $phpUnitConfig |
106
|
7 |
|
->createConfigData($execute); |
107
|
5 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $tool |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
8 |
|
protected 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 |
|
protected function configMessage(array $hookData) |
123
|
|
|
{ |
124
|
9 |
|
$message = new MessageConfigData($this->io, $this->hookName()); |
125
|
9 |
|
$this->configData[$this->hookName()][MessageConfigData::TOOL] = $message->config($hookData); |
126
|
9 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function hookName() |
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.