Completed
Push — master ( 30874d...ed8031 )
by Pablo
02:56
created

PreCommitTool   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 158
Duplicated Lines 12.66 %

Coupling/Cohesion

Components 1
Dependencies 22

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 13
c 7
b 0
f 0
lcom 1
cbo 22
dl 20
loc 158
ccs 89
cts 89
cp 1
rs 5.7894

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A execute() 0 21 3
C executeTools() 20 82 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpGitHooks\Module\Git\Service;
4
5
use PhpGitHooks\Infrastructure\CommandBus\CommandBus\CommandBus;
6
use PhpGitHooks\Infrastructure\CommandBus\QueryBus\QueryBus;
7
use PhpGitHooks\Module\Composer\Contract\Command\ComposerToolCommand;
8
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinderQuery;
9
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
10
use PhpGitHooks\Module\Configuration\Contract\Response\PreCommitResponse;
11
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse;
12
use PhpGitHooks\Module\Git\Infrastructure\Files\FilesCommittedExtractor;
13
use PhpGitHooks\Module\Git\Infrastructure\OutputWriter\ToolTittleOutputWriter;
14
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintToolCommand;
15
use PhpGitHooks\Module\PhpCs\Contract\Command\PhpCsToolCommand;
16
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolCommand;
17
use PhpGitHooks\Module\PhpLint\Contract\Command\PhpLintToolCommand;
18
use PhpGitHooks\Module\PhpMd\Contract\Command\PhpMdToolCommand;
19
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand;
20
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class PreCommitTool
24
{
25
    const NO_FILES_CHANGED_MESSAGE = '<comment>No files changed.</comment>';
26
    const TITLE = 'Pre-Commit tool';
27
    /**
28
     * @var OutputInterface
29
     */
30
    private $output;
31
    /**
32
     * @var FilesCommittedExtractor
33
     */
34
    private $filesCommittedExtractor;
35
    /**
36
     * @var CommandBus
37
     */
38
    private $commandBus;
39
    /**
40
     * @var QueryBus
41
     */
42
    private $queryBus;
43
    /**
44
     * @var ToolTittleOutputWriter
45
     */
46
    private $tittleOutputWriter;
47
48
    /**
49
     * PreCommitTool constructor.
50
     *
51
     *
52
     * @param OutputInterface         $output
53
     * @param FilesCommittedExtractor $filesCommittedExtractor
54
     * @param QueryBus                $queryBus
55
     * @param CommandBus              $commandBus
56
     * @param ToolTittleOutputWriter  $tittleOutputWriter
57
     */
58 2
    public function __construct(
59
        OutputInterface $output,
60
        FilesCommittedExtractor $filesCommittedExtractor,
61
        QueryBus $queryBus,
62
        CommandBus $commandBus,
63
        ToolTittleOutputWriter $tittleOutputWriter
64
    ) {
65 2
        $this->filesCommittedExtractor = $filesCommittedExtractor;
66 2
        $this->output = $output;
67 2
        $this->commandBus = $commandBus;
68 2
        $this->queryBus = $queryBus;
69 2
        $this->tittleOutputWriter = $tittleOutputWriter;
70 2
    }
71
72 2
    public function execute()
73
    {
74 2
        $this->tittleOutputWriter->writeTitle(self::TITLE);
75 2
        $committedFiles = $this->filesCommittedExtractor->getFiles();
76
77 2
        if (1 === count($committedFiles)) {
78 1
            $this->output->writeln(static::NO_FILES_CHANGED_MESSAGE);
79
80 1
            return;
81
        }
82
83
        /** @var ConfigurationDataResponse $configurationData */
84 1
        $configurationData = $this->queryBus->handle(new ConfigurationDataFinderQuery());
85 1
        $preCommit = $configurationData->getPreCommit();
86
87 1
        if (true === $preCommit->isPreCommit()) {
88 1
            $this->executeTools($preCommit, $committedFiles);
89 1
        }
90
91 1
        $this->output->writeln(GoodJobLogoResponse::paint($preCommit->getRightMessage()));
0 ignored issues
show
Documentation introduced by
$preCommit->getRightMessage() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92 1
    }
93
94
    /**
95
     * @param PreCommitResponse $preCommitResponse
96
     * @param array             $committedFiles
97
     */
98 1
    private function executeTools(PreCommitResponse $preCommitResponse, array $committedFiles)
99
    {
100 1
        if (true === $preCommitResponse->isComposer()) {
101 1
            $this->commandBus->handle(
102 1
                new ComposerToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
103 1
            );
104 1
        }
105
106 1
        if (true === $preCommitResponse->isJsonLint()) {
107 1
            $this->commandBus->handle(
108 1
                new JsonLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
109 1
            );
110 1
        }
111
112 1
        if (true === $preCommitResponse->isPhpLint()) {
113 1
            $this->commandBus->handle(
114 1
                new PhpLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
115 1
            );
116 1
        }
117
118 1
        $phpCsResponse = $preCommitResponse->getPhpCs();
119
120 1
        if (true === $phpCsResponse->isPhpCs()) {
121 1
            $this->commandBus->handle(
122 1
                new PhpCsToolCommand(
123 1
                    $committedFiles,
124 1
                    $phpCsResponse->getPhpCsStandard(),
125 1
                    $preCommitResponse->getErrorMessage()
126 1
                )
127 1
            );
128 1
        }
129
130 1
        $phpCsFixerResponse = $preCommitResponse->getPhpCsFixer();
131
132 1
        if (true === $phpCsFixerResponse->isPhpCsFixer()) {
133 1
            $this->commandBus->handle(
134 1
                new PhpCsFixerToolCommand(
135 1
                    $committedFiles,
136 1
                    $phpCsFixerResponse->isPhpCsFixerPsr0(),
137 1
                    $phpCsFixerResponse->isPhpCsFixerPsr1(),
138 1
                    $phpCsFixerResponse->isPhpCsFixerPsr2(),
139 1
                    $phpCsFixerResponse->isPhpCsFixerSymfony(),
140 1
                    $preCommitResponse->getErrorMessage()
141 1
                )
142 1
            );
143 1
        }
144
145 1
        $phpMdResponse = $preCommitResponse->getPhpMd();
146
147 1
        if (true === $phpMdResponse->isPhpMd()) {
148 1
            $this->commandBus->handle(
149 1
                new PhpMdToolCommand(
150 1
                    $committedFiles,
151 1
                    $phpMdResponse->getPhpMdOptions(),
152 1
                    $preCommitResponse->getErrorMessage()
153 1
                )
154 1
            );
155 1
        }
156
157 1
        $phpunitResponse = $preCommitResponse->getPhpUnit();
158
159 1 View Code Duplication
        if (true === $phpunitResponse->isPhpunit()) {
0 ignored issues
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...
160 1
            $this->commandBus->handle(
161 1
                new PhpUnitToolCommand(
162 1
                    $phpunitResponse->isPhpunitRandomMode(),
163 1
                    $phpunitResponse->getPhpunitOptions(),
164 1
                    $preCommitResponse->getErrorMessage()
165 1
                )
166 1
            );
167
168 1
            $phpunitStrictCoverageResponse = $preCommitResponse->getPhpUnitStrictCoverage();
169
170 1
            if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) {
171 1
                $this->commandBus->handle(
172 1
                    new StrictCoverageCommand(
173 1
                        $phpunitStrictCoverageResponse->getMinimum(),
174 1
                        $preCommitResponse->getErrorMessage()
175 1
                    )
176 1
                );
177 1
            }
178 1
        }
179 1
    }
180
}
181