use crate::{
query::{adj_ast, label_text},
syntax::{
builders::method_invocation::{build_method_invocation_node, DirectChildren},
fields::php::scoped_call_expression,
SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
use alloc::format;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let mut expr_id = args.required_field_alt(n_id, scoped_call_expression::SCOPE)?;
if graph.label_type(expr_id) == Some("relative_scope") {
if let Some(&first) = adj_ast(graph, expr_id, Some(1), &[]).first() {
expr_id = first;
}
}
let raw_expr = node_to_str(graph, expr_id);
let method_name_n_id = args.required_field_alt(n_id, scoped_call_expression::NAME)?;
let method_name = label_text(graph, method_name_n_id).unwrap_or("").to_owned();
let expr = format!("{raw_expr}::{method_name}");
let args_node = args.required_field_alt(n_id, scoped_call_expression::ARGUMENTS)?;
let arguments_id = adj_ast(graph, args_node, Some(1), &[])
.iter()
.any(|&c_id| !matches!(graph.label_type(c_id), Some("(" | "," | ")")))
.then_some(args_node);
let direct_children = DirectChildren {
expression_id: Some(expr_id),
arguments_id,
..DirectChildren::default()
};
build_method_invocation_node(args, n_id, expr, direct_children, None).map(Some)
}