$ show nikic/php-ast
The php-ast repository is a PHP extension that exposes the internal Abstract Syntax Tree (AST) used by PHP 7 and 8. It provides functions to parse PHP code into an inspectable AST structure that mirrors PHP's internal compiler representation, enabling static analysis, code transformation, refactoring tools, and other metaprogramming tasks. Unlike pure-PHP AST parsers, this extension leverages PHP's own parser for maximum performance and accuracy. The extension implements a sophisticated versioning system to maintain compatibility across PHP releases. Rather than breaking when PHP introduces new language features, it defines AST versions (currently 50 through 120) and can represent newer constructs using older node types when necessary for backward compatibility. This allows developers to write analysis tools that work across multiple PHP versions without constantly updating for each new release. Key architectural components include the C-based extension core that interfaces with Zend's parser, a comprehensive metadata system describing node kinds and their associated flags, and utility functions for AST inspection. The extension handles the full PHP grammar including recent additions like attributes, match expressions, nullsafe operators, constructor property promotion, and enum support. In production use, this extension serves as the foundation for many PHP static analysis tools, enabling them to operate with near-native performance by avoiding the overhead of pure-PHP parsing while providing a stable, versioned interface to PHP's AST structure.
The LLM identified 1 files worth highlighting for reuse:
Contains the ast_dump() function and related helpers for formatting and inspecting AST structures. While it depends on a few functions from the ast extension (get_metadata, kind_uses_flags, get_kind_name), the core tree traversal and formatting logic is self-contained and demonstrates clean separation of concerns. It handles various data types properly (objects, arrays, strings, null) and provides configurable output options through bit flags. The recursive algorithm for tree formatting could be adapted for other tree-structured data with minimal changes by replacing the extension-specific dependency functions.