talira

PHP-Parser

A PHP parser written in PHP, for static analysis and code changes

open sourceBSD-3-Clausefree

PHP-Parser is a parser for PHP code written in PHP, made to simplify static analysis and code manipulation. It parses PHP 7 and PHP 8 code into an abstract syntax tree with accurate locations, even partial trees from invalid code, and can dump the tree, turn it back into PHP while keeping the formatting of unchanged parts, and convert it to JSON and back. It has visitors to traverse and modify trees, name resolution, constant expression evaluation and builders for generating code.

Problems it solves

  • parse PHP source code into an AST from a PHP script
  • write a codemod that rewrites PHP code but keeps the original formatting
  • build a static analysis check for a PHP codebase

Features

  • parses PHP 7 and PHP 8 into an AST
  • partial ASTs from invalid code
  • turns an AST back into PHP, keeping unchanged formatting
  • visitors to traverse and modify ASTs
  • namespaced name resolution
  • builders for code generation
  • AST to JSON and back

Limitations

  • Parsing of PHP 5.x code is limited in version 5.1