TableOutputFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 3 1
A renderTable() 0 4 2
A outputResults() 0 14 3
A addIssuesInTable() 0 44 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\OutputFormatters;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\AbsoluteFileName;
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
final 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
        $hasWarnings = $this->addIssuesInTable($bufferedOutput, $analysisResults);
26
27
        if ($hasWarnings) {
28
            $bufferedOutput->writeln("\nThese results include warnings. To exclude warnings from output use the --ignore-warnings flag.");
29
        }
30
31
        return $bufferedOutput->fetch();
32
    }
33
34
    public function getIdentifier(): string
35
    {
36
        return self::CODE;
37
    }
38
39
    private function addIssuesInTable(BufferedOutput $output, AnalysisResults $analysisResults): bool
40
    {
41
        $hasWarnings = false;
42
43
        /** @var string[] $headings */
44
        $headings = [
45
            'Line',
46
            'Description',
47
        ];
48
49
        /** @var AbsoluteFileName $currentFileName */
50
        $currentFileName = null;
51
        /** @var Table|null $currentTable */
52
        $currentTable = null;
53
        foreach ($analysisResults->getAnalysisResults() as $analysisResult) {
54
            $fileName = $analysisResult->getLocation()->getAbsoluteFileName();
55
56
            if (!$fileName->isEqual($currentFileName)) {
57
                $this->renderTable($currentTable);
58
59
                $output->writeln("\nFILE: {$fileName->getFileName()}");
60
                $currentFileName = $fileName;
61
                $currentTable = new Table($output);
62
                $currentTable->setHeaders($headings);
63
            }
64
65
            $warning = $analysisResult->getSeverity()->isWarning();
66
            if ($warning) {
67
                $hasWarnings = true;
68
                $prefix = 'WARNING: ';
69
            } else {
70
                $prefix = '';
71
            }
72
73
            Assert::notNull($currentTable, 'No Table object');
74
            $currentTable->addRow([
75
                $analysisResult->getLocation()->getLineNumber()->getLineNumber(),
76
                $prefix.$analysisResult->getMessage(),
77
            ]);
78
        }
79
80
        $this->renderTable($currentTable);
81
82
        return $hasWarnings;
83
    }
84
85
    private function renderTable(?Table $table): void
86
    {
87
        if (null !== $table) {
88
            $table->render();
89
        }
90
    }
91
}
92