PHP Parser is a static code analysis library that parses PHP code into an abstract syntax tree.
The tool addresses the need to programmatically analyze and manipulate PHP source code. It works by parsing PHP 7, PHP 8, and limited PHP 5.x syntax into an AST, then providing infrastructure to traverse, modify, and convert that tree back to PHP code. The parser can handle invalid or incomplete code, producing partial ASTs with accurate location information. Key capabilities include name resolution for namespaced identifiers, evaluation of constant expressions, AST builders for code generation, and formatting-preserving transformations that maintain whitespace and comments when modifying code.
Developers should choose this tool for static analysis tasks, code generation, refactoring, and linting applications where understanding PHP code structure is essential. It suits projects that need to inspect or transform PHP source without executing it. The library supports multiple PHP versions simultaneously—version 5.x runs on PHP 7.4 and later while parsing PHP 7.0 through 8.4, and version 4.x runs on PHP 7.0 and later while parsing PHP 5.2 through 8.3. The documentation covers walking and modifying ASTs, name resolution, pretty-printing with format preservation, error handling with recovery for malformed code, and performance optimization strategies.
The project maintains a substantial base of real-world adopters, as evidenced by the fact that nearly all open issues originate from external users rather than the core team. Maintainers typically respond to new issues and pull requests within a day.