Completed
Push — master ( f037e1...e766b9 )
by Billie
25s
created

Scripts::installGitMessageHook()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 8.7592
cc 6
eloc 34
nc 10
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
namespace PurpleBooth\GitLintValidators\Composer;
4
5
use Composer\Script\Event;
6
7
class Scripts
8
{
9
10
    /**
11
     * Installs and activates the Git commit message
12
     * hook when confirmed by the user. An existing hook
13
     * is backed with the .bak extension.
14
     *
15
     *
16
     * @param  Event $event The script event.
17
     * @return boolean
18
     */
19
    public static function installGitMessageHook(Event $event)
20
    {
21
        $gitHookContent = <<<CONTENT
22
#!/bin/sh
23
24
vendor/bin/git-lint-validators git-lint-validator:hook $1
25
CONTENT;
26
27
        $io = $event->getIO();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
28
        $question = "Do you want to install and activate the Git "
29
            . "commit message hook? ";
30
31
        if ($io->askConfirmation($question, false)) {
32
            $gitDirectory = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . '.git';
33
            if (!is_dir($gitDirectory)) {
34
                $errorMessage = "Couldn't locate the .git directory. "
35
                    . "Aborting the Git hook installation.";
36
                $io->error($errorMessage);
37
38
                return false;
39
            }
40
41
            $gitCommitMessageHookFile = $gitDirectory
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $gitCommitMessageHookFile exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
42
                . DIRECTORY_SEPARATOR . 'hooks'
43
                . DIRECTORY_SEPARATOR . 'commit-msg';
44
45
            $backedExistingGitCommitMessageHookFile = false;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $backedExistingGitCommitMessageHookFile exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
46
            if (file_exists($gitCommitMessageHookFile)) {
47
                $backedExistingGitCommitMessageHookFile = copy(
48
                    $gitCommitMessageHookFile,
49
                    $gitCommitMessageHookFile . '.bak'
50
                );
51
            }
52
53
            file_put_contents($gitCommitMessageHookFile, $gitHookContent);
54
            chmod($gitCommitMessageHookFile, 0751);
55
56
            $io->write("Installed and activated the Git commit message hook.");
57
58
            if ($backedExistingGitCommitMessageHookFile) {
59
                $io->write("Backed previous Git commit message hook.");
60
            }
61
62
            if ($io->isVerbose()) {
63
                $io->write("Wrote");
64
                $io->write("<comment>$gitHookContent</comment>");
65
                $io->write("into <info>$gitCommitMessageHookFile</info> and made it executable.");
66
            }
67
68
            return true;
69
        }
70
71
        $io->write("Aborted installation and activation of the Git commit message hook.");
72
73
        return false;
74
    }
75
}
76