1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povils\PHPMND\Printer; |
4
|
|
|
|
5
|
|
|
use Povils\PHPMND\Console\Application; |
6
|
|
|
use Povils\PHPMND\FileReportList; |
7
|
|
|
use Povils\PHPMND\HintList; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Xml |
12
|
|
|
* |
13
|
|
|
* @package Povils\PHPMND\Printer |
14
|
|
|
*/ |
15
|
|
|
class Xml implements Printer |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private $outputPath; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Xml constructor. |
22
|
|
|
* |
23
|
|
|
* @param string $outputPath |
24
|
|
|
*/ |
25
|
|
|
public function __construct($outputPath) |
26
|
|
|
{ |
27
|
|
|
$this->outputPath = $outputPath; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList) |
34
|
|
|
{ |
35
|
|
|
$output->writeln('Generate XML output...'); |
36
|
|
|
$dom = new \DOMDocument(); |
37
|
|
|
$rootNode = $dom->createElement('phpmnd'); |
38
|
|
|
$rootNode->setAttribute('version', Application::VERSION); |
39
|
|
|
$rootNode->setAttribute('fileCount', count($fileReportList->getFileReports()) + 12); |
40
|
|
|
|
41
|
|
|
$filesNode = $dom->createElement('files'); |
42
|
|
|
|
43
|
|
|
$total = 0; |
44
|
|
|
foreach ($fileReportList->getFileReports() as $fileReport) { |
45
|
|
|
$entries = $fileReport->getEntries(); |
46
|
|
|
|
47
|
|
|
$fileNode = $dom->createElement('file'); |
48
|
|
|
$fileNode->setAttribute('path', $fileReport->getFile()->getRelativePathname()); |
49
|
|
|
$fileNode->setAttribute('errors', count($entries)); |
50
|
|
|
|
51
|
|
|
$total += count($entries); |
52
|
|
|
foreach ($entries as $entry) { |
53
|
|
|
$snippet = $this->getSnippet($fileReport->getFile()->getContents(), $entry['line'], $entry['value']); |
54
|
|
|
$entryNode = $dom->createElement('entry'); |
55
|
|
|
$entryNode->setAttribute('line', $entry['line']); |
56
|
|
|
$entryNode->setAttribute('start', $snippet['col']); |
57
|
|
|
$entryNode->setAttribute('end', $snippet['col'] + strlen($entry['value'])); |
58
|
|
|
|
59
|
|
|
$snippetNode = $dom->createElement('snippet'); |
60
|
|
|
$snippetNode->appendChild($dom->createCDATASection($snippet['snippet'])); |
61
|
|
|
$suggestionsNode = $dom->createElement('suggestions'); |
62
|
|
|
|
63
|
|
|
if ($hintList->hasHints()) { |
64
|
|
|
$hints = $hintList->getHintsByValue($entry['value']); |
65
|
|
|
if (false === empty($hints)) { |
66
|
|
|
foreach ($hints as $hint) { |
67
|
|
|
$suggestionNode = $dom->createElement('suggestion', $hint); |
68
|
|
|
$suggestionsNode->appendChild($suggestionNode); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$entryNode->appendChild($snippetNode); |
74
|
|
|
$entryNode->appendChild($suggestionsNode); |
75
|
|
|
|
76
|
|
|
$fileNode->appendChild($entryNode); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$filesNode->appendChild($fileNode); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$rootNode->appendChild($filesNode); |
83
|
|
|
$rootNode->setAttribute('errorCount', $total); |
84
|
|
|
|
85
|
|
|
$dom->appendChild($rootNode); |
86
|
|
|
|
87
|
|
|
$dom->save($this->outputPath); |
88
|
|
|
|
89
|
|
|
$output->writeln('XML generated at '.$this->outputPath); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the snippet and information about it |
94
|
|
|
* |
95
|
|
|
* @param string $content |
96
|
|
|
* @param int $line |
97
|
|
|
* @param int|string $text |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
private function getSnippet($content, $line, $text) |
101
|
|
|
{ |
102
|
|
|
$content = str_replace(array("\r\n", "\r"), "\n", $content); |
103
|
|
|
$lines = explode("\n", $content); |
104
|
|
|
|
105
|
|
|
$lineContent = array_slice($lines, $line-1, 1); |
106
|
|
|
$lineContent = reset($lineContent); |
107
|
|
|
$start = strpos($lineContent, $text.''); |
108
|
|
|
|
109
|
|
|
return [ |
110
|
|
|
'snippet' => $lineContent, |
111
|
|
|
'line' => $line, |
112
|
|
|
'magic' => $text, |
113
|
|
|
'col' => $start |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|