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