-
Notifications
You must be signed in to change notification settings - Fork 430
/
BaselineNeonErrorFormatter.php
75 lines (63 loc) · 1.73 KB
/
BaselineNeonErrorFormatter.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use Nette\DI\Helpers;
use Nette\Neon\Neon;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use function ksort;
use function preg_quote;
use const SORT_STRING;
class BaselineNeonErrorFormatter implements ErrorFormatter
{
public function __construct(private RelativePathHelper $relativePathHelper)
{
}
public function formatErrors(
AnalysisResult $analysisResult,
Output $output,
): int
{
if (!$analysisResult->hasErrors()) {
$output->writeRaw(Neon::encode([
'parameters' => [
'ignoreErrors' => [],
],
], Neon::BLOCK));
return 0;
}
$fileErrors = [];
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
if (!$fileSpecificError->canBeIgnored()) {
continue;
}
$fileErrors[$this->relativePathHelper->getRelativePath($fileSpecificError->getFilePath())][] = $fileSpecificError->getMessage();
}
ksort($fileErrors, SORT_STRING);
$errorsToOutput = [];
foreach ($fileErrors as $file => $errorMessages) {
$fileErrorsCounts = [];
foreach ($errorMessages as $errorMessage) {
if (!isset($fileErrorsCounts[$errorMessage])) {
$fileErrorsCounts[$errorMessage] = 1;
continue;
}
$fileErrorsCounts[$errorMessage]++;
}
ksort($fileErrorsCounts, SORT_STRING);
foreach ($fileErrorsCounts as $message => $count) {
$errorsToOutput[] = [
'message' => Helpers::escape('#^' . preg_quote($message, '#') . '$#'),
'count' => $count,
'path' => Helpers::escape($file),
];
}
}
$output->writeRaw(Neon::encode([
'parameters' => [
'ignoreErrors' => $errorsToOutput,
],
], Neon::BLOCK));
return 1;
}
}