From c42190a06894408a3b66c45ea77afd95919042fe Mon Sep 17 00:00:00 2001 From: gertdepagter Date: Tue, 26 Nov 2024 13:51:28 +0100 Subject: [PATCH 1/2] Introduce the 'multiple' error formatter Allows using multiple error formatters with just configuration --- conf/config.neon | 6 ++++ .../ErrorFormatter/ChainErrorFormatter.php | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/Command/ErrorFormatter/ChainErrorFormatter.php diff --git a/conf/config.neon b/conf/config.neon index ba4bbb2f62..2a00f5a748 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -2174,3 +2174,9 @@ services: class: PHPStan\Command\ErrorFormatter\TeamcityErrorFormatter arguments: relativePathHelper: @simpleRelativePathHelper + + errorFormatter.multiple: + class: PHPStan\Command\ErrorFormatter\ChainErrorFormatter + arguments: + formatters: + - @errorFormatter.table diff --git a/src/Command/ErrorFormatter/ChainErrorFormatter.php b/src/Command/ErrorFormatter/ChainErrorFormatter.php new file mode 100644 index 0000000000..8b64199bce --- /dev/null +++ b/src/Command/ErrorFormatter/ChainErrorFormatter.php @@ -0,0 +1,32 @@ + $formatters + */ + public function __construct( + private array $formatters, + ) + { + } + + public function formatErrors(AnalysisResult $analysisResult, Output $output): int + { + foreach ($this->formatters as $errorFormatter) { + $errorFormatter->formatErrors($analysisResult, $output); + } + + return $analysisResult->hasErrors() ? 1 : 0; + } + +} From 71ebd8fbd7bfdca585cc4609e6bde9368b7d4cff Mon Sep 17 00:00:00 2001 From: gertdepagter Date: Tue, 26 Nov 2024 13:53:20 +0100 Subject: [PATCH 2/2] Allow gitlab error formatter to output to a specific file instead of stdout Gitlab code quality report doesnt read from the logs, but expects a file. In order to use this with multiple error reporters, we want to be able to redirect its output --- src/Command/ErrorFormatter/GitlabErrorFormatter.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Command/ErrorFormatter/GitlabErrorFormatter.php b/src/Command/ErrorFormatter/GitlabErrorFormatter.php index 9a8ccb35cd..9d0a4ce00c 100644 --- a/src/Command/ErrorFormatter/GitlabErrorFormatter.php +++ b/src/Command/ErrorFormatter/GitlabErrorFormatter.php @@ -6,6 +6,7 @@ use PHPStan\Command\AnalysisResult; use PHPStan\Command\Output; use PHPStan\File\RelativePathHelper; +use function file_put_contents; use function hash; use function implode; @@ -15,7 +16,10 @@ final class GitlabErrorFormatter implements ErrorFormatter { - public function __construct(private RelativePathHelper $relativePathHelper) + public function __construct( + private RelativePathHelper $relativePathHelper, + private ?string $fileLocation = null, + ) { } @@ -64,7 +68,11 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in $json = Json::encode($errorsArray, Json::PRETTY); - $output->writeRaw($json); + if ($this->fileLocation !== null) { + file_put_contents($this->fileLocation, $json); + } else { + $output->writeRaw($json); + } return $analysisResult->hasErrors() ? 1 : 0; }