Passed
Pull Request — master (#98)
by
unknown
02:40
created

JunitOutputFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
eloc 73
c 3
b 2
f 1
dl 0
loc 104
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getXmlString() 0 11 1
A getIdentifier() 0 3 1
B outputResults() 0 81 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\OutputFormatters;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\OutputFormatter\OutputFormatter;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults;
9
use DOMDocument;
10
use Exception;
11
use http\Exception\RuntimeException;
12
use SimpleXMLElement;
13
14
class JunitOutputFormatter implements OutputFormatter
15
{
16
    /**
17
     * @throws Exception
18
     */
19
    public function outputResults(AnalysisResults $analysisResults): string
20
    {
21
        if (0 === $analysisResults->getCount()) {
22
            return <<<XML
23
<?xml version="1.0" encoding="UTF-8"?>
24
<testsuites
25
        name="SARB" tests="1" failures="0">
26
    <testsuite errors="0" tests="1" failures="0" name="Success">
27
        <testcase name="Success"/>
28
    </testsuite>
29
</testsuites>
30
31
XML;
32
        }
33
34
        $xml = $this->getXmlString();
35
        $test = new SimpleXMLElement($xml);
36
37
        $testCount = (string) $analysisResults->getCount();
38
        $test['tests'] = $testCount;
39
        $test['failures'] = $testCount;
40
41
        $suitCount = 0;
42
        $caseCount = 0;
43
        $oldRel = null;
44
        $testsuite = null;
45
        foreach ($analysisResults->getAnalysisResults() as $analysisResult) {
46
            $details = $analysisResult->getFullDetails();
47
            $type = $details['type'] ?? 'error';
48
            $type = strtolower($type);
49
            $column = $details['column'] ?? '0';
50
51
            $relativeFileName = $analysisResult->getLocation()->getRelativeFileName()->getFileName();
52
53
            if ($oldRel !== $relativeFileName || null === $testsuite) {
54
                $testsuite = $test->addChild('testsuite');
55
                $testsuite->addAttribute('errors', '0');
56
                $testsuite->addAttribute('tests', (string) $caseCount);
57
                $testsuite->addAttribute('failures', (string) $caseCount);
58
                $testsuite->addAttribute('name', $relativeFileName);
59
60
                $oldRel = $relativeFileName;
61
                ++$suitCount;
62
                $caseCount = 0;
63
            }
64
65
            $lineSprint = sprintf(
66
                '%s at %s (%s:%s)',
67
                $analysisResult->getType()->getType(),
68
                $analysisResult->getLocation()->getAbsoluteFileName()->getFileName(),
69
                (string) $analysisResult->getLocation()->getLineNumber()->getLineNumber(),
70
                $column
71
            );
72
            $testcase = $testsuite->addChild('testcase');
73
            $testcase->addAttribute('name', $lineSprint);
74
            $testcase->addChild('failure');
75
            $testcase->failure->addAttribute('type', $type);
76
            $testcase->failure->addAttribute(
77
                'message',
78
                $analysisResult->getMessage()
79
            );
80
            ++$caseCount;
81
            $testsuite['tests'] = $caseCount;
82
            $testsuite['failures'] = $caseCount;
83
        }
84
85
        $dom = new DOMDocument('1.0');
86
        $dom->preserveWhiteSpace = false;
87
        $dom->formatOutput = true;
88
        $asXml = $test->asXML();
89
90
        if (false !== $asXml) {
91
            $dom->loadXML($asXml);
0 ignored issues
show
Bug introduced by
It seems like $asXml can also be of type true; however, parameter $source of DOMDocument::loadXML() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $dom->loadXML(/** @scrutinizer ignore-type */ $asXml);
Loading history...
92
        } else {
93
            throw new RuntimeException('xml could not be loaded');
94
        }
95
        $saveXml = $dom->saveXML();
96
        if (false !== $saveXml) {
97
            return $saveXml;
98
        }
99
        throw new RuntimeException('dom could not be saved');
100
    }
101
102
    private function getXmlString(): string
103
    {
104
        $xmlstr = <<<XML
105
<?xml version="1.0" encoding="UTF-8"?>
106
<testsuites
107
        name="SARB" tests="1" failures="0">
108
</testsuites>
109
110
XML;
111
112
        return $xmlstr;
113
    }
114
115
    public function getIdentifier(): string
116
    {
117
        return 'junit';
118
    }
119
}
120