use crate::php;
use tree_sitter::{Node, Tree};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstructorField {
pub name: String,
pub rust_type: String,
}
pub fn extract(
tree: &Tree,
source: &str,
class_name: &str,
) -> Result<Vec<ConstructorField>, String> {
let Some(constructor) = php::find_method(tree, source, class_name, "__construct") else {
return Ok(Vec::new());
};
let Some(parameters) = constructor.child_by_field_name("parameters") else {
return Ok(Vec::new());
};
let mut fields = Vec::new();
for param in php::direct_children_of_kind(parameters, "property_promotion_parameter") {
let type_node = param
.child_by_field_name("type")
.ok_or_else(|| "a constructor-promoted property is missing a type hint".to_string())?;
let name = parameter_name(param, source)
.ok_or_else(|| "a constructor-promoted property is missing a name".to_string())?;
let rust_type = map_type(type_node, source)?;
fields.push(ConstructorField {
name: crate::codegen::to_snake_case(&name),
rust_type,
});
}
let classic_candidates: Vec<(String, Node)> =
php::direct_children_of_kind(parameters, "simple_parameter")
.into_iter()
.filter_map(|param| {
let type_node = param.child_by_field_name("type")?;
let name = parameter_name(param, source)?;
Some((name, type_node))
})
.collect();
if !classic_candidates.is_empty() {
let Some(body) = constructor.child_by_field_name("body") else {
return Ok(fields);
};
for (name, type_node) in classic_candidates {
if has_self_assignment(body, &name, source) {
let rust_type = map_type(type_node, source)?;
fields.push(ConstructorField {
name: crate::codegen::to_snake_case(&name),
rust_type,
});
}
}
}
Ok(fields)
}
fn parameter_name(param: Node, source: &str) -> Option<String> {
let name_node = param.child_by_field_name("name")?;
let inner = name_node.named_child(0)?;
Some(inner.utf8_text(source.as_bytes()).ok()?.to_string())
}
fn has_self_assignment(body: Node, param_name: &str, source: &str) -> bool {
let bytes = source.as_bytes();
php::statement_expressions(body).into_iter().any(|stmt| {
stmt.kind() == "assignment_expression"
&& is_this_property(stmt.child_by_field_name("left"), param_name, bytes)
&& is_matching_variable(stmt.child_by_field_name("right"), param_name, bytes)
})
}
fn is_this_property(left: Option<Node>, param_name: &str, bytes: &[u8]) -> bool {
let Some(left) = left else { return false };
if left.kind() != "member_access_expression" {
return false;
}
let Some(object) = left.child_by_field_name("object") else {
return false;
};
if object.kind() != "variable_name" {
return false;
}
let Some(object_name) = object.named_child(0).and_then(|n| n.utf8_text(bytes).ok()) else {
return false;
};
if object_name != "this" {
return false;
}
left.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
== Some(param_name)
}
fn is_matching_variable(right: Option<Node>, param_name: &str, bytes: &[u8]) -> bool {
let Some(right) = right else { return false };
if right.kind() != "variable_name" {
return false;
}
right.named_child(0).and_then(|n| n.utf8_text(bytes).ok()) == Some(param_name)
}
fn map_type(type_node: Node, source: &str) -> Result<String, String> {
let bytes = source.as_bytes();
match type_node.kind() {
"primitive_type" => {
let text = type_node.utf8_text(bytes).map_err(|e| e.to_string())?;
match text {
"int" => Ok("i64".to_string()),
"string" => Ok("String".to_string()),
"bool" => Ok("bool".to_string()),
"float" => Ok("f64".to_string()),
other => Err(format!("unsupported primitive type `{other}`")),
}
}
"named_type" => {
let name = type_node
.named_child(0)
.and_then(|n| n.utf8_text(bytes).ok())
.unwrap_or("?");
Err(format!(
"class type hints aren't supported for event/job fields (`{name}`) - reference the model's id instead (e.g. `int ${{name}}Id`)"
))
}
"optional_type" | "union_type" => Err(format!(
"nullable/union type hints aren't supported: `{}`",
type_node.utf8_text(bytes).unwrap_or("")
)),
other => Err(format!("unsupported type hint shape `{other}`")),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extracts_constructor_promoted_properties() {
let source = "<?php\nclass OrderShipped {\n public function __construct(public int $orderId, public string $carrier) {}\n}\n";
let tree = php::parse(source).unwrap();
let fields = extract(&tree, source, "OrderShipped").unwrap();
assert_eq!(
fields,
vec![
ConstructorField {
name: "order_id".to_string(),
rust_type: "i64".to_string(),
},
ConstructorField {
name: "carrier".to_string(),
rust_type: "String".to_string(),
},
]
);
}
#[test]
fn rejects_a_class_type_hint_on_a_promoted_property() {
let source = "<?php\nclass OrderShipped {\n public function __construct(public Order $order) {}\n}\n";
let tree = php::parse(source).unwrap();
assert!(extract(&tree, source, "OrderShipped").is_err());
}
#[test]
fn extracts_classic_property_plus_assignment_style() {
let source = "<?php\nclass InvoicePaid {\n public $invoiceId;\n public $amount;\n public function __construct($invoiceId, $amount)\n {\n $this->invoiceId = $invoiceId;\n $this->amount = $amount;\n }\n}\n";
let tree = php::parse(source).unwrap();
let fields = extract(&tree, source, "InvoicePaid").unwrap();
assert!(fields.is_empty());
}
#[test]
fn extracts_classic_style_with_typed_constructor_parameters() {
let source = "<?php\nclass X {\n public $orderId;\n public function __construct(int $orderId)\n {\n $this->orderId = $orderId;\n }\n}\n";
let tree = php::parse(source).unwrap();
let fields = extract(&tree, source, "X").unwrap();
assert_eq!(
fields,
vec![ConstructorField {
name: "order_id".to_string(),
rust_type: "i64".to_string(),
}]
);
}
#[test]
fn rejects_a_class_type_hint_in_classic_style() {
let source = "<?php\nclass X {\n public $order;\n public function __construct(Order $order)\n {\n $this->order = $order;\n }\n}\n";
let tree = php::parse(source).unwrap();
assert!(extract(&tree, source, "X").is_err());
}
#[test]
fn a_constructor_parameter_with_no_self_assignment_is_not_treated_as_a_field() {
let source = "<?php\nclass X {\n public function __construct(int $retries)\n {\n // used locally, never stored\n }\n}\n";
let tree = php::parse(source).unwrap();
let fields = extract(&tree, source, "X").unwrap();
assert!(fields.is_empty());
}
#[test]
fn a_class_with_no_constructor_yields_no_fields() {
let source = "<?php\nclass X {}\n";
let tree = php::parse(source).unwrap();
assert_eq!(extract(&tree, source, "X").unwrap(), vec![]);
}
#[test]
fn rejects_a_nullable_type_hint() {
let source =
"<?php\nclass X {\n public function __construct(public ?Order $order) {}\n}\n";
let tree = php::parse(source).unwrap();
assert!(extract(&tree, source, "X").is_err());
}
#[test]
fn rejects_a_union_type_hint() {
let source =
"<?php\nclass X {\n public function __construct(public Order|string $order) {}\n}\n";
let tree = php::parse(source).unwrap();
assert!(extract(&tree, source, "X").is_err());
}
#[test]
fn maps_every_primitive_type() {
let source = "<?php\nclass X {\n public function __construct(public int $a, public string $b, public bool $c, public float $d) {}\n}\n";
let tree = php::parse(source).unwrap();
let fields = extract(&tree, source, "X").unwrap();
assert_eq!(fields[0].rust_type, "i64");
assert_eq!(fields[1].rust_type, "String");
assert_eq!(fields[2].rust_type, "bool");
assert_eq!(fields[3].rust_type, "f64");
}
}