// the find
nikic/PHP-Parser
A PHP parser written in PHP
nikic/PHP-Parser is the de facto standard for parsing PHP source into an AST, used under the hood by PHPStan, Psalm, PHP-CS-Fixer, and Rector. If you're writing a static analysis tool, a code transformation pipeline, or anything that needs to read or rewrite PHP code programmatically, this is what you reach for — there's no real competition. It handles everything from PHP 5.x through 8.4.
The formatting-preserving printer is genuinely impressive: you can modify a subtree and round-trip it back to code without mangling the surrounding whitespace and comments. The emulative lexer lets you parse PHP 8.x syntax on a PHP 7 runtime, which matters for tooling that ships independently of the projects it analyzes. Error recovery is solid — it produces a partial AST even for broken code, which is exactly what you need in an IDE integration or linter. The visitor/traverser pattern is well-designed; you can stack multiple visitors in one pass and short-circuit early, which keeps transforms fast on large codebases.
The AST is mutable by default, which makes it easy to accidentally corrupt a tree mid-traversal if you're not careful about cloning. There's no type inference built in — you get structural information, not semantic information, so figuring out what type a variable holds at a given point is your problem to solve (that's what PHPStan layers on top). The `PrettyPrinter\Standard` output style is non-configurable in terms of brace placement and indentation, so if you're generating new code rather than transforming existing code, you're stuck with its opinions. Documentation is good but the upgrade guides between major versions are the only place to find reasoning behind API design decisions — the inline API docs are thin.