Repository navigation
Expand file tree
/
Copy pathJunitErrorFormatter.php
More file actions
138 lines (117 loc) · 3.76 KB
/
Copy pathJunitErrorFormatter.php
File metadata and controls
138 lines (117 loc) · 3.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\RelativePathHelper;
use SplFileObject;
use function array_filter;
use function file_exists;
use function htmlspecialchars;
use function implode;
use function is_null;
use function sprintf;
use const ENT_COMPAT;
use const ENT_XML1;
use const PHP_EOL;
#[AutowiredService(name: 'errorFormatter.junit')]
final class JunitErrorFormatter implements ErrorFormatter
{
public function __construct(
#[AutowiredParameter(ref: '@simpleRelativePathHelper')]
private RelativePathHelper $relativePathHelper,
)
{
}
public function formatErrors(
AnalysisResult $analysisResult,
Output $output,
): int
{
$totalFailuresCount = $analysisResult->getTotalErrorsCount();
$totalTestsCount = $analysisResult->hasErrors() ? $totalFailuresCount : 1;
$result = '<?xml version="1.0" encoding="UTF-8"?>';
$result .= sprintf(
'<testsuite failures="%d" name="phpstan" tests="%d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.9.3/platform-tests/src/test/resources/jenkins-junit.xsd">',
$totalFailuresCount,
$totalTestsCount,
);
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
$fileName = $this->relativePathHelper->getRelativePath($fileSpecificError->getFile());
$result .= $this->createTestCase(
$fileName,
'ERROR',
implode(PHP_EOL, array_filter([ $fileSpecificError->getMessage(), $fileSpecificError->getTip() ], static fn (?string $part): bool => $part !== null && $part !== '')),
$fileSpecificError->getLine(),
$fileSpecificError->getIdentifier(),
);
}
foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
$result .= $this->createTestCase('General error', 'ERROR', $notFileSpecificError);
}
foreach ($analysisResult->getWarnings() as $warning) {
$result .= $this->createTestCase('Warning', 'WARNING', $warning);
}
if (!$analysisResult->hasErrors()) {
$result .= $this->createTestCase('phpstan', '');
}
$result .= '</testsuite>';
$output->writeRaw($result);
return $analysisResult->hasErrors() ? 1 : 0;
}
/**
* Format a single test case
*
*
*/
private function createTestCase(string $reference, string $type, ?string $message = null, ?int $line = null, ?string $identifier = null): string
{
$result = sprintf(
'<testcase name="%s" classname="%s">', // class="%s" file="%s" line="%d",
$this->escape(is_null($identifier) ? '' : $identifier),
//$this->escape($reference),
//$this->escape($reference),
$this->escape(sprintf('%s:%d', $reference, $line ?? 0)),
//$line,
);
if ($message !== null) {
if ($line === null) {
$result .= sprintf(
'<failure type="%s" message="%s" />',
$this->escape($type),
$this->escape($message),
);
} else {
$result .= sprintf(
'<failure type="%s" message="%s"><![CDATA[%s]]></failure>',
$this->escape($type),
$this->escape($message),
$this->fileGetLine($reference, $line),
);
}
}
return $result . '</testcase>';
}
private function fileGetLine(string $fileName, int $lineNumber): ?string
{
if (!file_exists($fileName)) {
return null;
}
$file = new SplFileObject($fileName);
if ($file->eof()) {
return null;
}
$file->seek($lineNumber);
// @phpstan-ignore-next-line
return $file->current(); // $contents would hold the data from line x
}
/**
* Escapes values for using in XML
*
*/
private function escape(string $string): string
{
return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8');
}
}