-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Input php code
<?php
class Match
{
}
Now when i look at the AST and ignore the wrapping array (just to clear additional clutter in the following code snippets), i get these outputs:
var_export($ast)
PhpParser\Node\Stmt\Class_::__set_state(array(
'flags' => 0,
'extends' => NULL,
'implements' =>
array (
),
'name' =>
PhpParser\Node\Identifier::__set_state(array(
'name' => 'MyClass',
'attributes' =>
array (
'startLine' => 3,
'endLine' => 3,
),
)),
'stmts' =>
array (
),
'attributes' =>
array (
'startLine' => 3,
'endLine' => 5,
),
)
dumper
Stmt_Class(
flags: 0
name: Identifier(
name: Match
)
extends: null
implements: array(
)
stmts: array(
)
)
What i like to have is a dumper that prints PHP code that can construct the AST
new PhpParser\Node\Stmt\Class_('MyClass');
For this the dumper should have some knowledge about the node constructor argument defaults, so that no unneeded arguments are given. For example if the same class had some statements in it, it would need to be:
new PhpParser\Node\Stmt\Class_('MyClass', ['stmts' => [
/* actual statements here*/
]]);
This would be a very handy tool to create PHP from AST nodes with the help of a "static written" php file used for prototyping dynamic php code creation. While the export of var_export is probably runnable php code .. it's very hard to create code based on this because it's very verbose and __set_state is probably not the best thing to use here either.