XmlOutput::result()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace phphound\output;
3
4
use phphound\AnalysisResult;
5
use Sabre\Xml\Writer;
6
7
class XmlOutput extends AbstractOutput
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function result(AnalysisResult $result)
13
    {
14
        $this->cli->out($this->getXmlFor($result));
15
    }
16
17
    /**
18
     * Prepare XML file based on AnalysisResult.
19
     * @param AnalysisResult $result analysis result object.
20
     * @return string XML contents.
21
     */
22
    protected function getXmlFor(AnalysisResult $result)
23
    {
24
        $writer = new Writer;
25
        $writer->openMemory();
26
        $writer->write($this->getSabreXmlArrayFor($result));
27
        return '<?xml version="1.0" encoding="UTF-8"?>' . $writer->outputMemory();
28
    }
29
30
    /**
31
     * Prepare XML array for Sabre XML Writer.
32
     * @param AnalysisResult $result analysis result object.
33
     * @return array XML following Sabre structure.
34
     */
35
    protected function getSabreXmlArrayFor(AnalysisResult $result)
36
    {
37
        $sabreXmlArray = [
38
            'phphound' => [],
39
        ];
40
41
        foreach ($result->toArray() as $fileName => $lines) {
42
            $linesForXml = [];
43
44
            foreach ($lines as $lineNumber => $issues) {
45
                $issuesForXml = [];
46
47
                foreach ($issues as $issue) {
48
                    $issuesForXml[] = [
49
                        'name' => 'issue',
50
                        'value' => trim($issue['message']),
51
                        'attributes' => [
52
                            'tool' => $issue['tool'],
53
                            'type' => $issue['type'],
54
                        ]
55
                    ];
56
                }
57
58
                $linesForXml[] = [
59
                    'name' => 'line',
60
                    'value' => $issuesForXml,
61
                    'attributes' => ['number' => $lineNumber],
62
                ];
63
            }
64
65
            $sabreXmlArray['phphound'][] = [
66
                'name' => 'file',
67
                'value' => $linesForXml,
68
                'attributes' => ['name' => $fileName],
69
            ];
70
        }
71
72
        return $sabreXmlArray;
73
    }
74
}
75