Passed
Pull Request — master (#69)
by Dave
02:21
created

CreateBaseLineCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
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\Creator\BaseLineCreatorInterface;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactory;
17
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactoryLookupService;
18
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\InvalidHistoryFactoryException;
19
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\InvalidResultsParserException;
20
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParser;
21
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParserLookupService;
22
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\BaseLineFileHelper;
23
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\CliConfigReader;
24
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ErrorReporter;
25
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\InvalidConfigException;
26
use DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal\ProjectRootHelper;
27
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\ResultsParsers\SarbJsonResultsParser\SarbJsonIdentifier;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Input\InputOption;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Throwable;
33
34
class CreateBaseLineCommand extends Command
35
{
36
    public const COMMAND_NAME = 'create';
37
38
    public const INPUT_FORMAT = 'input-format';
39
    private const DEFAULT_STATIC_ANALYSIS_FORMAT = SarbJsonIdentifier::CODE;
40
41
    private const HISTORY_ANALYSER = 'history-analyser';
42
    private const DEFAULT_HISTORY_FACTORY_NAME = 'git';
43
    private const DOC_URL = 'https://github.com/DaveLiddament/sarb/blob/master/docs/ViolationTypeClassificationGuessing.md';
44
45
    /**
46
     * @var string
47
     */
48
    protected static $defaultName = self::COMMAND_NAME;
49
50
    /**
51
     * @var ResultsParserLookupService
52
     */
53
    private $resultsParserLookupService;
54
55
    /**
56
     * @var HistoryFactoryLookupService
57
     */
58
    private $historyFactoryLookupService;
59
    /**
60
     * @var BaseLineCreatorInterface
61
     */
62
    private $baseLineCreator;
63
64
    public function __construct(
65
        ResultsParserLookupService $resultsParsersLookupService,
66
        HistoryFactoryLookupService $historyFactoryLookupService,
67
        BaseLineCreatorInterface $baseLineCreator
68
    ) {
69
        $this->resultsParserLookupService = $resultsParsersLookupService;
70
        $this->historyFactoryLookupService = $historyFactoryLookupService;
71
        $this->baseLineCreator = $baseLineCreator;
72
        parent::__construct(self::COMMAND_NAME);
73
    }
74
75
    protected function configure(): void
76
    {
77
        $this->setDescription('Creates a baseline of the static analysis results for the specified static analysis format');
78
79
        $staticAnalysisParserIdentifiers = implode('|', $this->resultsParserLookupService->getIdentifiers());
80
        $this->addOption(
81
            self::INPUT_FORMAT,
82
            null,
83
            InputOption::VALUE_REQUIRED,
84
            sprintf('Static analysis tool. One of: %s', $staticAnalysisParserIdentifiers),
85
            self::DEFAULT_STATIC_ANALYSIS_FORMAT
86
        );
87
88
        $historyAnalyserIdentifiers = implode('|', $this->historyFactoryLookupService->getIdentifiers());
89
        $this->addOption(
90
            self::HISTORY_ANALYSER,
91
            null,
92
            InputOption::VALUE_REQUIRED,
93
            sprintf('History analyser. One of: %s', $historyAnalyserIdentifiers),
94
            self::DEFAULT_HISTORY_FACTORY_NAME
95
        );
96
97
        ProjectRootHelper::configureProjectRootOption($this);
98
99
        BaseLineFileHelper::configureBaseLineFileArgument($this);
100
    }
101
102
    protected function execute(InputInterface $input, OutputInterface $output): int
103
    {
104
        try {
105
            $projectRoot = ProjectRootHelper::getProjectRoot($input);
106
            $historyFactory = $this->getHistoryFactory($input, $output);
107
            $resultsParser = $this->getResultsParser($input, $output);
108
            $baselineFile = BaseLineFileHelper::getBaselineFile($input);
109
            $analysisResultsAsString = CliConfigReader::getStdin($input);
110
111
            $baseLine = $this->baseLineCreator->createBaseLine(
112
                $historyFactory,
113
                $resultsParser,
114
                $baselineFile,
115
                $projectRoot,
116
                $analysisResultsAsString
117
            );
118
119
            $errorsInBaseLine = $baseLine->getAnalysisResults()->getCount();
120
            ErrorReporter::writeToStdError($output, '<info>Baseline created</info>');
121
            ErrorReporter::writeToStdError($output, "<info>Errors in baseline $errorsInBaseLine</info>");
122
123
            return 0;
124
        } catch (Throwable $throwable) {
125
            return ErrorReporter::reportError($output, $throwable);
126
        }
127
    }
128
129
    /**
130
     * @throws InvalidConfigException
131
     */
132
    private function getResultsParser(InputInterface $input, OutputInterface $output): ResultsParser
133
    {
134
        $identifier = CliConfigReader::getOptionWithDefaultValue($input, self::INPUT_FORMAT);
135
136
        try {
137
            $resultsParser = $this->resultsParserLookupService->getResultsParser($identifier);
138
        } catch (InvalidResultsParserException $e) {
139
            throw InvalidConfigException::invalidOptionValue(self::INPUT_FORMAT, $identifier, $this->resultsParserLookupService->getIdentifiers());
140
        }
141
142
        if ($resultsParser->showTypeGuessingWarning()) {
143
            $warning = '[%s] guesses the classification of violations. This means results might not be 100%% accurate. See %s for more details.';
144
            $output->writeln(sprintf($warning, $identifier, self::DOC_URL));
145
        }
146
147
        return $resultsParser;
148
    }
149
150
    /**
151
     * @throws InvalidConfigException
152
     */
153
    private function getHistoryFactory(InputInterface $input, OutputInterface $output): HistoryFactory
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

153
    private function getHistoryFactory(InputInterface $input, /** @scrutinizer ignore-unused */ OutputInterface $output): HistoryFactory

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        $identifier = CliConfigReader::getOptionWithDefaultValue($input, self::HISTORY_ANALYSER);
156
157
        try {
158
            $resultsParser = $this->historyFactoryLookupService->getHistoryFactory($identifier);
159
        } catch (InvalidHistoryFactoryException $e) {
160
            throw InvalidConfigException::invalidOptionValue(self::HISTORY_ANALYSER, $identifier, $this->historyFactoryLookupService->getIdentifiers());
161
        }
162
163
        return $resultsParser;
164
    }
165
}
166