Skip to content

Commit dd89c2c

Browse files
committed
fix: json error formatter when files are empty
1 parent ae3a484 commit dd89c2c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Command/ErrorFormatter/JsonErrorFormatter.php

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
6060
$errorsArray['errors'][] = $notFileSpecificError;
6161
}
6262

63+
// When files empty format it as an empty object
64+
if ($errorsArray['files'] === []) {
65+
$errorsArray['files'] = new stdClass();
66+
}
67+
6368
$json = Json::encode($errorsArray, $this->pretty ? Json::PRETTY : 0);
6469

6570
$output->writeRaw($json);

tests/PHPStan/Command/ErrorFormatter/JsonErrorFormatterTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function dataFormatterOutputProvider(): iterable
2424
"errors":0,
2525
"file_errors":0
2626
},
27-
"files":[],
27+
"files":{},
2828
"errors": []
2929
}',
3030
];
@@ -67,7 +67,7 @@ public function dataFormatterOutputProvider(): iterable
6767
"errors":1,
6868
"file_errors":0
6969
},
70-
"files":[],
70+
"files":{},
7171
"errors": [
7272
"first generic error"
7373
]
@@ -133,7 +133,7 @@ public function dataFormatterOutputProvider(): iterable
133133
"errors":2,
134134
"file_errors":0
135135
},
136-
"files":[],
136+
"files":{},
137137
"errors": [
138138
"first generic error",
139139
"second generic<error>"

0 commit comments

Comments
 (0)