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

Function parse_casts_array

src/parser/classes.rs:447–488  ·  view source on GitHub ↗

Parse key-value pairs from a PHP array literal text. Accepts text starting with `[` and extracts `'key' => 'value'` pairs. Both single-quoted and double-quoted strings are supported for keys and values. Handles multi-line arrays and trailing commas. Returns a list of `(key, value)` string pairs.

(text: &str)

Source from the content-addressed store, hash-verified

445///
446/// Returns a list of `(key, value)` string pairs.
447fn parse_casts_array(text: &str) -> Vec<(String, String)> {
448 let mut results = Vec::new();
449 let trimmed = text.trim();
450
451 // Must start with `[`
452 let inner = if let Some(s) = trimmed.strip_prefix('[') {
453 // Strip trailing `]` if present
454 s.strip_suffix(']').unwrap_or(s)
455 } else {
456 return results;
457 };
458
459 // Split on commas, handling each `'key' => 'value'` pair.
460 // This simple approach works because cast arrays contain only
461 // string literals — no nested arrays or complex expressions.
462 for segment in inner.split(',') {
463 let segment = segment.trim();
464 if segment.is_empty() {
465 continue;
466 }
467
468 // Look for the `=>` arrow.
469 let Some(arrow_pos) = segment.find("=>") else {
470 continue;
471 };
472
473 let key_part = segment[..arrow_pos].trim();
474 let value_part = segment[arrow_pos + 2..].trim();
475
476 let key = extract_string_literal(key_part);
477 let value = extract_string_literal(value_part);
478
479 if let (Some(k), Some(v)) = (key, value)
480 && !k.is_empty()
481 && !v.is_empty()
482 {
483 results.push((k, v));
484 }
485 }
486
487 results
488}
489
490/// Extract the string content from a PHP string literal.
491///

Callers 1

Calls 4

pushMethod · 0.80
extract_string_literalFunction · 0.70
is_emptyMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected