Skip to main content

Module php

Module php 

Source
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§

CallStep
One link in a fluent method-call chain - $table->string('x') or Route::get('/x', ...) (a scoped_call_expression, scope: Some) vs. ->nullable()/->name(...) (a member_call_expression chained off a preceding call, scope: None).
Capture
One matched query capture: the capture’s name (from (... ) @name in the .scm source) and its exact source text.

Functions§

argument_node
The raw (unwrapped-from-argument) expression node at position index of node’s call arguments - used instead of CallStep::args when 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 body node’s original PHP source as a comment block - the shared “preserve, never translate” primitive used by controllers.rs/policies.rs/jobs.rs for 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 (None if 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 body field of an anonymous_function node (itself a compound_statement) - None if node isn’t an anonymous function.
direct_children_of_kind
Every direct child of node with the given kind - for a caller that already has a specific node in hand (e.g. one particular rules() method’s own array literal, found via find_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 own array_element_initializer children, which a naive (array_element_initializer) @entry query run from the tree root would also match) - direct-children iteration has no such risk since it only ever looks at node’s own immediate children.
find_ancestor
Walks upward from node to the nearest ancestor of kind kind - used instead of a query predicate (e.g. matching a rules() 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. None if no such ancestor exists.
find_class
The class_declaration named class_name, anywhere in tree - 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_declaration named method_name inside the class_ declaration named class_name - queries broadly for every method_declaration by kind, then filters by name and climbs to its enclosing class via find_ancestor (the same “query broadly, check/ climb from each match” shape requests.rs already established for rules(), 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
true if any node in tree is a tree-sitter ERROR node - 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 source as 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 matched Nodes 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_statement wraps - None if node isn’t a return statement, or it’s a bare return; with nothing to return.
run_query
Runs query_source against tree, returning one Vec<Capture> per match (in source order) - the shape every converter builds its extraction logic on top of, so no converter module touches the raw tree-sitter API directly.
statement_expressions
Every top-level expression_statement’s inner expression node, directly inside body (a compound_statement, e.g. an anonymous function’s block) - used to enumerate each $table->...; line in a migration closure, or each Route::...; 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 base scoped_call_expression, e.g. Route::get('/x', $h)->name('x')->middleware($m)) from the base of the chain outward. Returns None if node isn’t a call-chain node at all. Used by both migrations.rs ($table->id()->nullable() chains) and routes.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.