|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CodeLts\CliTools; |
| 6 | + |
| 7 | +use CodeLts\CliTools\ErrorFormatter\CheckstyleErrorFormatter; |
| 8 | +use CodeLts\CliTools\ErrorFormatter\ErrorFormatter; |
| 9 | +use CodeLts\CliTools\ErrorFormatter\GithubErrorFormatter; |
| 10 | +use CodeLts\CliTools\ErrorFormatter\GitlabErrorFormatter; |
| 11 | +use CodeLts\CliTools\ErrorFormatter\JsonErrorFormatter; |
| 12 | +use CodeLts\CliTools\ErrorFormatter\JunitErrorFormatter; |
| 13 | +use CodeLts\CliTools\ErrorFormatter\RawErrorFormatter; |
| 14 | +use CodeLts\CliTools\ErrorFormatter\RawTextErrorFormatter; |
| 15 | +use CodeLts\CliTools\ErrorFormatter\TableErrorFormatter; |
| 16 | +use CodeLts\CliTools\ErrorFormatter\TeamcityErrorFormatter; |
| 17 | +use CodeLts\CliTools\File\FuzzyRelativePathHelper; |
| 18 | +use CodeLts\CliTools\File\NullRelativePathHelper; |
| 19 | +use CodeLts\CliTools\File\RelativePathHelper; |
| 20 | +use CodeLts\CliTools\File\SimpleRelativePathHelper; |
| 21 | +use Exception; |
| 22 | + |
| 23 | +class OutputFormat |
| 24 | +{ |
| 25 | + public const OUTPUT_FORMAT_RAW = 'raw'; |
| 26 | + public const OUTPUT_FORMAT_RAW_TEXT = 'rawtext'; |
| 27 | + public const OUTPUT_FORMAT_JSON = 'json'; |
| 28 | + public const OUTPUT_FORMAT_JSON_PRETTY = 'prettyJson'; |
| 29 | + public const OUTPUT_FORMAT_JUNIT = 'junit'; |
| 30 | + public const OUTPUT_FORMAT_TABLE = 'table'; |
| 31 | + public const OUTPUT_FORMAT_CHECKSTYLE = 'checkstyle'; |
| 32 | + public const OUTPUT_FORMAT_GITLAB = 'gitlab'; |
| 33 | + public const OUTPUT_FORMAT_GITHUB = 'github'; |
| 34 | + public const OUTPUT_FORMAT_TEAMCITY = 'teamcity'; |
| 35 | + |
| 36 | + public const VALID_OUTPUT_FORMATS = [ |
| 37 | + OutputFormat::OUTPUT_FORMAT_RAW, |
| 38 | + OutputFormat::OUTPUT_FORMAT_RAW_TEXT, |
| 39 | + OutputFormat::OUTPUT_FORMAT_TABLE, |
| 40 | + OutputFormat::OUTPUT_FORMAT_CHECKSTYLE, |
| 41 | + OutputFormat::OUTPUT_FORMAT_JSON, |
| 42 | + OutputFormat::OUTPUT_FORMAT_JUNIT, |
| 43 | + OutputFormat::OUTPUT_FORMAT_JSON_PRETTY, |
| 44 | + OutputFormat::OUTPUT_FORMAT_GITLAB, |
| 45 | + OutputFormat::OUTPUT_FORMAT_GITHUB, |
| 46 | + OutputFormat::OUTPUT_FORMAT_TEAMCITY, |
| 47 | + ]; |
| 48 | + |
| 49 | + /** |
| 50 | + * Checks that the format is valid |
| 51 | + * |
| 52 | + * @param string $outputFormat The format to check |
| 53 | + * @return true |
| 54 | + * |
| 55 | + * @throws Exception |
| 56 | + */ |
| 57 | + public static function checkOutputFormatIsValid(string $outputFormat): bool |
| 58 | + { |
| 59 | + if (in_array($outputFormat, OutputFormat::VALID_OUTPUT_FORMATS)) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + throw new Exception( |
| 63 | + sprintf( |
| 64 | + 'Error formatter "%s" not found. Available error formatters are: %s', |
| 65 | + $outputFormat, |
| 66 | + implode(', ', OutputFormat::VALID_OUTPUT_FORMATS) |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Display to the output the AnalysisResult for the format |
| 73 | + */ |
| 74 | + public static function displayUserChoiceFormat( |
| 75 | + string $outputFormat, |
| 76 | + AnalysisResult $analysisResult, |
| 77 | + ?string $sourceRootDirectory, |
| 78 | + Output $output |
| 79 | + ): void { |
| 80 | + $pathHelper = new NullRelativePathHelper(); |
| 81 | + if ($sourceRootDirectory !== null) { |
| 82 | + $pathHelper = new FuzzyRelativePathHelper( |
| 83 | + new SimpleRelativePathHelper( |
| 84 | + $sourceRootDirectory |
| 85 | + ), |
| 86 | + $sourceRootDirectory |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + $formatter = OutputFormat::getFormatterForChoice($outputFormat, $pathHelper); |
| 91 | + $formatter->formatErrors( |
| 92 | + $analysisResult, |
| 93 | + $output |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * get an ErrorFormatter for the given format |
| 99 | + * |
| 100 | + * @throws Exception if the format is not implemented |
| 101 | + */ |
| 102 | + public static function getFormatterForChoice( |
| 103 | + string $outputFormat, |
| 104 | + RelativePathHelper $pathHelper |
| 105 | + ): ErrorFormatter { |
| 106 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_RAW) { |
| 107 | + return new RawErrorFormatter(); |
| 108 | + } |
| 109 | + |
| 110 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_RAW_TEXT) { |
| 111 | + return new RawTextErrorFormatter(); |
| 112 | + } |
| 113 | + |
| 114 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JSON) { |
| 115 | + return new JsonErrorFormatter(false); |
| 116 | + } |
| 117 | + |
| 118 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JSON_PRETTY) { |
| 119 | + return new JsonErrorFormatter(true); |
| 120 | + } |
| 121 | + |
| 122 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JUNIT) { |
| 123 | + return new JunitErrorFormatter($pathHelper); |
| 124 | + } |
| 125 | + |
| 126 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_TABLE) { |
| 127 | + return new TableErrorFormatter($pathHelper); |
| 128 | + } |
| 129 | + |
| 130 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_CHECKSTYLE) { |
| 131 | + return new CheckstyleErrorFormatter($pathHelper); |
| 132 | + } |
| 133 | + |
| 134 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_GITLAB) { |
| 135 | + return new GitlabErrorFormatter($pathHelper); |
| 136 | + } |
| 137 | + |
| 138 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_GITHUB) { |
| 139 | + return new GithubErrorFormatter($pathHelper, new TableErrorFormatter($pathHelper)); |
| 140 | + } |
| 141 | + |
| 142 | + if ($outputFormat === OutputFormat::OUTPUT_FORMAT_TEAMCITY) { |
| 143 | + return new TeamcityErrorFormatter($pathHelper); |
| 144 | + } |
| 145 | + |
| 146 | + throw new Exception('Not implemented format'); |
| 147 | + } |
| 148 | +} |
0 commit comments