XmlOutput   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A result() 0 4 1
A getXmlFor() 0 7 1
A getSabreXmlArrayFor() 0 39 4
1
<?php
2
namespace Finder\Logic\Output;
3
4
use Finder\Logic\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
     *
20
     * @param  AnalysisResult $result analysis result object.
21
     * @return string XML contents.
22
     */
23
    protected function getXmlFor(AnalysisResult $result)
24
    {
25
        $writer = new Writer;
26
        $writer->openMemory();
27
        $writer->write($this->getSabreXmlArrayFor($result));
28
        return '<?xml version="1.0" encoding="UTF-8"?>' . $writer->outputMemory();
29
    }
30
31
    /**
32
     * Prepare XML array for Sabre XML Writer.
33
     *
34
     * @param  AnalysisResult $result analysis result object.
35
     * @return array XML following Sabre structure.
36
     */
37
    protected function getSabreXmlArrayFor(AnalysisResult $result)
38
    {
39
        $sabreXmlArray = [
40
            'Finder\Logic' => [],
41
        ];
42
43
        foreach ($result->toArray() as $fileName => $lines) {
44
            $linesForXml = [];
45
46
            foreach ($lines as $lineNumber => $issues) {
47
                $issuesForXml = [];
48
49
                foreach ($issues as $issue) {
50
                    $issuesForXml[] = [
51
                        'name' => 'issue',
52
                        'value' => trim($issue['message']),
53
                        'attributes' => [
54
                            'tool' => $issue['tool'],
55
                            'type' => $issue['type'],
56
                        ]
57
                    ];
58
                }
59
60
                $linesForXml[] = [
61
                    'name' => 'line',
62
                    'value' => $issuesForXml,
63
                    'attributes' => ['number' => $lineNumber],
64
                ];
65
            }
66
67
            $sabreXmlArray['Finder\Logic'][] = [
68
                'name' => 'file',
69
                'value' => $linesForXml,
70
                'attributes' => ['name' => $fileName],
71
            ];
72
        }
73
74
        return $sabreXmlArray;
75
    }
76
}
77