Skip to content

Commit 71ebd8f

Browse files
committed
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
1 parent c42190a commit 71ebd8f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Command/ErrorFormatter/GitlabErrorFormatter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Command\AnalysisResult;
77
use PHPStan\Command\Output;
88
use PHPStan\File\RelativePathHelper;
9+
use function file_put_contents;
910
use function hash;
1011
use function implode;
1112

@@ -15,7 +16,10 @@
1516
final class GitlabErrorFormatter implements ErrorFormatter
1617
{
1718

18-
public function __construct(private RelativePathHelper $relativePathHelper)
19+
public function __construct(
20+
private RelativePathHelper $relativePathHelper,
21+
private ?string $fileLocation = null,
22+
)
1923
{
2024
}
2125

@@ -64,7 +68,11 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
6468

6569
$json = Json::encode($errorsArray, Json::PRETTY);
6670

67-
$output->writeRaw($json);
71+
if ($this->fileLocation !== null) {
72+
file_put_contents($this->fileLocation, $json);
73+
} else {
74+
$output->writeRaw($json);
75+
}
6876

6977
return $analysisResult->hasErrors() ? 1 : 0;
7078
}

0 commit comments

Comments
 (0)