Repository navigation
Expand file tree
/
Copy pathGithubErrorFormatter.php
More file actions
130 lines (106 loc) · 4 KB
/
Copy pathGithubErrorFormatter.php
File metadata and controls
130 lines (106 loc) · 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
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
<?php declare(strict_types = 1);
namespace PHPStan\Command\ErrorFormatter;
use PHPStan\Analyser\Error;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\File\RelativePathHelper;
use Symfony\Component\Console\Formatter\OutputFormatter;
use function array_walk;
use function explode;
use function implode;
use function ltrim;
use function preg_replace;
use function sprintf;
use function str_contains;
use function str_replace;
/**
* Allow errors to be reported in pull-requests diff when run in a GitHub Action
* @see https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
*/
#[AutowiredService(name: 'errorFormatter.github')]
final class GithubErrorFormatter implements ErrorFormatter
{
public function __construct(
#[AutowiredParameter(ref: '@simpleRelativePathHelper')]
private RelativePathHelper $relativePathHelper,
)
{
}
public function formatErrors(AnalysisResult $analysisResult, Output $output): int
{
$projectConfigFile = 'phpstan.neon';
if ($analysisResult->getProjectConfigFile() !== null) {
$projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile());
}
$tipFormatter = new OutputFormatter(false);
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
$metas = [
'file' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
'line' => $fileSpecificError->getLine(),
'col' => 0,
];
array_walk($metas, static function (&$value, string $key): void {
$value = sprintf('%s=%s', $key, (string) $value);
});
$message = $this->buildFileSpecificMessage($fileSpecificError, $tipFormatter, $projectConfigFile);
$message = $this->formatMessage($message);
$line = sprintf('::error %s::%s', implode(',', $metas), $message);
$output->writeRaw($line);
$output->writeLineFormatted('');
}
foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
$notFileSpecificError = $this->formatMessage($notFileSpecificError);
$line = sprintf('::error ::%s', $notFileSpecificError);
$output->writeRaw($line);
$output->writeLineFormatted('');
}
foreach ($analysisResult->getWarnings() as $warning) {
$warning = $this->formatMessage($warning);
$warning = str_replace("\n", '%0A', $warning);
$line = sprintf('::warning ::%s', $warning);
$output->writeRaw($line);
$output->writeLineFormatted('');
}
return $analysisResult->hasErrors() ? 1 : 0;
}
/**
* Mimics the TableErrorFormatter's layout: the message is followed by
* the fixable marker, the identifier and the tip on separate lines.
*/
private function buildFileSpecificMessage(Error $error, OutputFormatter $tipFormatter, string $projectConfigFile): string
{
$message = $error->getMessage();
if ($error->getFixedErrorDiffs() !== []) {
$message .= "\n🔧 Can be fixed automatically. Run PHPStan again with --fix.";
}
$identifier = $error->getIdentifier();
if ($identifier !== null) {
$message .= sprintf("\n🪪 %s: https://phpstan.org/error-identifiers/%s", $identifier, $identifier);
if (!$error->canBeIgnored()) {
$message .= ' (non-ignorable)';
}
}
$tip = $error->getTip();
if ($tip !== null) {
$tip = str_replace('%configurationFile%', $projectConfigFile, $tip);
$tip = $tipFormatter->format(ConsoleTagEscaper::escape($tip)) ?? $tip;
if (str_contains($tip, "\n")) {
foreach (explode("\n", $tip) as $tipLine) {
$message .= "\n💡 " . ltrim($tipLine, ' •');
}
} else {
$message .= "\n💡 " . $tip;
}
}
return $message;
}
private function formatMessage(string $message): string
{
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$message = str_replace("\n", '%0A', $message);
return preg_replace('/(^|\s)@([a-zA-Z0-9_\-]+)(\s|$)/', '$1`@$2`$3', $message) ?? $message;
}
}