1111 * children are never materialized into PHP arrays unless something actually
1212 * asks for them.
1313 *
14- * The hedge in those methods (`if ( $this->has_unmaterialized_native_ast() )`)
15- * is NOT a runtime check for whether the native extension is loaded — if this
16- * class is in use, the extension is loaded by definition. It checks whether
17- * THIS specific node still has an authoritative native AST behind it. A node
18- * loses its native backing the first time it is mutated from PHP via
19- * `append_child()` or `merge_fragment()`: those overrides call
20- * `materialize_native_children()`, which copies the native children into the
21- * inherited `$children` array and then drops the native AST reference. From
22- * that point on, the node is a plain PHP-backed `WP_Parser_Node` and the read
23- * methods fall through to the parent implementation.
14+ * Read methods eagerly call `materialize_native_children()` — once the
15+ * children have been copied into PHP, `was_mutated()` returns true and the
16+ * call falls through to the parent implementation. The `was_mutated` flag is
17+ * NOT a runtime check for whether the native extension is loaded — if this
18+ * class is in use, the extension is loaded by definition. It tracks whether
19+ * THIS specific node has had its children pulled into the inherited
20+ * `$children` array (which happens on first read or first mutation via
21+ * `append_child()` / `merge_fragment()`). From that point on, the node is a
22+ * plain PHP-backed `WP_Parser_Node`.
2423 *
2524 * Mutation from PHP is real and intentional — query rewriters in
2625 * `WP_PDO_MySQL_On_SQLite` (e.g. building synthetic `count(*)` expressions)
3130class WP_MySQL_Native_Parser_Node extends WP_Parser_Node {
3231 private $ native_ast = null ;
3332 private $ native_node_index = null ;
33+ private $ was_mutated = false ;
3434
3535 public function __construct ( $ rule_id , $ rule_name , $ native_ast = null , $ native_node_index = null ) {
3636 parent ::__construct ( $ rule_id , $ rule_name );
@@ -162,18 +162,18 @@ public function get_length(): int {
162162 }
163163
164164 /**
165- * Indicates whether this node still has an unmaterialized native AST .
165+ * Indicates whether this node has been mutated from PHP .
166166 *
167- * Returns true for freshly-parsed nodes whose children live in the
168- * Rust-owned AST buffer; returns false once the node has been mutated and
169- * its children copied into the inherited `$ children` array (see
170- * self::materialize_native_children()) .
167+ * Returns false for freshly-parsed nodes whose children still live in the
168+ * Rust-owned AST buffer; returns true once `append_child()` or
169+ * `merge_fragment()` has copied the children into the inherited
170+ * `$children` array and dropped the native AST reference .
171171 *
172172 * This is a per-instance state check, not a check for whether the native
173173 * extension is loaded.
174174 */
175- private function has_unmaterialized_native_ast (): bool {
176- return null !== $ this ->native_ast ;
175+ private function was_mutated (): bool {
176+ return $ this ->was_mutated ;
177177 }
178178
179179 /**
@@ -182,16 +182,17 @@ private function has_unmaterialized_native_ast(): bool {
182182 *
183183 * Called before any mutation (append_child, merge_fragment) so the node's
184184 * authoritative state lives in PHP from that point on. After this runs,
185- * has_unmaterialized_native_ast () returns false and read methods fall
186- * through to the parent WP_Parser_Node implementation.
185+ * was_mutated () returns true and read methods fall through to the parent
186+ * WP_Parser_Node implementation.
187187 */
188188 private function materialize_native_children (): void {
189- if ( ! $ this ->has_unmaterialized_native_ast () ) {
189+ if ( $ this ->was_mutated ) {
190190 return ;
191191 }
192192
193193 $ this ->children = wp_sqlite_mysql_native_ast_get_children ( $ this ->native_ast , $ this ->native_node_index );
194194 $ this ->native_ast = null ;
195195 $ this ->native_node_index = null ;
196+ $ this ->was_mutated = true ;
196197 }
197198}
0 commit comments