|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\OutputFormatters; |
|
6
|
|
|
|
|
7
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\FileName; |
|
8
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\OutputFormatter\OutputFormatter; |
|
9
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults; |
|
10
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
11
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
12
|
|
|
use Webmozart\Assert\Assert; |
|
13
|
|
|
|
|
14
|
|
|
class TableOutputFormatter implements OutputFormatter |
|
15
|
|
|
{ |
|
16
|
|
|
public const CODE = 'table'; |
|
17
|
|
|
|
|
18
|
|
|
public function outputResults(AnalysisResults $analysisResults): string |
|
19
|
|
|
{ |
|
20
|
|
|
if ($analysisResults->hasNoIssues()) { |
|
21
|
|
|
return 'No issues'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$bufferedOutput = new BufferedOutput(); |
|
25
|
|
|
$this->addIssuesInTable($bufferedOutput, $analysisResults); |
|
26
|
|
|
|
|
27
|
|
|
return $bufferedOutput->fetch(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getIdentifier(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return self::CODE; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function addIssuesInTable(BufferedOutput $output, AnalysisResults $analysisResults): void |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var string[] $headings */ |
|
38
|
|
|
$headings = [ |
|
39
|
|
|
'Line', |
|
40
|
|
|
'Description', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** @var FileName $currentFileName */ |
|
44
|
|
|
$currentFileName = null; |
|
45
|
|
|
/** @var Table|null $currentTable */ |
|
46
|
|
|
$currentTable = null; |
|
47
|
|
|
foreach ($analysisResults->getAnalysisResults() as $analysisResult) { |
|
48
|
|
|
$fileName = $analysisResult->getLocation()->getFileName(); |
|
49
|
|
|
|
|
50
|
|
|
if (!$fileName->isEqual($currentFileName)) { |
|
51
|
|
|
$this->renderTable($currentTable); |
|
52
|
|
|
|
|
53
|
|
|
$output->writeln("\nFILE: {$fileName->getFileName()}"); |
|
54
|
|
|
$currentFileName = $fileName; |
|
55
|
|
|
$currentTable = new Table($output); |
|
56
|
|
|
$currentTable->setHeaders($headings); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
Assert::notNull($currentTable); |
|
60
|
|
|
$currentTable->addRow([ |
|
61
|
|
|
$analysisResult->getLocation()->getLineNumber()->getLineNumber(), |
|
62
|
|
|
$analysisResult->getMessage(), |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->renderTable($currentTable); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function renderTable(?Table $table): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (null !== $table) { |
|
72
|
|
|
$table->render(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|