Skip to content

Commit acf4cd3

Browse files
Update index.php to better parse PMD response
1 parent bce1f7b commit acf4cd3

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

index.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,30 @@
6363
@unlink($rulesFile);
6464
}
6565

66-
/* ---------- 8. Parse JSON output ---------- */
67-
if ($pmdRaw === null || $pmdRaw === '') {
68-
echo json_encode(['violations' => [], 'debug' => 'PMD returned no data']);
66+
/* ---------- 8. Extract JSON from raw output (strip warnings) ---------- */
67+
$jsonStart = strpos($pmdRaw, '{');
68+
if ($jsonStart === false) {
69+
http_response_code(500);
70+
echo json_encode(['error' => 'No JSON found in PMD output', 'raw' => $pmdRaw]);
6971
exit;
7072
}
7173

72-
$pmdJson = json_decode($pmdRaw, true);
74+
$cleanJson = substr($pmdRaw, $jsonStart);
75+
$pmdJson = json_decode($cleanJson, true);
76+
7377
if (json_last_error() !== JSON_ERROR_NONE) {
7478
http_response_code(500);
75-
echo json_encode(['error' => 'PMD JSON parse error', 'raw' => $pmdRaw]);
79+
echo json_encode([
80+
'error' => 'JSON decode failed: ' . json_last_error_msg(),
81+
'raw' => $pmdRaw,
82+
'clean' => $cleanJson
83+
]);
7684
exit;
7785
}
7886

79-
/* ---------- 9. Return only the violations ---------- */
87+
/* ---------- 9. Return violations ---------- */
8088
$violations = $pmdJson['files'][0]['violations'] ?? [];
81-
echo json_encode(['violations' => $violations, 'debug' => 'OK']);
89+
echo json_encode([
90+
'violations' => $violations,
91+
'debug' => 'PMD executed and parsed successfully'
92+
]);

0 commit comments

Comments
 (0)