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

TableOutputFormatter::addIssuesInTable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 32
rs 9.6666
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\OutputFormatter\SummaryStats;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults;
11
use Symfony\Component\Console\Helper\Table;
12
use Symfony\Component\Console\Output\BufferedOutput;
13
use Webmozart\Assert\Assert;
14
15
class TableOutputFormatter implements OutputFormatter
16
{
17
    public function outputResults(SummaryStats $summaryStats, AnalysisResults $analysisResults): string
18
    {
19
        $output = <<<EOF
20
Latest issue count: {$summaryStats->getLatestAnalysisResultsCount()}
21
Baseline issue count: {$summaryStats->getBaseLineCount()}
22
Issues count with baseline removed: {$analysisResults->getCount()}
23
EOF;
24
25
        if ($analysisResults->hasNoIssues()) {
26
            return $output;
27
        }
28
29
        $bufferedOutput = new BufferedOutput();
30
        $this->addIssuesInTable($bufferedOutput, $analysisResults);
31
32
        $output .= PHP_EOL;
33
        $output .= $bufferedOutput->fetch();
34
35
        return $output;
36
    }
37
38
    public function getIdentifier(): string
39
    {
40
        return 'table';
41
    }
42
43
    private function addIssuesInTable(BufferedOutput $output, AnalysisResults $analysisResults): void
44
    {
45
        /** @var string[] $headings */
46
        $headings = [
47
            'Line',
48
            'Description',
49
        ];
50
51
        /** @var FileName $currentFileName */
52
        $currentFileName = null;
53
        /** @var Table|null $currentTable */
54
        $currentTable = null;
55
        foreach ($analysisResults->getOrderedAnalysisResults() as $analysisResult) {
56
            $fileName = $analysisResult->getLocation()->getFileName();
57
58
            if (!$fileName->isEqual($currentFileName)) {
59
                $this->renderTable($currentTable);
60
61
                $output->writeln("\nFILE: {$fileName->getFileName()}");
62
                $currentFileName = $fileName;
63
                $currentTable = new Table($output);
64
                $currentTable->setHeaders($headings);
65
            }
66
67
            Assert::notNull($currentTable);
68
            $currentTable->addRow([
69
                $analysisResult->getLocation()->getLineNumber()->getLineNumber(),
70
                $analysisResult->getMessage(),
71
            ]);
72
        }
73
74
        $this->renderTable($currentTable);
75
    }
76
77
    private function renderTable(?Table $table): void
78
    {
79
        if (null !== $table) {
80
            $table->render();
81
        }
82
    }
83
}
84