explaingit

nikic/php-parser

17,430PHPAudience · developerComplexity · 3/5Setup · easy

TLDR

PHP library that parses PHP 7 and 8 source code into an Abstract Syntax Tree, lets you transform it with visitors, and prints it back as PHP.

Mindmap

mindmap
  root((PHP-Parser))
    Inputs
      PHP source string
      PHP file path
    Outputs
      AST objects
      Modified PHP source
      JSON-serialized AST
    Use Cases
      Build a static analyzer
      Write a code refactor tool
      Generate or rewrite PHP files
    Tech Stack
      PHP
      Composer
      AST
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Build a static analyzer that flags unused functions in a PHP codebase

USE CASE 2

Write a refactor script that renames a class everywhere it is used

USE CASE 3

Generate PHP files from a template by constructing AST nodes programmatically

USE CASE 4

Serialize PHP code to JSON for inspection from another language

Tech stack

PHPComposer

Getting it running

Difficulty · easy Time to first run · 30min

Needs PHP 7.4 or later on the host running the parser, even though it can parse older PHP code.

In plain English

PHP-Parser is a PHP library that can read and analyze PHP source code as structured data. It takes a PHP file or code string and converts it into an AST (Abstract Syntax Tree, a structured, tree-shaped representation of the code that's easy for programs to work with). This is the foundation for tools that inspect, transform, or generate PHP code programmatically. The library is written in PHP itself, which means it runs inside any PHP project without external dependencies. You install it via Composer (PHP's package manager) and use it to parse PHP 7 and PHP 8 code into an AST, traverse and modify that AST using visitor classes, convert the modified AST back to PHP source code (including options to preserve original formatting), evaluate constant expressions, resolve namespaces, and serialize the AST to JSON. The quickstart example in the README shows exactly how this works: you write a parser, call parse() on a PHP code string, get back an AST object, walk through it to make changes (the example removes all function bodies), then print the modified AST back out as valid PHP code. This kind of tool is used when building static analysis tools (programs that inspect code for errors or patterns without running it), code formatters, refactoring tools, code generators, or IDEs that need to understand code structure. The library supports PHP 7.0 through PHP 8.4 for parsing, and requires PHP 7.4 or higher to run the current version (5.x).

Copy-paste prompts

Prompt 1
Show me a minimal PHP-Parser script that parses a file and prints every function name it finds
Prompt 2
Write a node visitor with PHP-Parser that renames a class across a folder of PHP files
Prompt 3
Walk me through installing PHP-Parser with Composer and running the README quickstart
Prompt 4
Use PHP-Parser to remove all function bodies from a PHP file while preserving the original formatting
Prompt 5
Compare PHP-Parser pretty printers and explain how to keep original whitespace when emitting code
Open on GitHub → Explain another repo

← nikic on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.