A PHP package that parses PHP source into an abstract syntax tree for analysis and code transformation. It targets static analysis and programmatic rewriting workflows.
Overview
PHP Parser is a parser implemented in PHP whose role is to simplify static analysis and source manipulation. You install it as a Composer dependency and use it from PHP application or tooling code.
It parses modern PHP into an AST, including partial trees when input is syntactically invalid. Nodes carry accurate source locations. You can dump ASTs for inspection, pretty-print them back to PHP, and preserve formatting when only part of the tree changes.
The library includes traversal and visitor infrastructure, namespace name resolution, evaluation of constant expressions, and fluent builders for generating nodes. ASTs can serialize to JSON and round-trip again for tooling pipelines.
Documentation covers walking and modifying trees, short-circuiting traversals, parent and sibling references, lexer behavior, error recovery, and performance considerations such as reusing objects and optional Xdebug disabling.
Key features
- PHP 7 and 8 AST parsing
- Partial parse on invalid code
- AST dump and pretty-print
- Formatting-preserving transforms
- Visitors and traversal APIs
- Namespace resolution
- Constant expression evaluation
- JSON AST encoding
Use cases
- Building static analysis tools
- Automated PHP refactors
- Code generators
- Linting or custom fixers