Passed
Push — master ( 3a6da4...ca4c8c )
by Sebastian
03:44
created

Express::setupPHPUnitHook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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