-
-
Save mattsches/4d3e2cd2d766ab42071f to your computer and use it in GitHub Desktop.
Create a simple, human-readable AST dump using the `php-ast` extension and its `util.php` helper file. Requires PHP 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'path/to/php-ast/util.php'; | |
$code = <<<'EOC' | |
<?php | |
$random = random_int(0,1); | |
if ($random === 1) { | |
echo 'EINS'; | |
} else { | |
echo 'NICHT EINS'; | |
} | |
EOC; | |
echo ast_dump(ast\parse_code($code, 30)), "\n"; | |
// Output: | |
AST_STMT_LIST | |
0: AST_ASSIGN | |
var: AST_VAR | |
name: "random" | |
expr: AST_CALL | |
expr: AST_NAME | |
flags: NAME_NOT_FQ (1) | |
name: "random_int" | |
args: AST_ARG_LIST | |
0: 0 | |
1: 1 | |
1: AST_IF | |
0: AST_IF_ELEM | |
cond: AST_BINARY_OP | |
flags: BINARY_IS_IDENTICAL (15) | |
left: AST_VAR | |
name: "random" | |
right: 1 | |
stmts: AST_STMT_LIST | |
0: AST_ECHO | |
expr: "EINS" | |
1: AST_IF_ELEM | |
cond: null | |
stmts: AST_STMT_LIST | |
0: AST_ECHO | |
expr: "NICHT EINS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment