Completed
Push — master ( 27667a...b36312 )
by Dave
15s queued 12s
created

RemoveBaseLineFromResultsCommand::execute()   B

Complexity

Conditions 5
Paths 27

Size

Total Lines 63
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 41
c 0
b 0
f 0
nc 27
nop 2
dl 0
loc 63
rs 8.9528

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
3
/**
4
 * Static Analysis Results Baseliner (sarb).
5
 *
6
 * (c) Dave Liddament
7
 *
8
 * For the full copyright and licence information please view the LICENSE file distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command;
14
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\OutputFormatter\InvalidOutputFormatterException;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\OutputFormatter\OutputFormatter;
17
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\OutputFormatter\OutputFormatterLookupService;
18
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Pruner\ResultsPrunerInterface;
19
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\RandomResultsPicker\RandomResultsPicker;
20
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\BaseLineFileHelper;
21
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\CliConfigReader;
22
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ErrorReporter;
23
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\InvalidConfigException;
24
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\OutputWriter;
25
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ProjectRootHelper;
26
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\OutputFormatters\TableOutputFormatter;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Throwable;
32
33
class RemoveBaseLineFromResultsCommand extends Command
34
{
35
    public const COMMAND_NAME = 'remove-baseline-results';
36
37
    private const OUTPUT_FORMAT = 'output-format';
38
    private const SHOW_RANDOM_ERRORS = 'clean-up';
39
40
    /**
41
     * @var string|null
42
     */
43
    protected static $defaultName = self::COMMAND_NAME;
44
45
    /**
46
     * @var OutputFormatterLookupService
47
     */
48
    private $outputFormatterLookupService;
49
    /**
50
     * @var ResultsPrunerInterface
51
     */
52
    private $resultsPruner;
53
    /**
54
     * @var TableOutputFormatter
55
     */
56
    private $tableOutputFormatter;
57
    /**
58
     * @var RandomResultsPicker
59
     */
60
    private $randomResultsPicker;
61
62
    public function __construct(
63
        ResultsPrunerInterface $resultsPruner,
64
        OutputFormatterLookupService $outputFormatterLookupService,
65
        TableOutputFormatter $tableOutputFormatter,
66
        RandomResultsPicker $randomResultsPicker
67
    ) {
68
        $this->outputFormatterLookupService = $outputFormatterLookupService;
69
        parent::__construct(self::COMMAND_NAME);
70
        $this->resultsPruner = $resultsPruner;
71
        $this->tableOutputFormatter = $tableOutputFormatter;
72
        $this->randomResultsPicker = $randomResultsPicker;
73
    }
74
75
    protected function configure(): void
76
    {
77
        $this->setDescription('Shows issues created since the baseline');
78
79
        $outputFormatters = $this->outputFormatterLookupService->getIdentifiers();
80
        $this->addOption(
81
            self::OUTPUT_FORMAT,
82
            null,
83
            InputOption::VALUE_REQUIRED,
84
            'Output format. One of: '.implode('|', $outputFormatters),
85
            TableOutputFormatter::CODE
86
        );
87
88
        $this->addOption(
89
            self::SHOW_RANDOM_ERRORS,
90
            null,
91
            InputOption::VALUE_NONE,
92
            'Show a random 5 issues in the baseline to fix'
93
        );
94
95
        ProjectRootHelper::configureProjectRootOption($this);
96
97
        BaseLineFileHelper::configureBaseLineFileArgument($this);
98
    }
99
100
    protected function execute(InputInterface $input, OutputInterface $output): int
101
    {
102
        try {
103
            $projectRoot = ProjectRootHelper::getProjectRoot($input);
104
            $outputFormatter = $this->getOutputFormatter($input);
105
            $baseLineFileName = BaseLineFileHelper::getBaselineFile($input);
106
            $inputAnalysisResultsAsString = CliConfigReader::getStdin($input);
107
            $showRandomIssues = CliConfigReader::getBooleanOption($input, self::SHOW_RANDOM_ERRORS);
108
109
            $prunedResults = $this->resultsPruner->getPrunedResults(
110
                $baseLineFileName,
111
                $inputAnalysisResultsAsString,
112
                $projectRoot
113
            );
114
115
            $outputAnalysisResults = $prunedResults->getPrunedResults();
116
117
            OutputWriter::writeToStdError(
118
                $output,
119
                "Latest analysis issue count: {$prunedResults->getInputAnalysisResults()->getCount()}",
120
                false
121
            );
122
123
            OutputWriter::writeToStdError(
124
                $output,
125
                "Baseline issue count: {$prunedResults->getBaseLine()->getAnalysisResults()->getCount()}",
126
                false
127
            );
128
129
            OutputWriter::writeToStdError(
130
                $output,
131
                "Issue count with baseline removed: {$outputAnalysisResults->getCount()}",
132
                !$outputAnalysisResults->hasNoIssues()
133
            );
134
135
            $outputAsString = $outputFormatter->outputResults($outputAnalysisResults);
136
            $output->writeln($outputAsString);
137
138
            $returnCode = $outputAnalysisResults->hasNoIssues() ? 0 : 1;
139
140
            if ($showRandomIssues && !$prunedResults->getInputAnalysisResults()->hasNoIssues()) {
141
                $randomIssues = $this->randomResultsPicker->getRandomResultsToFix($prunedResults->getInputAnalysisResults());
142
143
                OutputWriter::writeToStdError(
144
                    $output,
145
                    "\n\nRandom {$randomIssues->getCount()} issues in the baseline to fix...",
146
                    false
147
                );
148
149
                $outputAsString = $this->tableOutputFormatter->outputResults($randomIssues);
150
151
                OutputWriter::writeToStdError(
152
                    $output,
153
                    $outputAsString,
154
                    false
155
                );
156
            }
157
158
            return $returnCode;
159
        } catch (Throwable $throwable) {
160
            $returnCode = ErrorReporter::reportError($output, $throwable);
161
162
            return $returnCode;
163
        }
164
    }
165
166
    /**
167
     * @throws InvalidConfigException
168
     */
169
    private function getOutputFormatter(InputInterface $input): OutputFormatter
170
    {
171
        $identifier = CliConfigReader::getOptionWithDefaultValue($input, self::OUTPUT_FORMAT);
172
173
        try {
174
            return $this->outputFormatterLookupService->getOutputFormatter($identifier);
175
        } catch (InvalidOutputFormatterException $e) {
176
            throw InvalidConfigException::invalidOptionValue(self::OUTPUT_FORMAT, $identifier, $this->outputFormatterLookupService->getIdentifiers());
177
        }
178
    }
179
}
180