forked from php/docbook-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckstyleReporter.php
More file actions
48 lines (37 loc) · 1.5 KB
/
Copy pathCheckstyleReporter.php
File metadata and controls
48 lines (37 loc) · 1.5 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
<?php
declare(strict_types=1);
namespace DocbookCS\Report\Reporter;
use DocbookCS\RelativePath;
use DocbookCS\Report\Report;
final class CheckstyleReporter implements ReporterInterface
{
public function generate(Report $report): string
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('checkstyle');
$root->setAttribute('version', '3.0');
$dom->appendChild($root);
$comment = $dom->createComment(
sprintf(' total runtime: %.3fs ', $report->getTotalTime())
);
$root->appendChild($comment);
foreach ($report->getFileReports() as $fileReport) {
if (!$fileReport->hasViolations()) {
continue;
}
$fileNode = $dom->createElement('file');
$fileNode->setAttribute('name', RelativePath::fromWorkingDirectory($fileReport->filePath));
foreach ($fileReport->getViolations() as $violation) {
$errorNode = $dom->createElement('error');
$errorNode->setAttribute('line', (string) $violation->line);
$errorNode->setAttribute('severity', $violation->severity->value);
$errorNode->setAttribute('message', $violation->message);
$errorNode->setAttribute('source', $violation->sniffCode);
$fileNode->appendChild($errorNode);
}
$root->appendChild($fileNode);
}
return $dom->saveXML() ?: '';
}
}