From 1a3911b8c7eb318e7776316264bad1c7cec7b432 Mon Sep 17 00:00:00 2001 From: Thomas Nabord Date: Sun, 28 Jun 2020 10:00:06 +0100 Subject: [PATCH] Add Formatter for GitHub Actions --- conf/config.neon | 6 + .../ErrorFormatter/GithubErrorFormatter.php | 54 ++++++ .../GithubErrorFormatterTest.php | 169 ++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 src/Command/ErrorFormatter/GithubErrorFormatter.php create mode 100644 tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php diff --git a/conf/config.neon b/conf/config.neon index 9ad402094f..1bf6f76114 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1216,3 +1216,9 @@ services: class: PHPStan\Command\ErrorFormatter\GitlabErrorFormatter arguments: relativePathHelper: @simpleRelativePathHelper + + errorFormatter.github: + class: PHPStan\Command\ErrorFormatter\GithubErrorFormatter + arguments: + relativePathHelper: @simpleRelativePathHelper + tableErrorformatter: '@errorFormatter.table' diff --git a/src/Command/ErrorFormatter/GithubErrorFormatter.php b/src/Command/ErrorFormatter/GithubErrorFormatter.php new file mode 100644 index 0000000000..0dd9fcddb2 --- /dev/null +++ b/src/Command/ErrorFormatter/GithubErrorFormatter.php @@ -0,0 +1,54 @@ +relativePathHelper = $relativePathHelper; + $this->tableErrorformatter = $tableErrorformatter; + } + + public function formatErrors(AnalysisResult $analysisResult, Output $output): int + { + $this->tableErrorformatter->formatErrors($analysisResult, $output); + + 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 = $fileSpecificError->getMessage(); + + $line = sprintf('::error %s::%s', implode(',', $metas), $message); + + $output->writeRaw($line); + $output->writeLineFormatted(''); + } + + return $analysisResult->hasErrors() ? 1 : 0; + } + +} diff --git a/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php new file mode 100644 index 0000000000..efab2c3d0d --- /dev/null +++ b/tests/PHPStan/Command/ErrorFormatter/GithubErrorFormatterTest.php @@ -0,0 +1,169 @@ +assertSame($exitCode, $formatter->formatErrors( + $this->getAnalysisResult($numFileErrors, $numGenericErrors), + $this->getOutput() + ), sprintf('%s: response code do not match', $message)); + + $this->assertEquals($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message)); + } + +}