MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / parse_phpcs_json

Function parse_phpcs_json

src/phpcs.rs:254–287  ·  view source on GitHub ↗

Parse PHPCS's JSON output into LSP diagnostics. PHPCS JSON format (with `--report=json`): ```json { "totals": { "errors": 1, "warnings": 1, "fixable": 2 }, "files": { "/path/to/file.php": { "errors": 1, "warnings": 1, "messages": [ { "message": "Line indented incorrectly; expected 4 spaces, found 2", "source": "PSR2.Methods.FunctionCallSignature.Indent", "severity": 5, "fixable": true, "type": "

(json_str: &str, file_path: &Path)

Source from the content-addressed store, hash-verified

252/// by the `--stdin-path` value. When there is only one file entry,
253/// we use it regardless of the key to avoid path-matching issues.
254fn parse_phpcs_json(json_str: &str, file_path: &Path) -> Result<Vec<Diagnostic>, String> {
255 let output: serde_json::Value =
256 serde_json::from_str(json_str).map_err(|e| format!("Failed to parse PHPCS JSON: {}", e))?;
257
258 let mut diagnostics = Vec::new();
259
260 if let Some(files) = output.get("files").and_then(|f| f.as_object()) {
261 // When using stdin mode with --stdin-path, PHPCS keys the output
262 // by the stdin-path value. Try matching by the path, and if
263 // there's only one file entry, use it regardless of key.
264 let messages = if files.len() == 1 {
265 files.values().next()
266 } else {
267 let file_path_str = file_path.to_string_lossy();
268 files
269 .iter()
270 .find(|(path, _)| paths_match(path, &file_path_str))
271 .map(|(_, v)| v)
272 };
273
274 if let Some(msgs) = messages
275 .and_then(|fd| fd.get("messages"))
276 .and_then(|m| m.as_array())
277 {
278 for msg in msgs {
279 if let Some(diag) = parse_phpcs_message(msg) {
280 diagnostics.push(diag);
281 }
282 }
283 }
284 }
285
286 Ok(diagnostics)
287}
288
289/// Parse a single PHPCS message object into an LSP `Diagnostic`.
290fn parse_phpcs_message(msg: &serde_json::Value) -> Option<Diagnostic> {

Callers 11

run_phpcsFunction · 0.85
parse_empty_resultFunction · 0.85
parse_file_messagesFunction · 0.85
parse_fixable_flagFunction · 0.85
parse_no_matching_fileFunction · 0.85
parse_invalid_jsonFunction · 0.85
parse_warning_severityFunction · 0.85
parse_full_line_rangeFunction · 0.85
parse_stdin_path_keyFunction · 0.85

Calls 9

parse_phpcs_messageFunction · 0.85
nextMethod · 0.80
valuesMethod · 0.80
iterMethod · 0.80
pushMethod · 0.80
paths_matchFunction · 0.70
getMethod · 0.45
mapMethod · 0.45
findMethod · 0.45

Tested by 10

parse_empty_resultFunction · 0.68
parse_file_messagesFunction · 0.68
parse_fixable_flagFunction · 0.68
parse_no_matching_fileFunction · 0.68
parse_invalid_jsonFunction · 0.68
parse_warning_severityFunction · 0.68
parse_full_line_rangeFunction · 0.68
parse_stdin_path_keyFunction · 0.68