Failed Conditions
Pull Request — master (#15)
by Raphael
08:54
created

Scripts::installGitMessageHook()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 30

Duplication

Lines 8
Ratio 15.69 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 8
loc 51
rs 8.6588
cc 6
eloc 30
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
    const BACKUP_EXTENSION  = '.bak';
10
    const GIT_PATH          = '.git';
11
    const HOOKS_PATH        = 'hooks';
12
    const HOOK_FILENAME     = 'commit-msg';
13
    const TEMPLATE_FILENAME = '.gitmessage';
14
15
    /**
16
     * Default Permissions (Copied from example hooks)
17
     *
18
     * User: Read, Write, Execute
19
     * Group: Read, Execute
20
     * Other: Execute
21
     */
22
    const EXECUTABLE_PERMISSIONS = 0751;
23
    const HOOK_CONTENTS          = <<<CONTENT
24
#!/bin/sh
25
26
vendor/bin/git-lint-validators git-lint-validator:hook $1
27
CONTENT;
28
29
    const TEMPLATE_CONTENTS      = <<<CONTENT
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 6 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

Loading history...
30
Subject line
31
32
# - Capitalise the subject line and do not end it with a period
33
# - Use the imperative mood in the subject line
34
# - Summarise changes in around 50 (soft limit, hard limit at 69)
35
#   characters or less in the subject line
36
# - Separate subject line from body with a blank line
37
Subject body
38
# - Use the subject body to explain what and why vs. how
39
# - Wrap the subject body at 72 characters
40
41
CONTENT;
42
43
    /**
44
     * Installs and activates the Git commit message
45
     * hook when confirmed by the user. An existing hook
46
     * is backed with the .bak extension.
47
     *
48
     * @param  Event $event The script event.
49
     *
50
     * @return boolean
51
     */
52
    public static function installGitMessageHook(Event $event)
53
    {
54
        $hookContent = self::HOOK_CONTENTS;
55
        $inputOutput = $event->getIO();
56
        $question    = "Do you want to install and activate the Git ";
57
        $question   .= "commit message hook? ";
58
59
        if ($inputOutput->askConfirmation($question, false)) {
60
            $gitDirectory = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 4), self::GIT_PATH]);
61
62 View Code Duplication
            if (!is_dir($gitDirectory)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
63
                $errorMessage  = "Couldn't locate the .git directory. ";
64
                $errorMessage .= "Aborting the Git hook installation.";
65
66
                $inputOutput->error($errorMessage);
67
68
                return false;
69
            }
70
71
            $hookFile         = implode(DIRECTORY_SEPARATOR, [$gitDirectory, self::HOOKS_PATH, self::HOOK_FILENAME]);
72
            $existingHookFile = false;
73
74
            if (file_exists($hookFile)) {
75
                $existingHookFile = copy(
76
                    $hookFile,
77
                    $hookFile . self::BACKUP_EXTENSION
78
                );
79
            }
80
81
            file_put_contents($hookFile, $hookContent);
82
            chmod($hookFile, self::EXECUTABLE_PERMISSIONS);
83
84
            $inputOutput->write("Installed and activated the Git commit message hook.");
85
86
            if ($existingHookFile) {
87
                $inputOutput->write("Backed previous Git commit message hook.");
88
            }
89
90
            if ($inputOutput->isVerbose()) {
91
                $inputOutput->write("Wrote");
92
                $inputOutput->write("<comment>$hookContent</comment>");
93
                $inputOutput->write("into <info>$hookFile</info> and made it executable.");
94
            }
95
96
            return true;
97
        }
98
99
        $inputOutput->write("Aborted installation and activation of the Git commit message hook.");
100
101
        return false;
102
    }
103
104
    /**
105
     * Installs and configures the Git commit message
106
     * template when confirmed by the user.
107
     *
108
     * @param  Event $event The script event.
109
     *
110
     * @return boolean
111
     */
112
    public static function installGitCommitMessageTemplate(Event $event)
1 ignored issue
show
Coding Style introduced by
function installGitCommitMessageTemplate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
113
    {
114
        $templateContent = self::TEMPLATE_CONTENTS;
115
        $inputOutput = $event->getIO();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
116
        $question    = "Do you want to install and configure the Git ";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 4 spaces

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...
117
        $question   .= "commit message template? ";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 3 spaces

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...
118
119
        if ($inputOutput->askConfirmation($question, false)) {
120
            $gitDirectory = implode(DIRECTORY_SEPARATOR, [dirname(__DIR__, 4), self::GIT_PATH]);
121
122 View Code Duplication
            if (!is_dir($gitDirectory)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
123
                $errorMessage  = "Couldn't locate the .git directory. ";
124
                $errorMessage .= "Aborting the Git commit message template installation.";
125
126
                $inputOutput->error($errorMessage);
127
128
                return false;
129
            }
130
131
            $templateFile = implode(DIRECTORY_SEPARATOR, [$gitDirectory, self::TEMPLATE_FILENAME]);
132
133
            file_put_contents($templateFile, $templateContent);
134
            $gitConfigureCommand = "git config --add commit.template $templateFile";
135
            exec($gitConfigureCommand);
136
137
            $inputOutput->write("Installed and configured the Git commit message template.");
138
139
            if ($inputOutput->isVerbose()) {
140
                $inputOutput->write("Wrote");
141
                $inputOutput->write("<comment>$templateContent</comment>");
142
                $inputOutput->write("into <info>$templateFile</info> and configured the Git commit message template ");
143
                $inputOutput->write("via <comment>$gitConfigureCommand</comment>.");
144
            }
145
146
            return true;
147
        }
148
149
        $inputOutput->write("Aborted installation and configuration of the Git commit message template.");
150
151
        return false;
152
    }
153
}
154