Completed
Push — master ( 41ae82...437c90 )
by Dave
13s queued 11s
created

RemoveBaseLineFromResultsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 103
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 42 3
A getOutputFormatter() 0 8 2
A configure() 0 16 1
A __construct() 0 7 1
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\Framework\Command\internal\BaseLineFileHelper;
20
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\CliConfigReader;
21
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ErrorReporter;
22
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\InvalidConfigException;
23
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\OutputWriter;
24
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ProjectRootHelper;
25
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\OutputFormatters\TableOutputFormatter;
26
use Symfony\Component\Console\Command\Command;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Throwable;
31
32
class RemoveBaseLineFromResultsCommand extends Command
33
{
34
    public const COMMAND_NAME = 'remove-baseline-results';
35
36
    private const OUTPUT_FORMAT = 'output-format';
37
38
    /**
39
     * @var string|null
40
     */
41
    protected static $defaultName = self::COMMAND_NAME;
42
43
    /**
44
     * @var OutputFormatterLookupService
45
     */
46
    private $outputFormatterLookupService;
47
    /**
48
     * @var ResultsPrunerInterface
49
     */
50
    private $resultsPruner;
51
52
    public function __construct(
53
        ResultsPrunerInterface $resultsPruner,
54
        OutputFormatterLookupService $outputFormatterLookupService
55
    ) {
56
        $this->outputFormatterLookupService = $outputFormatterLookupService;
57
        parent::__construct(self::COMMAND_NAME);
58
        $this->resultsPruner = $resultsPruner;
59
    }
60
61
    protected function configure(): void
62
    {
63
        $this->setDescription('Shows issues created since the baseline');
64
65
        $outputFormatters = $this->outputFormatterLookupService->getIdentifiers();
66
        $this->addOption(
67
            self::OUTPUT_FORMAT,
68
            null,
69
            InputOption::VALUE_REQUIRED,
70
            'Output format. One of: '.implode('|', $outputFormatters),
71
            TableOutputFormatter::CODE
72
        );
73
74
        ProjectRootHelper::configureProjectRootOption($this);
75
76
        BaseLineFileHelper::configureBaseLineFileArgument($this);
77
    }
78
79
    protected function execute(InputInterface $input, OutputInterface $output): int
80
    {
81
        try {
82
            $projectRoot = ProjectRootHelper::getProjectRoot($input);
83
            $outputFormatter = $this->getOutputFormatter($input);
84
            $baseLineFileName = BaseLineFileHelper::getBaselineFile($input);
85
            $inputAnalysisResultsAsString = CliConfigReader::getStdin($input);
86
87
            $prunedResults = $this->resultsPruner->getPrunedResults(
88
                $baseLineFileName,
89
                $inputAnalysisResultsAsString,
90
                $projectRoot
91
            );
92
93
            $outputAnalysisResults = $prunedResults->getPrunedResults();
94
95
            OutputWriter::writeToStdError(
96
                $output,
97
                "Latest analysis issue count: {$prunedResults->getInputAnalysisResultsCount()}",
98
                false
99
            );
100
101
            OutputWriter::writeToStdError(
102
                $output,
103
                "Baseline issue count: {$prunedResults->getBaseLine()->getAnalysisResults()->getCount()}",
104
                false
105
            );
106
107
            OutputWriter::writeToStdError(
108
                $output,
109
                "Issue count with baseline removed: {$outputAnalysisResults->getCount()}",
110
                !$outputAnalysisResults->hasNoIssues()
111
            );
112
113
            $outputAsString = $outputFormatter->outputResults($outputAnalysisResults);
114
            $output->writeln($outputAsString);
115
116
            return $outputAnalysisResults->hasNoIssues() ? 0 : 1;
117
        } catch (Throwable $throwable) {
118
            $returnCode = ErrorReporter::reportError($output, $throwable);
119
120
            return $returnCode;
121
        }
122
    }
123
124
    /**
125
     * @throws InvalidConfigException
126
     */
127
    private function getOutputFormatter(InputInterface $input): OutputFormatter
128
    {
129
        $identifier = CliConfigReader::getOptionWithDefaultValue($input, self::OUTPUT_FORMAT);
130
131
        try {
132
            return $this->outputFormatterLookupService->getOutputFormatter($identifier);
133
        } catch (InvalidOutputFormatterException $e) {
134
            throw InvalidConfigException::invalidOptionValue(self::OUTPUT_FORMAT, $identifier, $this->outputFormatterLookupService->getIdentifiers());
135
        }
136
    }
137
}
138