Parse a comma-separated parameter list into `(type_hint, $name)` pairs.
(params_str: &str)
| 643 | |
| 644 | /// Parse a comma-separated parameter list into `(type_hint, $name)` pairs. |
| 645 | fn parse_params(params_str: &str) -> Vec<(Option<PhpType>, String)> { |
| 646 | if params_str.trim().is_empty() { |
| 647 | return Vec::new(); |
| 648 | } |
| 649 | |
| 650 | let mut result = Vec::new(); |
| 651 | |
| 652 | // Split on commas, respecting nested parens/angle brackets |
| 653 | for param in split_params(params_str) { |
| 654 | let param = param.trim(); |
| 655 | if param.is_empty() { |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | // Each param looks like: [Type] [$name] [= default] |
| 660 | // or: [Type] &$name, [Type] ...$name |
| 661 | let tokens: Vec<&str> = param.split_whitespace().collect(); |
| 662 | |
| 663 | let mut type_hint: Option<PhpType> = None; |
| 664 | let mut name: Option<String> = None; |
| 665 | |
| 666 | for token in &tokens { |
| 667 | let t = *token; |
| 668 | // Skip default value part |
| 669 | if t == "=" { |
| 670 | break; |
| 671 | } |
| 672 | if t.starts_with('$') || t.starts_with("&$") || t.starts_with("...$") { |
| 673 | // This is the variable name |
| 674 | let clean = t.trim_start_matches("...").trim_start_matches('&'); |
| 675 | name = Some(clean.to_string()); |
| 676 | break; |
| 677 | } |
| 678 | // Skip constructor promotion modifiers — they are not type hints |
| 679 | match t.to_lowercase().as_str() { |
| 680 | "public" | "protected" | "private" | "static" | "readonly" => continue, |
| 681 | _ => {} |
| 682 | } |
| 683 | // Otherwise it's (part of) the type hint |
| 684 | if let Some(existing) = type_hint { |
| 685 | // Union/intersection types with spaces shouldn't happen, |
| 686 | // but handle it gracefully |
| 687 | type_hint = Some(PhpType::parse(&format!("{}{}", existing, t))); |
| 688 | } else { |
| 689 | type_hint = Some(PhpType::parse(t)); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if let Some(n) = name { |
| 694 | result.push((type_hint, n)); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | result |
| 699 | } |
| 700 | |
| 701 | /// Extract the return type from the portion after `)` in a function declaration. |
| 702 | /// |