Completed
Push — master ( efc44e...835840 )
by Pablo
02:59
created

PreCommitToolHandler::executeTools()   F

Complexity

Conditions 11
Paths 324

Size

Total Lines 98
Code Lines 56

Duplication

Lines 30
Ratio 30.61 %

Code Coverage

Tests 54
CRAP Score 11.0176

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 30
loc 98
rs 3.8181
ccs 54
cts 57
cp 0.9474
cc 11
eloc 56
nc 324
nop 2
crap 11.0176

How to fix   Long Method    Complexity   

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
3
namespace PhpGitHooks\Module\Git\Contract\Command;
4
5
use Bruli\EventBusBundle\CommandBus\CommandBus;
6
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface;
7
use Bruli\EventBusBundle\CommandBus\CommandInterface;
8
use Bruli\EventBusBundle\QueryBus\QueryBus;
9
use PhpGitHooks\Module\Composer\Contract\Command\ComposerTool;
10
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder;
11
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
12
use PhpGitHooks\Module\Configuration\Contract\Response\PreCommitResponse;
13
use PhpGitHooks\Module\Files\Contract\Query\PhpFilesExtractor;
14
use PhpGitHooks\Module\Files\Contract\Response\PhpFilesResponse;
15
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse;
16
use PhpGitHooks\Module\Git\Infrastructure\Files\FilesCommittedExtractor;
17
use PhpGitHooks\Module\Git\Infrastructure\OutputWriter\ToolTittleOutputWriter;
18
use PhpGitHooks\Module\JsonLint\Contract\Command\JsonLintToolCommand;
19
use PhpGitHooks\Module\PhpCs\Contract\Command\PhpCsToolCommand;
20
use PhpGitHooks\Module\PhpCsFixer\Contract\Command\PhpCsFixerToolCommand;
21
use PhpGitHooks\Module\PhpLint\Contract\Command\PhpLintToolCommand;
22
use PhpGitHooks\Module\PhpMd\Contract\Command\PhpMdToolCommand;
23
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand;
24
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand;
25
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class PreCommitToolHandler implements CommandHandlerInterface
29
{
30
    const NO_FILES_CHANGED_MESSAGE = '<comment>-\_(ö)_/- No files changed.</comment>';
31
    const TITLE = 'Pre-Commit tool';
32
    /**
33
     * @var OutputInterface
34
     */
35
    private $output;
36
    /**
37
     * @var FilesCommittedExtractor
38
     */
39
    private $filesCommittedExtractor;
40
    /**
41
     * @var CommandBus
42
     */
43
    private $commandBus;
44
    /**
45
     * @var QueryBus
46
     */
47
    private $queryBus;
48
    /**
49
     * @var ToolTittleOutputWriter
50
     */
51
    private $tittleOutputWriter;
52
53
    /**
54
     * PreCommitTool constructor.
55
     *
56
     * @param OutputInterface         $output
57
     * @param FilesCommittedExtractor $filesCommittedExtractor
58
     * @param QueryBus                $queryBus
59
     * @param CommandBus              $commandBus
60
     * @param ToolTittleOutputWriter  $tittleOutputWriter
61
     */
62 2
    public function __construct(
63
        OutputInterface $output,
64
        FilesCommittedExtractor $filesCommittedExtractor,
65
        QueryBus $queryBus,
66
        CommandBus $commandBus,
67
        ToolTittleOutputWriter $tittleOutputWriter
68
    ) {
69 2
        $this->filesCommittedExtractor = $filesCommittedExtractor;
70 2
        $this->output = $output;
71 2
        $this->commandBus = $commandBus;
72 2
        $this->queryBus = $queryBus;
73 2
        $this->tittleOutputWriter = $tittleOutputWriter;
74 2
    }
75
76 2
    private function execute()
77
    {
78 2
        $this->tittleOutputWriter->writeTitle(self::TITLE);
79 2
        $committedFiles = $this->filesCommittedExtractor->getFiles();
80
81 2
        if (1 === count($committedFiles)) {
82 1
            $this->output->writeln(static::NO_FILES_CHANGED_MESSAGE);
83
84 1
            return;
85
        }
86
87
        /**
88
         * @var ConfigurationDataResponse
89
         */
90 1
        $configurationData = $this->queryBus->handle(new ConfigurationDataFinder());
91 1
        $preCommit = $configurationData->getPreCommit();
92
93 1
        if (true === $preCommit->isPreCommit()) {
94 1
            $this->executeTools($preCommit, $committedFiles);
95
        }
96
97 1
        $this->output->writeln(GoodJobLogoResponse::paint($preCommit->getRightMessage()));
98 1
    }
99
100
    /**
101
     * @param PreCommitResponse $preCommitResponse
102
     * @param array             $committedFiles
103
     */
104 1
    private function executeTools(PreCommitResponse $preCommitResponse, array $committedFiles)
105
    {
106 1
        if (true === $preCommitResponse->isComposer()) {
107 1
            $this->commandBus->handle(
108 1
                new ComposerTool($committedFiles, $preCommitResponse->getErrorMessage())
109
            );
110
        }
111
112 1
        if (true === $preCommitResponse->isJsonLint()) {
113 1
            $this->commandBus->handle(
114 1
                new JsonLintToolCommand($committedFiles, $preCommitResponse->getErrorMessage())
115
            );
116
        }
117
118 1
        $phpFiles = $this->getPhpFiles($committedFiles);
119
120 1
        if ($phpFiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $phpFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121 1
            if (true === $preCommitResponse->isPhpLint()) {
122 1
                $this->commandBus->handle(
123 1
                    new PhpLintToolCommand($phpFiles, $preCommitResponse->getErrorMessage())
124
                );
125
            }
126
127 1
            $phpCsResponse = $preCommitResponse->getPhpCs();
128
129 1
            if (true === $phpCsResponse->isPhpCs()) {
130 1
                $this->commandBus->handle(
131 1
                    new PhpCsToolCommand(
132
                        $phpFiles,
133 1
                        $phpCsResponse->getPhpCsStandard(),
134 1
                        $preCommitResponse->getErrorMessage(),
135 1
                        $phpCsResponse->getIgnore()
136
                    )
137
                );
138
            }
139
140 1
            $phpCsFixerResponse = $preCommitResponse->getPhpCsFixer();
141
142 1
            if (true === $phpCsFixerResponse->isPhpCsFixer()) {
143 1
                $this->commandBus->handle(
144 1
                    new PhpCsFixerToolCommand(
145
                        $phpFiles,
146 1
                        $phpCsFixerResponse->isPhpCsFixerPsr0(),
147 1
                        $phpCsFixerResponse->isPhpCsFixerPsr1(),
148 1
                        $phpCsFixerResponse->isPhpCsFixerPsr2(),
149 1
                        $phpCsFixerResponse->isPhpCsFixerSymfony(),
150 1
                        $phpCsFixerResponse->getPhpCsFixerOptions(),
151 1
                        $preCommitResponse->getErrorMessage()
152
                    )
153
                );
154
            }
155
156 1
            $phpMdResponse = $preCommitResponse->getPhpMd();
157
158 1
            if (true === $phpMdResponse->isPhpMd()) {
159 1
                $this->commandBus->handle(
160 1
                    new PhpMdToolCommand(
161
                        $phpFiles,
162 1
                        $phpMdResponse->getPhpMdOptions(),
163 1
                        $preCommitResponse->getErrorMessage()
164
                    )
165
                );
166
            }
167
168 1
            $phpunitResponse = $preCommitResponse->getPhpUnit();
169
170 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...
171 1
                $this->commandBus->handle(
172 1
                    new PhpUnitToolCommand(
173 1
                        $phpunitResponse->isPhpunitRandomMode(),
174 1
                        $phpunitResponse->getPhpunitOptions(),
175 1
                        $preCommitResponse->getErrorMessage()
176
                    )
177
                );
178
179 1
                $phpunitStrictCoverageResponse = $preCommitResponse->getPhpUnitStrictCoverage();
180
181 1
                if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) {
182 1
                    $this->commandBus->handle(
183 1
                        new StrictCoverageCommand(
184 1
                            $phpunitStrictCoverageResponse->getMinimum(),
185 1
                            $preCommitResponse->getErrorMessage()
186
                        )
187
                    );
188
                }
189
190 1
                $phpunitGuardCoverageResponse = $preCommitResponse->getPhpUnitGuardCoverage();
191
192 1
                if (true === $phpunitGuardCoverageResponse->isEnabled()) {
193 1
                    $this->commandBus->handle(
194 1
                        new GuardCoverageCommand(
195 1
                            $phpunitGuardCoverageResponse->getWarningMessage()
196
                        )
197
                    );
198
                }
199
            }
200
        }
201 1
    }
202
203
    /**
204
     * @param array $committedFiles
205
     *
206
     * @return array
207
     */
208 1
    private function getPhpFiles(array $committedFiles)
209
    {
210
        /**
211
         * @var PhpFilesResponse
212
         */
213 1
        $phpFilesResponse = $this->queryBus->handle(new PhpFilesExtractor($committedFiles));
214
215 1
        return $phpFilesResponse->getFiles();
216
    }
217
218
    /**
219
     * @param CommandInterface $command
220
     */
221 2
    public function handle(CommandInterface $command)
222
    {
223 2
        $this->execute();
224 2
    }
225
}
226