Expand description
Thin wrapper over tree-sitter/tree-sitter-php, shared by every
converter module. tree-sitter-php was chosen (over the alternative
crates surveyed - php-parser, a brand-new single-maintainer crate with
no independent evidence backing its own claims, and php-parser-rs,
explicitly alpha) for its error-tolerant CST: a syntax-error-adjacent
chunk of real-world PHP still parses into a walkable tree with a
detectable ERROR node, rather than aborting the whole file - the right
fit for a converter whose core rule is “never silently mistranslate.”
Every converter matches structure via tree-sitter’s own query language
(.scm patterns), not manual tree-walking - see each converter module
for its query source.
Structs§
- Call
Step - One link in a fluent method-call chain -
$table->string('x')orRoute::get('/x', ...)(ascoped_call_expression,scope: Some) vs.->nullable()/->name(...)(amember_call_expressionchained off a preceding call,scope: None). - Capture
- One matched query capture: the capture’s name (from
(... ) @namein the.scmsource) and its exact source text.
Functions§
- argument_
node - The raw (unwrapped-from-
argument) expression node at positionindexofnode’s call arguments - used instead ofCallStep::argswhen a caller needs to recurse into an argument’s own structure (e.g.Route::middleware(...)->group(function () { ... })’s closure body), not just its source text. - body_
as_ comment - Renders a method/closure
bodynode’s original PHP source as a comment block - the shared “preserve, never translate” primitive used bycontrollers.rs/policies.rs/jobs.rsfor exactly the same purpose (a reference for whoever ports the real logic by hand). Strips the wrapping{ }before splitting into lines - a single-line body ({ return $x; }) has both braces and the statement on one physical line, so relying on.lines()alone to separate them (as a multi-line body’s own formatting naturally does) wouldn’t work. - capture
- Convenience for the common case: a match’s single capture text by name
(
Noneif that capture didn’t fire in this match - some captures in a query are conditional on optional syntax, e.g.->nullable()). - captures_
named - Every capture matching
name, in source order - for queries where a construct can repeat within one match (e.g. every->method(...)call chained off one Blueprint column). - closure_
body - The
bodyfield of ananonymous_functionnode (itself acompound_statement) -Noneifnodeisn’t an anonymous function. - direct_
children_ of_ kind - Every direct child of
nodewith the given kind - for a caller that already has a specific node in hand (e.g. one particularrules()method’s own array literal, found viafind_ancestor) and wants its immediate entries, not a fresh tree-wide query. A tree-wide query anchored only by node kind (no specific node identity) risks also matching same-shaped nodes nested arbitrarily deeper in the same subtree (e.g. an array-form rule value like['required', 'max:255']has its ownarray_element_initializerchildren, which a naive(array_element_initializer) @entryquery run from the tree root would also match) - direct-children iteration has no such risk since it only ever looks atnode’s own immediate children. - find_
ancestor - Walks upward from
nodeto the nearest ancestor of kindkind- used instead of a query predicate (e.g. matching arules()method declaration by name) when it’s simpler to query broadly for a structural shape and then check/climb from each match than to encode the constraint in the query itself.Noneif no such ancestor exists. - find_
class - The
class_declarationnamedclass_name, anywhere intree- the shared “find a class by name” primitive every Phase 3 converter needs (models, controllers, policies, events, jobs all start here). - find_
method - The
method_declarationnamedmethod_nameinside theclass_ declarationnamedclass_name- queries broadly for everymethod_declarationby kind, then filters by name and climbs to its enclosing class viafind_ancestor(the same “query broadly, check/ climb from each match” shaperequests.rsalready established forrules(), rather than trying to correlate a class-name capture and a method-name capture from one query - this crate’s query helpers only return nodes for one named capture at a time). - has_
syntax_ error trueif any node intreeis a tree-sitterERRORnode - a syntax construct the grammar couldn’t make sense of. Callers use this to decide whether a file is safe to mechanically convert at all, or should be flagged for manual review instead of risking a partial/misleading translation.- parse
- Parses
sourceas a PHP file (the<?php ... ?>-tagged dialect, not the “assume the whole file is PHP with no tags” variant - every real Laravel source file opens with<?php). - query_
nodes - Like
run_query, but returns the matchedNodes under one named capture directly, rather than their text - for callers (migrations, routes) that need to keep walking the tree structurally from the match (e.g. into a closure argument’s own body) instead of just reading the matched text. - return_
expression - The single expression a
return_statementwraps -Noneifnodeisn’t a return statement, or it’s a barereturn;with nothing to return. - run_
query - Runs
query_sourceagainsttree, returning oneVec<Capture>per match (in source order) - the shape every converter builds its extraction logic on top of, so no converter module touches the rawtree-sitterAPI directly. - statement_
expressions - Every top-level
expression_statement’s inner expression node, directly insidebody(acompound_statement, e.g. an anonymous function’s block) - used to enumerate each$table->...;line in a migration closure, or eachRoute::...;line inside a->group(...)closure. - unquote
- Strips a PHP single- or double-quoted string literal’s surrounding
quotes. Laravel source is overwhelmingly single-quoted string literals
for route paths/table names/column names - this doesn’t attempt real
PHP string-escape unescaping (
\',\n, interpolation), since none of that shows up in the mechanically-regular constructs Phase 1 converts (a route path, a column name, a config key are never written with embedded escapes or interpolation in practice). - walk_
call_ chain - Walks a call-chain node (nested
member_call_expressions wrapping one basescoped_call_expression, e.g.Route::get('/x', $h)->name('x')->middleware($m)) from the base of the chain outward. ReturnsNoneifnodeisn’t a call-chain node at all. Used by bothmigrations.rs($table->id()->nullable() chains) androutes.rs(Route::get(…)->name(…) chains) - the two constructs this phase converts that both need arbitrary-depth chain unwrapping, which tree-sitter’s query language can’t express in one fixed pattern.