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
|
|
|
|
32
|
|
|
final class RemoveBaseLineFromResultsCommand extends Command |
33
|
|
|
{ |
34
|
|
|
public const COMMAND_NAME = 'remove-baseline-results'; |
35
|
|
|
|
36
|
|
|
private const OUTPUT_FORMAT = 'output-format'; |
37
|
|
|
private const SHOW_RANDOM_ERRORS = 'clean-up'; |
38
|
|
|
private const IGNORE_WARNINGS = 'ignore-warnings'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
protected static $defaultName = self::COMMAND_NAME; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
private ResultsPrunerInterface $resultsPruner, |
47
|
|
|
private OutputFormatterLookupService $outputFormatterLookupService, |
48
|
|
|
private TableOutputFormatter $tableOutputFormatter, |
49
|
|
|
private RandomResultsPicker $randomResultsPicker, |
50
|
|
|
) { |
51
|
|
|
parent::__construct(self::COMMAND_NAME); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function configure(): void |
55
|
|
|
{ |
56
|
|
|
$this->setDescription('Shows issues created since the baseline'); |
57
|
|
|
|
58
|
|
|
$outputFormatters = $this->outputFormatterLookupService->getIdentifiers(); |
59
|
|
|
$this->addOption( |
60
|
|
|
self::OUTPUT_FORMAT, |
61
|
|
|
null, |
62
|
|
|
InputOption::VALUE_REQUIRED, |
63
|
|
|
'Output format. One of: '.implode('|', $outputFormatters), |
64
|
|
|
TableOutputFormatter::CODE, |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$this->addOption( |
68
|
|
|
self::IGNORE_WARNINGS, |
69
|
|
|
null, |
70
|
|
|
InputOption::VALUE_NONE, |
71
|
|
|
"Ignore any issues with severity 'warning'.", |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->addOption( |
75
|
|
|
self::SHOW_RANDOM_ERRORS, |
76
|
|
|
null, |
77
|
|
|
InputOption::VALUE_NONE, |
78
|
|
|
'Show a random 5 issues in the baseline to fix', |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
ProjectRootHelper::configureProjectRootOption($this); |
82
|
|
|
|
83
|
|
|
BaseLineFileHelper::configureBaseLineFileArgument($this); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$projectRoot = ProjectRootHelper::getProjectRoot($input); |
90
|
|
|
$outputFormatter = $this->getOutputFormatter($input); |
91
|
|
|
$baseLineFileName = BaseLineFileHelper::getBaselineFile($input); |
92
|
|
|
$inputAnalysisResultsAsString = CliConfigReader::getStdin($input); |
93
|
|
|
$showRandomIssues = CliConfigReader::getBooleanOption($input, self::SHOW_RANDOM_ERRORS); |
94
|
|
|
$ignoreWarnings = CliConfigReader::getBooleanOption($input, self::IGNORE_WARNINGS); |
95
|
|
|
|
96
|
|
|
$prunedResults = $this->resultsPruner->getPrunedResults( |
97
|
|
|
$baseLineFileName, |
98
|
|
|
$inputAnalysisResultsAsString, |
99
|
|
|
$projectRoot, |
100
|
|
|
$ignoreWarnings, |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$outputAnalysisResults = $prunedResults->getPrunedResults(); |
104
|
|
|
|
105
|
|
|
OutputWriter::writeToStdError( |
106
|
|
|
$output, |
107
|
|
|
"Latest analysis issue count: {$prunedResults->getInputAnalysisResults()->getCount()}", |
108
|
|
|
false, |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
OutputWriter::writeToStdError( |
112
|
|
|
$output, |
113
|
|
|
"Baseline issue count: {$prunedResults->getBaseLine()->getAnalysisResults()->getCount()}", |
114
|
|
|
false, |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
OutputWriter::writeToStdError( |
118
|
|
|
$output, |
119
|
|
|
"Issue count with baseline removed: {$outputAnalysisResults->getCount()}", |
120
|
|
|
!$outputAnalysisResults->hasNoIssues(), |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$outputAsString = $outputFormatter->outputResults($outputAnalysisResults); |
124
|
|
|
$output->writeln($outputAsString); |
125
|
|
|
|
126
|
|
|
$returnCode = $outputAnalysisResults->hasNoIssues() ? 0 : 1; |
127
|
|
|
|
128
|
|
|
if ($showRandomIssues && !$prunedResults->getInputAnalysisResults()->hasNoIssues()) { |
129
|
|
|
$randomIssues = $this->randomResultsPicker->getRandomResultsToFix($prunedResults->getInputAnalysisResults()); |
130
|
|
|
|
131
|
|
|
OutputWriter::writeToStdError( |
132
|
|
|
$output, |
133
|
|
|
"\n\nRandom {$randomIssues->getCount()} issues in the baseline to fix...", |
134
|
|
|
false, |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$outputAsString = $this->tableOutputFormatter->outputResults($randomIssues); |
138
|
|
|
|
139
|
|
|
OutputWriter::writeToStdError( |
140
|
|
|
$output, |
141
|
|
|
$outputAsString, |
142
|
|
|
false, |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $returnCode; |
147
|
|
|
} catch (\Throwable $throwable) { |
148
|
|
|
$returnCode = ErrorReporter::reportError($output, $throwable); |
149
|
|
|
|
150
|
|
|
return $returnCode; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws InvalidConfigException |
156
|
|
|
*/ |
157
|
|
|
private function getOutputFormatter(InputInterface $input): OutputFormatter |
158
|
|
|
{ |
159
|
|
|
$identifier = CliConfigReader::getOptionWithDefaultValue($input, self::OUTPUT_FORMAT); |
160
|
|
|
|
161
|
|
|
try { |
162
|
|
|
return $this->outputFormatterLookupService->getOutputFormatter($identifier); |
163
|
|
|
} catch (InvalidOutputFormatterException) { |
164
|
|
|
throw InvalidConfigException::invalidOptionValue(self::OUTPUT_FORMAT, $identifier, $this->outputFormatterLookupService->getIdentifiers()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|