Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ parameters:
editorUrl: null
editorUrlTitle: null
errorFormat: null
githubErrorLevel: 'error'
pro:
dnsServers:
- '1.1.1.1'
Expand Down Expand Up @@ -424,6 +425,7 @@ parametersSchema:
editorUrl: schema(string(), nullable())
editorUrlTitle: schema(string(), nullable())
errorFormat: schema(string(), nullable())
githubErrorLevel: string()
pro: structure([
dnsServers: schema(listOf(string()), min(1)),
])
Expand Down Expand Up @@ -2161,6 +2163,7 @@ services:
class: PHPStan\Command\ErrorFormatter\GithubErrorFormatter
arguments:
relativePathHelper: @simpleRelativePathHelper
errorLevel: %githubErrorLevel%

errorFormatter.teamcity:
class: PHPStan\Command\ErrorFormatter\TeamcityErrorFormatter
Expand Down
10 changes: 8 additions & 2 deletions src/Command/ErrorFormatter/GithubErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace PHPStan\Command\ErrorFormatter;

use InvalidArgumentException;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use function array_walk;
use function implode;
use function in_array;
use function sprintf;
use function str_replace;

Expand All @@ -19,8 +21,12 @@ class GithubErrorFormatter implements ErrorFormatter

public function __construct(
private RelativePathHelper $relativePathHelper,
private string $errorLevel = 'error',
)
{
if (!in_array($errorLevel, ['notice', 'warning', 'error'], true)) {
throw new InvalidArgumentException(sprintf('Invalid error level "%s"', $errorLevel));
}
}

public function formatErrors(AnalysisResult $analysisResult, Output $output): int
Expand All @@ -40,7 +46,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$message = str_replace("\n", '%0A', $message);

$line = sprintf('::error %s::%s', implode(',', $metas), $message);
$line = sprintf('::%s %s::%s', $this->errorLevel, implode(',', $metas), $message);

$output->writeRaw($line);
$output->writeLineFormatted('');
Expand All @@ -51,7 +57,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$notFileSpecificError = str_replace("\n", '%0A', $notFileSpecificError);

$line = sprintf('::error ::%s', $notFileSpecificError);
$line = sprintf('::%s ::%s', $this->errorLevel, $notFileSpecificError);

$output->writeRaw($line);
$output->writeLineFormatted('');
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function dataFormatterOutputProvider(): iterable
0,
0,
'',
'error',
];

yield [
Expand All @@ -27,6 +28,7 @@ public function dataFormatterOutputProvider(): iterable
0,
'::error file=folder with unicode 😃/file name with "spaces" and unicode 😃.php,line=4,col=0::Foo
',
'error',
];

yield [
Expand All @@ -36,6 +38,7 @@ public function dataFormatterOutputProvider(): iterable
1,
'::error ::first generic error
',
'error',
];

yield [
Expand All @@ -48,6 +51,7 @@ public function dataFormatterOutputProvider(): iterable
::error file=foo.php,line=1,col=0::Foo
::error file=foo.php,line=5,col=0::Bar%0ABar2
',
'error',
];

yield [
Expand All @@ -58,6 +62,7 @@ public function dataFormatterOutputProvider(): iterable
'::error ::first generic error
::error ::second generic error
',
'error',
];

yield [
Expand All @@ -72,6 +77,17 @@ public function dataFormatterOutputProvider(): iterable
::error ::first generic error
::error ::second generic error
',
'error',
];

yield [
'One generic error as warning',
1,
0,
1,
'::warning ::first generic error
',
'warning',
];
}

Expand All @@ -85,11 +101,13 @@ public function testFormatErrors(
int $numFileErrors,
int $numGenericErrors,
string $expected,
string $errorLevel,
): void
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new GithubErrorFormatter(
$relativePathHelper,
$errorLevel,
);

$this->assertSame($exitCode, $formatter->formatErrors(
Expand Down