Repolode
nikic

nikic/PHP-Parser

A PHP parser written in PHP that turns PHP source code into an abstract syntax tree for static analysis and code manipulation.

Stars
17,467
Forks
1,145
Open issues
75
Last commit
yesterday

What is it?

PHP-Parser parses PHP 7 and PHP 8 code into an abstract syntax tree (AST), with location information attached to each node. It can also parse invalid code into a partial tree, which helps tools that need to work on code that is still being edited.

Beyond parsing, the library provides a node traverser with visitors for reading and modifying the tree, a pretty printer that converts an AST back into PHP code (preserving original formatting for partially changed trees), name resolution for namespaced names, and evaluation of constant expressions. Builders make it easier to generate AST nodes for code generation, and the tree can be serialized to JSON and back.

The library is installed via Composer and is used as the foundation for code analysis, refactoring and code generation tools in the PHP ecosystem. Version 5.x runs on PHP 7.4 and later.

Key features

  • 01Parses PHP 7 and PHP 8 code into an AST with accurate location information.
  • 02Recovers a partial AST from syntactically invalid code.
  • 03Converts an AST back into PHP code, preserving formatting for unchanged parts.
  • 04Provides visitors and a traverser for reading and modifying trees.
  • 05Resolves namespaced names and evaluates constant expressions.
  • 06Serializes ASTs to JSON and back.

Use cases

  • Building static analysis or linting tools for PHP codebases.
  • Writing automated refactoring scripts that rewrite PHP source files.
  • Generating PHP code programmatically from AST builders.
  • Analyzing PHP code for security or coding-standard issues.

Quick start

  1. 1

    Install the library with Composer

    php composer.phar require nikic/php-parser
  2. 2

    Parse PHP code into an AST and dump it, using the code sample in the README

Things to know

  • Requires PHP 7.4 or newer to run version 5.x; version 4.x needs PHP 7.0 or newer.

Topics

FAQ

What is PHP-Parser?
A PHP parser written in PHP that turns PHP source code into an abstract syntax tree for static analysis and code manipulation. PHP-Parser parses PHP 7 and PHP 8 code into an abstract syntax tree (AST), with location information attached to each node. It can also parse invalid code into a partial tree, which helps tools that need to work on code that is still being edited.
Is PHP-Parser free to use? What license does it have?
PHP-Parser is open source under the BSD-3-Clause license. Check the license terms before using it in commercial products.
Is PHP-Parser still maintained?
Actively maintained. The last commit to PHP-Parser was on Nov 5, 2026.
What can I use PHP-Parser for?
Building static analysis or linting tools for PHP codebases. Writing automated refactoring scripts that rewrite PHP source files. Generating PHP code programmatically from AST builders. Analyzing PHP code for security or coding-standard issues.
How do I install PHP-Parser?
Install the library with Composer: php composer.phar require nikic/php-parser Parse PHP code into an AST and dump it, using the code sample in the README
Where can I get PHP-Parser?
The source code and installation instructions are on GitHub: https://github.com/nikic/PHP-Parser

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects