-
-
Notifications
You must be signed in to change notification settings - Fork 744
Expand file tree
/
Copy pathCheckstyleDOMElementFactory.php
More file actions
105 lines (82 loc) · 3.4 KB
/
Copy pathCheckstyleDOMElementFactory.php
File metadata and controls
105 lines (82 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Xml;
use DOMDocument;
use DOMElement;
use Rector\ChangesReporting\Application\ErrorAndDiffCollector;
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
use Rector\Core\ValueObject\Reporting\FileDiff;
/**
* Inspiration in @see \Symfony\Component\Console\Descriptor\XmlDescriptor
*/
final class CheckstyleDOMElementFactory
{
/**
* @var string
*/
private const CHECKSTYLE = 'checkstyle';
/**
* @var string
*/
private const FILE = 'file';
/**
* @var string
*/
private const ERROR = 'error';
public function create(DOMDocument $domDocument, ErrorAndDiffCollector $errorAndDiffCollector): DOMElement
{
$domElement = $domDocument->createElement(self::CHECKSTYLE);
foreach ($errorAndDiffCollector->getFileDiffs() as $fileDiff) {
$fileDOMElement = $this->createFileDOMElement($domDocument, $fileDiff);
$domElement->appendChild($fileDOMElement);
}
$nonFileErrorDOMElement = $this->createNonFileErrorDOMElements($domDocument, $errorAndDiffCollector);
if ($nonFileErrorDOMElement !== null) {
$domElement->appendChild($nonFileErrorDOMElement);
}
return $domElement;
}
private function createFileDOMElement(DOMDocument $domDocument, FileDiff $fileDiff): DOMElement
{
$domElement = $domDocument->createElement(self::FILE);
$domElement->setAttribute('name', $this->escapeForXml($fileDiff->getRelativeFilePath()));
foreach ($fileDiff->getRectorChanges() as $rectorWithFileAndLineChange) {
$errorDOMElement = $this->createErrorDOMElement($rectorWithFileAndLineChange, $domDocument);
$domElement->appendChild($errorDOMElement);
}
return $domElement;
}
private function createNonFileErrorDOMElements(
DOMDocument $domDocument,
ErrorAndDiffCollector $errorAndDiffCollector
): ?DOMElement {
if ($errorAndDiffCollector->getErrors() === []) {
return null;
}
$domElement = $domDocument->createElement(self::FILE);
foreach ($errorAndDiffCollector->getErrors() as $rectorError) {
$errorDOMElement = $domDocument->createElement(self::ERROR);
$errorDOMElement->setAttribute('severity', self::ERROR);
$errorDOMElement->setAttribute('message', $this->escapeForXml($rectorError->getMessage()));
$domElement->appendChild($errorDOMElement);
}
return $domElement;
}
private function escapeForXml(string $string): string
{
return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT);
}
private function createErrorDOMElement(
RectorWithFileAndLineChange $rectorWithFileAndLineChange,
DOMDocument $domDocument
): DOMElement {
$domElement = $domDocument->createElement(self::ERROR);
$domElement->setAttribute('line', $this->escapeForXml((string) $rectorWithFileAndLineChange->getLine()));
$domElement->setAttribute('column', '1');
$domElement->setAttribute('severity', self::ERROR);
$message = $rectorWithFileAndLineChange->
getRectorDefinitionsDescription() . ' (Reported by: ' . $rectorWithFileAndLineChange->getRectorClass() . ')';
$domElement->setAttribute('message', $this->escapeForXml($message));
return $domElement;
}
}