forked from easy-coding-standard/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckstyleOutputFormatter.php
More file actions
93 lines (76 loc) · 3.04 KB
/
CheckstyleOutputFormatter.php
File metadata and controls
93 lines (76 loc) · 3.04 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
<?php
declare(strict_types=1);
namespace Symplify\EasyCodingStandard\Console\Output;
use DOMDocument;
use DOMElement;
use RuntimeException;
use Symplify\EasyCodingStandard\Console\ExitCode;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult;
/**
* @see \Symplify\EasyCodingStandard\Tests\Console\Output\JsonOutputFormatterTest
*/
final readonly class CheckstyleOutputFormatter implements OutputFormatterInterface
{
public function __construct(
private EasyCodingStandardStyle $easyCodingStandardStyle,
private ExitCodeResolver $exitCodeResolver
) {
}
/**
* @return ExitCode::*
*/
public function report(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): int
{
$checkstyleContent = $this->createCheckstyleContent(
$errorAndDiffResult,
$configuration->isReportingWithRealPath()
);
$this->easyCodingStandardStyle->writeln($checkstyleContent);
return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration);
}
public static function getName(): string
{
return 'checkstyle';
}
public static function hasSupportForProgressBars(): bool
{
return false;
}
/**
* @api
*/
public function createCheckstyleContent(
ErrorAndDiffResult $errorAndDiffResult,
bool $absoluteFilePath = false
): string {
if (! \extension_loaded('dom')) {
throw new RuntimeException('Cannot generate report! `ext-dom` is not available!');
}
$domDocument = new DOMDocument('1.0', 'UTF-8');
/** @var DOMElement $domNode */
$domNode = $domDocument->appendChild($domDocument->createElement('checkstyle'));
foreach ($errorAndDiffResult->getFileDiffs() as $fileDiff) {
$filePath = $absoluteFilePath ? $fileDiff->getAbsoluteFilePath() : $fileDiff->getRelativeFilePath();
/** @var DOMElement $file */
$file = $domNode->appendChild($domDocument->createElement('file'));
$file->setAttribute('name', $filePath ?? '');
foreach ($fileDiff->getAppliedCheckers() as $appliedChecker) {
$errorElement = $this->createError($domDocument, $appliedChecker);
$file->appendChild($errorElement);
}
}
$domDocument->formatOutput = true;
return (string) $domDocument->saveXML();
}
private function createError(DOMDocument $domDocument, string $appliedChecker): DOMElement
{
$domElement = $domDocument->createElement('error');
$domElement->setAttribute('severity', 'warning');
$domElement->setAttribute('source', 'EasyCodingStandard.' . $appliedChecker);
$domElement->setAttribute('message', 'Found violation(s) of type: ' . $appliedChecker);
return $domElement;
}
}