Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json error formatter when files are empty #3798

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions src/Command/ErrorFormatter/JsonErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Nette\Utils\Json;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use stdClass;
use Symfony\Component\Console\Formatter\OutputFormatter;
use function array_key_exists;
use function count;
use function property_exists;

final class JsonErrorFormatter implements ErrorFormatter
{
Expand All @@ -23,21 +24,21 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
'errors' => count($analysisResult->getNotFileSpecificErrors()),
'file_errors' => count($analysisResult->getFileSpecificErrors()),
],
'files' => [],
'files' => new stdClass(),
'errors' => [],
];

$tipFormatter = new OutputFormatter(false);

foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
$file = $fileSpecificError->getFile();
if (!array_key_exists($file, $errorsArray['files'])) {
$errorsArray['files'][$file] = [
if (!property_exists($errorsArray['files'], $file)) {
$errorsArray['files']->$file = [
'errors' => 0,
'messages' => [],
];
}
$errorsArray['files'][$file]['errors']++;
$errorsArray['files']->$file['errors']++;

$message = [
'message' => $fileSpecificError->getMessage(),
Expand All @@ -53,7 +54,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
$message['identifier'] = $fileSpecificError->getIdentifier();
}

$errorsArray['files'][$file]['messages'][] = $message;
$errorsArray['files']->$file['messages'][] = $message;
}

foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function dataFormatterOutputProvider(): iterable
"errors":0,
"file_errors":0
},
"files":[],
"files":{},
"errors": []
}',
];
Expand Down Expand Up @@ -67,7 +67,7 @@ public function dataFormatterOutputProvider(): iterable
"errors":1,
"file_errors":0
},
"files":[],
"files":{},
"errors": [
"first generic error"
]
Expand Down Expand Up @@ -133,7 +133,7 @@ public function dataFormatterOutputProvider(): iterable
"errors":2,
"file_errors":0
},
"files":[],
"files":{},
"errors": [
"first generic error",
"second generic<error>"
Expand Down
Loading