1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of CaptainHook. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\CaptainHook\Runner\Configurator\Setup; |
11
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Config; |
13
|
|
|
use SebastianFeldmann\CaptainHook\Console\IOUtil; |
14
|
|
|
use SebastianFeldmann\CaptainHook\Runner\Configurator\Setup; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Express |
18
|
|
|
* |
19
|
|
|
* @package CaptainHook |
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
21
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
22
|
|
|
* @since Class available since Release 2.2.0 |
23
|
|
|
*/ |
24
|
|
|
class Express extends Guided implements Setup |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Setup hooks by asking some basic questions |
28
|
|
|
* |
29
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config $config |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
3 |
|
public function configureHooks(Config $config) |
33
|
|
|
{ |
34
|
3 |
|
$msgHook = $config->getHookConfig('commit-msg'); |
35
|
3 |
|
$preHook = $config->getHookConfig('pre-commit'); |
36
|
3 |
|
$msgHook->setEnabled(true); |
37
|
3 |
|
$preHook->setEnabled(true); |
38
|
|
|
|
39
|
3 |
|
$this->setupMessageHook($msgHook); |
40
|
3 |
|
$this->setupPHPLintingHook($preHook); |
41
|
3 |
|
$this->setupPHPUnitHook($preHook); |
42
|
3 |
|
$this->setupPHPCodesnifferHook($preHook); |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Setup the commit message hook |
47
|
|
|
* |
48
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
3 |
|
private function setupMessageHook(Config\Hook $config) |
52
|
|
|
{ |
53
|
3 |
|
$answer = $this->io->ask( |
54
|
3 |
|
' <info>Do you want to validate your commit messages?</info> <comment>[y,n]</comment> ', |
55
|
3 |
|
'n' |
56
|
|
|
); |
57
|
|
|
|
58
|
3 |
|
if (IOUtil::answerToBool($answer)) { |
59
|
1 |
|
$type = 'php'; |
60
|
1 |
|
$call = '\\SebastianFeldmann\\CaptainHook\\Hook\\Message\\Action\\Beams'; |
61
|
1 |
|
$options = ['subjectLength' => 50, 'bodyLineLength' => 72]; |
62
|
1 |
|
$config->addAction(new Config\Action($type, $call, $options)); |
63
|
|
|
} |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Setup the linting hook |
68
|
|
|
* |
69
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
70
|
|
|
* @throws \Exception |
71
|
|
|
*/ |
72
|
3 |
View Code Duplication |
private function setupPHPLintingHook(Config\Hook $config) |
|
|
|
|
73
|
|
|
{ |
74
|
3 |
|
$answer = $this->io->ask( |
75
|
3 |
|
' <info>Do you want to check your files for syntax errors?</info> <comment>[y,n]</comment> ', |
76
|
3 |
|
'n' |
77
|
|
|
); |
78
|
|
|
|
79
|
3 |
|
if (IOUtil::answerToBool($answer)) { |
80
|
1 |
|
$type = 'php'; |
81
|
1 |
|
$call = '\\SebastianFeldmann\\CaptainHook\\Hook\\PHP\\Action\\Linting'; |
82
|
1 |
|
$config->addAction(new Config\Action($type, $call)); |
83
|
|
|
} |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Setup the phpunit hook |
88
|
|
|
* |
89
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
3 |
View Code Duplication |
private function setupPHPUnitHook(Config\Hook $config) |
|
|
|
|
93
|
|
|
{ |
94
|
3 |
|
$answer = $this->io->ask( |
95
|
3 |
|
' <info>Do you want to run phpunit before committing?</info> <comment>[y,n]</comment> ', |
96
|
3 |
|
'n' |
97
|
|
|
); |
98
|
|
|
|
99
|
3 |
|
if (IOUtil::answerToBool($answer)) { |
100
|
1 |
|
$type = 'cli'; |
101
|
1 |
|
$call = $this->io->ask( |
102
|
|
|
' <info>Enter the phpunit command you want to execute.</info> ' |
103
|
1 |
|
. '<comment>[phpunit]</comment> ', 'phpunit'); |
104
|
1 |
|
$config->addAction(new Config\Action($type, $call)); |
105
|
|
|
} |
106
|
3 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Setup the code sniffer hook |
110
|
|
|
* |
111
|
|
|
* @param \SebastianFeldmann\CaptainHook\Config\Hook $config |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
3 |
View Code Duplication |
private function setupPHPCodesnifferHook(Config\Hook $config) |
|
|
|
|
115
|
|
|
{ |
116
|
3 |
|
$answer = $this->io->ask( |
117
|
3 |
|
' <info>Do you want to run phpcs before committing?</info> <comment>[y,n]</comment> ', |
118
|
3 |
|
'n' |
119
|
|
|
); |
120
|
|
|
|
121
|
3 |
|
if (IOUtil::answerToBool($answer)) { |
122
|
1 |
|
$type = 'cli'; |
123
|
1 |
|
$call = $this->io->ask( |
124
|
|
|
' <info>Enter the phpcs command you want to execute.</info> ' |
125
|
1 |
|
. '<comment>[phpcs --standard=psr2 src]</comment> ', |
126
|
1 |
|
'phpcs --standard=psr2 src' |
127
|
|
|
); |
128
|
1 |
|
$config->addAction(new Config\Action($type, $call)); |
129
|
|
|
} |
130
|
3 |
|
} |
131
|
|
|
} |
132
|
|
|
|
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.