@@ -98,7 +98,7 @@ private function decodeStructuredOutput(string $content): mixed
9898 }
9999
100100 try {
101- return json_decode ($ trimmedContent , true );
101+ return $ this -> normalizeStructuredPayload ( json_decode ($ trimmedContent , true ) );
102102 } catch (JsonException ) {
103103 }
104104
@@ -140,7 +140,7 @@ private function decodeJsonDocumentStream(string $content): ?array
140140 }
141141
142142 try {
143- $ decodedDocuments [] = json_decode ($ document , true );
143+ $ decodedDocuments [] = $ this -> normalizeStructuredPayload ( json_decode ($ document , true ) );
144144 } catch (JsonException ) {
145145 return null ;
146146 }
@@ -219,4 +219,52 @@ private function consumeJsonDocument(string $content, int &$offset): ?string
219219
220220 return null ;
221221 }
222+
223+ /**
224+ * Normalizes decoded structured payloads produced by wrapped tooling.
225+ *
226+ * @param mixed $payload the decoded payload
227+ *
228+ * @return mixed the normalized payload
229+ */
230+ private function normalizeStructuredPayload (mixed $ payload ): mixed
231+ {
232+ if (! \is_array ($ payload )) {
233+ return $ payload ;
234+ }
235+
236+ if (! isset ($ payload ['totals ' ]) || ! \is_array ($ payload ['totals ' ])) {
237+ return $ payload ;
238+ }
239+
240+ $ changedFilesTotal = $ payload ['totals ' ]['changed_files ' ] ?? null ;
241+
242+ if (! \is_int ($ changedFilesTotal )) {
243+ return $ payload ;
244+ }
245+
246+ if (0 === $ changedFilesTotal ) {
247+ $ payload ['changed_files ' ] = [];
248+
249+ return $ payload ;
250+ }
251+
252+ if (! isset ($ payload ['file_diffs ' ]) || ! \is_array ($ payload ['file_diffs ' ])) {
253+ return $ payload ;
254+ }
255+
256+ $ changedFiles = [];
257+
258+ foreach ($ payload ['file_diffs ' ] as $ fileDiff ) {
259+ if (! \is_array ($ fileDiff ) || ! isset ($ fileDiff ['file ' ]) || ! \is_string ($ fileDiff ['file ' ])) {
260+ continue ;
261+ }
262+
263+ $ changedFiles [$ fileDiff ['file ' ]] = $ fileDiff ['file ' ];
264+ }
265+
266+ $ payload ['changed_files ' ] = array_values ($ changedFiles );
267+
268+ return $ payload ;
269+ }
222270}
0 commit comments