This is a PHP 5.2 to PHP 8.0 parser written in PHP. Its purpose is to simplify static code analysis and manipulation. A parser is useful for static analysis, manipulation of code and basically any other application dealing with code programmatically. A parser constructs an Abstract Syntax Tree (AST) of the code and thus allows dealing with it in an abstract and robust way. As the parser is based on the tokens returned by token_get_all (which is only able to lex the PHP version it runs on), additionally a wrapper for emulating tokens from newer versions is provided. This allows to parse PHP 7.4 source code running on PHP 7.0, for example. This emulation is somewhat hacky and not perfect, but it should work well on any sane code. Support for pretty printing, which is the act of converting an AST into PHP code. Please note that "pretty printing" does not imply that the output is especially pretty.
Features
- Parsing PHP 5, PHP 7, and PHP 8 code into an abstract syntax tree (AST)
- Dumping the AST in human-readable form
- Converting an AST back to PHP code
- Infrastructure to traverse and modify ASTs
- Resolution of namespaced names
- Evaluation of constant expressions
- Builders to simplify AST construction for code generation
- Converting an AST into JSON and back