use anyhow::{Context, Result};
use streaming_iterator::StreamingIterator;
use tree_sitter::{Language, Node, Parser, Query, QueryCursor, Tree};
pub fn parse(source: &str) -> Result<Tree> {
let mut parser = Parser::new();
parser
.set_language(&language())
.context("loading the PHP grammar")?;
parser
.parse(source, None)
.context("tree-sitter failed to produce a parse tree")
}
fn language() -> Language {
tree_sitter_php::LANGUAGE_PHP.into()
}
pub fn has_syntax_error(tree: &Tree) -> bool {
fn walk(node: tree_sitter::Node) -> bool {
if node.is_error() {
return true;
}
let mut cursor = node.walk();
let children: Vec<_> = node.children(&mut cursor).collect();
children.into_iter().any(walk)
}
walk(tree.root_node())
}
#[derive(Debug, Clone)]
pub struct Capture {
pub name: String,
pub text: String,
}
pub fn run_query(tree: &Tree, source: &str, query_source: &str) -> Result<Vec<Vec<Capture>>> {
let query = Query::new(&language(), query_source).context("compiling tree-sitter query")?;
let capture_names = query.capture_names();
let mut cursor = QueryCursor::new();
let bytes = source.as_bytes();
let mut results = Vec::new();
let mut matches = cursor.matches(&query, tree.root_node(), bytes);
while let Some(m) = matches.next() {
let mut captures = Vec::with_capacity(m.captures.len());
for capture in m.captures {
let name = capture_names[capture.index as usize].to_string();
let text = capture
.node
.utf8_text(bytes)
.unwrap_or_default()
.to_string();
captures.push(Capture { name, text });
}
results.push(captures);
}
Ok(results)
}
pub fn query_nodes<'a>(
tree: &'a Tree,
source: &str,
query_source: &str,
capture_name: &str,
) -> Result<Vec<Node<'a>>> {
let query = Query::new(&language(), query_source).context("compiling tree-sitter query")?;
let Some(target_index) = query
.capture_names()
.iter()
.position(|name| *name == capture_name)
else {
return Ok(Vec::new());
};
let mut cursor = QueryCursor::new();
let bytes = source.as_bytes();
let mut results = Vec::new();
let mut matches = cursor.matches(&query, tree.root_node(), bytes);
while let Some(m) = matches.next() {
for capture in m.captures {
if capture.index as usize == target_index {
results.push(capture.node);
}
}
}
Ok(results)
}
pub fn capture<'a>(captures: &'a [Capture], name: &str) -> Option<&'a str> {
captures
.iter()
.find(|c| c.name == name)
.map(|c| c.text.as_str())
}
pub fn captures_named<'a>(captures: &'a [Capture], name: &str) -> Vec<&'a str> {
captures
.iter()
.filter(|c| c.name == name)
.map(|c| c.text.as_str())
.collect()
}
pub fn unquote(literal: &str) -> String {
let trimmed = literal.trim();
if trimmed.len() >= 2 {
let bytes = trimmed.as_bytes();
let first = bytes[0];
let last = bytes[bytes.len() - 1];
if (first == b'\'' && last == b'\'') || (first == b'"' && last == b'"') {
return trimmed[1..trimmed.len() - 1].to_string();
}
}
trimmed.to_string()
}
#[derive(Debug, Clone)]
pub struct CallStep {
pub scope: Option<String>,
pub method: String,
pub args: Vec<String>,
}
pub fn walk_call_chain(node: Node, source: &str) -> Option<Vec<CallStep>> {
let bytes = source.as_bytes();
match node.kind() {
"member_call_expression" => {
let object = node.child_by_field_name("object")?;
let mut chain = walk_call_chain(object, source).unwrap_or_default();
let name = node
.child_by_field_name("name")?
.utf8_text(bytes)
.ok()?
.to_string();
chain.push(CallStep {
scope: None,
method: name,
args: call_argument_texts(node, bytes),
});
Some(chain)
}
"scoped_call_expression" => {
let scope = node
.child_by_field_name("scope")?
.utf8_text(bytes)
.ok()?
.to_string();
let name = node
.child_by_field_name("name")?
.utf8_text(bytes)
.ok()?
.to_string();
Some(vec![CallStep {
scope: Some(scope),
method: name,
args: call_argument_texts(node, bytes),
}])
}
_ => None,
}
}
fn children_vec(node: Node) -> Vec<Node> {
let mut cursor = node.walk();
let children: Vec<Node> = node.children(&mut cursor).collect();
children
}
fn first_child(node: Node) -> Option<Node> {
children_vec(node).into_iter().next()
}
pub fn direct_children_of_kind<'a>(node: Node<'a>, kind: &str) -> Vec<Node<'a>> {
children_vec(node)
.into_iter()
.filter(|c| c.kind() == kind)
.collect()
}
fn call_argument_texts(node: Node, bytes: &[u8]) -> Vec<String> {
let Some(arguments) = node.child_by_field_name("arguments") else {
return Vec::new();
};
children_vec(arguments)
.into_iter()
.filter(|c| c.kind() == "argument")
.filter_map(|arg| first_child(arg).map(|expr| text(expr, bytes)))
.collect()
}
fn text(node: Node, bytes: &[u8]) -> String {
node.utf8_text(bytes).unwrap_or_default().to_string()
}
pub fn argument_node(node: Node, index: usize) -> Option<Node> {
let arguments = node.child_by_field_name("arguments")?;
let nth = children_vec(arguments)
.into_iter()
.filter(|c| c.kind() == "argument")
.nth(index)?;
first_child(nth)
}
pub fn statement_expressions(body: Node) -> Vec<Node> {
children_vec(body)
.into_iter()
.filter(|c| c.kind() == "expression_statement")
.filter_map(first_child)
.collect()
}
pub fn closure_body(node: Node) -> Option<Node> {
if node.kind() != "anonymous_function" {
return None;
}
node.child_by_field_name("body")
}
pub fn find_ancestor<'a>(node: Node<'a>, kind: &str) -> Option<Node<'a>> {
let mut current = node.parent();
while let Some(candidate) = current {
if candidate.kind() == kind {
return Some(candidate);
}
current = candidate.parent();
}
None
}
pub fn find_method<'a>(
tree: &'a Tree,
source: &str,
class_name: &str,
method_name: &str,
) -> Option<Node<'a>> {
let query = r#"(method_declaration name: (name) @name) @method"#;
let bytes = source.as_bytes();
let candidates = query_nodes(tree, source, query, "method").ok()?;
for method_node in candidates {
let name_node = method_node.child_by_field_name("name")?;
if name_node.utf8_text(bytes).ok()? != method_name {
continue;
}
let class_decl = find_ancestor(method_node, "class_declaration")?;
let class_name_node = class_decl.child_by_field_name("name")?;
if class_name_node.utf8_text(bytes).ok()? == class_name {
return Some(method_node);
}
}
None
}
pub fn return_expression(node: Node) -> Option<Node> {
if node.kind() != "return_statement" {
return None;
}
node.named_child(0)
}
pub fn body_as_comment(body: Node, source: &str) -> String {
let text = body.utf8_text(source.as_bytes()).unwrap_or("");
let inner = text
.trim()
.strip_prefix('{')
.unwrap_or(text)
.strip_suffix('}')
.unwrap_or(text)
.trim();
let mut out = String::from(
" // Original Laravel method body, preserved for reference - not translated:\n",
);
for line in inner.lines() {
out.push_str(&format!(" // {}\n", line.trim()));
}
out
}
pub fn find_class<'a>(tree: &'a Tree, source: &str, class_name: &str) -> Option<Node<'a>> {
let query = r#"(class_declaration name: (name) @name) @class"#;
let bytes = source.as_bytes();
let candidates = query_nodes(tree, source, query, "class").ok()?;
candidates.into_iter().find(|node| {
node.child_by_field_name("name")
.and_then(|n| n.utf8_text(bytes).ok())
== Some(class_name)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_a_simple_php_file_without_error() {
let tree = parse("<?php\n\n$x = 1;\n").unwrap();
assert!(!has_syntax_error(&tree));
}
#[test]
fn detects_a_syntax_error() {
let tree = parse("<?php\n\nRoute::get('/x', [PostController::class 'index']);\n").unwrap();
assert!(has_syntax_error(&tree));
}
#[test]
fn find_method_locates_a_method_by_class_and_name() {
let source = "<?php\nclass Post {\n public function index() { return 1; }\n}\nclass User {\n public function index() { return 2; }\n}\n";
let tree = parse(source).unwrap();
let method = find_method(&tree, source, "User", "index").unwrap();
let body = method.child_by_field_name("body").unwrap();
assert!(body
.utf8_text(source.as_bytes())
.unwrap()
.contains("return 2"));
}
#[test]
fn find_method_returns_none_for_a_missing_method() {
let source = "<?php\nclass Post {\n public function index() {}\n}\n";
let tree = parse(source).unwrap();
assert!(find_method(&tree, source, "Post", "show").is_none());
}
#[test]
fn find_class_locates_a_class_by_name() {
let source = "<?php\nclass Post {}\nclass User {}\n";
let tree = parse(source).unwrap();
let class = find_class(&tree, source, "User").unwrap();
assert_eq!(class.kind(), "class_declaration");
}
#[test]
fn find_class_returns_none_for_a_missing_class() {
let source = "<?php\nclass Post {}\n";
let tree = parse(source).unwrap();
assert!(find_class(&tree, source, "User").is_none());
}
#[test]
fn return_expression_unwraps_a_return_statement() {
let source = "<?php\nclass X {\n public function f() { return $this->hasMany(Post::class); }\n}\n";
let tree = parse(source).unwrap();
let method = find_method(&tree, source, "X", "f").unwrap();
let body = method.child_by_field_name("body").unwrap();
assert!(statement_expressions(body).is_empty());
let return_stmt = direct_children_of_kind(body, "return_statement")
.into_iter()
.next()
.unwrap();
let expr = return_expression(return_stmt).unwrap();
assert_eq!(expr.kind(), "member_call_expression");
}
#[test]
fn unquote_strips_single_and_double_quotes() {
assert_eq!(unquote("'posts'"), "posts");
assert_eq!(unquote("\"posts\""), "posts");
assert_eq!(unquote("posts"), "posts");
}
#[test]
fn walk_call_chain_unwraps_a_member_call_chain() {
let source = "<?php\n\n$table->foreignId('user_id')->constrained()->nullable();\n";
let tree = parse(source).unwrap();
let program = tree.root_node();
let stmt = program.child(1).unwrap();
let expr = stmt.child(0).unwrap();
let chain = walk_call_chain(expr, source).unwrap();
assert_eq!(chain.len(), 3);
assert_eq!(chain[0].method, "foreignId");
assert_eq!(chain[0].args, vec!["'user_id'".to_string()]);
assert_eq!(chain[1].method, "constrained");
assert_eq!(chain[2].method, "nullable");
}
#[test]
fn walk_call_chain_captures_scope_on_the_base_call() {
let source = "<?php\n\nRoute::get('/posts', $h)->name('posts.index');\n";
let tree = parse(source).unwrap();
let program = tree.root_node();
let stmt = program.child(1).unwrap();
let expr = stmt.child(0).unwrap();
let chain = walk_call_chain(expr, source).unwrap();
assert_eq!(chain.len(), 2);
assert_eq!(chain[0].scope.as_deref(), Some("Route"));
assert_eq!(chain[0].method, "get");
assert_eq!(chain[1].scope, None);
assert_eq!(chain[1].method, "name");
assert_eq!(chain[1].args, vec!["'posts.index'".to_string()]);
}
#[test]
fn run_query_finds_a_static_method_call() {
let source = "<?php\n\nRoute::get('/posts', [PostController::class, 'index']);\n";
let tree = parse(source).unwrap();
let query = r#"
(scoped_call_expression
scope: (name) @class
name: (name) @method) @call
"#;
let matches = run_query(&tree, source, query).unwrap();
assert!(!matches.is_empty());
let first = &matches[0];
assert_eq!(capture(first, "class"), Some("Route"));
assert_eq!(capture(first, "method"), Some("get"));
}
}