4 [![Build Status](https://travis-ci.org/nikic/PHP-Parser.svg?branch=master)](https://travis-ci.org/nikic/PHP-Parser) [![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
6 This is a PHP 5.2 to PHP 7.2 parser written in PHP. Its purpose is to simplify static code analysis and
9 [**Documentation for version 4.x**][doc_master] (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 7.2).
11 [Documentation for version 3.x][doc_3_x] (stable; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
16 The main features provided by this library are:
18 * Parsing PHP 5 and PHP 7 code into an abstract syntax tree (AST).
19 * Invalid code can be parsed into a partial AST.
20 * The AST contains accurate location information.
21 * Dumping the AST in human-readable form.
22 * Converting an AST back to PHP code.
23 * Experimental: Formatting can be preserved for partially changed ASTs.
24 * Infrastructure to traverse and modify ASTs.
25 * Resolution of namespaced names.
26 * Evaluation of constant expressions.
27 * Builders to simplify AST construction for code generation.
28 * Converting an AST into JSON and back.
33 Install the library using [composer](https://getcomposer.org):
35 php composer.phar require nikic/php-parser
37 Parse some PHP code into an AST and dump the result in human-readable form:
42 use PhpParser\NodeDumper;
43 use PhpParser\ParserFactory;
54 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
56 $ast = $parser->parse($code);
57 } catch (Error $error) {
58 echo "Parse error: {$error->getMessage()}\n";
62 $dumper = new NodeDumper;
63 echo $dumper->dump($ast) . "\n";
66 This dumps an AST looking something like this:
111 Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
115 use PhpParser\Node\Stmt\Function_;
116 use PhpParser\NodeTraverser;
117 use PhpParser\NodeVisitorAbstract;
119 $traverser = new NodeTraverser();
120 $traverser->addVisitor(new class extends NodeVisitorAbstract {
121 public function enterNode(Node $node) {
122 if ($node instanceof Function_) {
123 // Clean out the function body
129 $ast = $traverser->traverse($ast);
130 echo $dumper->dump($ast) . "\n";
133 This gives us an AST where the `Function_::$stmts` are empty:
160 Finally, we can convert the new AST back to PHP code:
163 use PhpParser\PrettyPrinter;
165 $prettyPrinter = new PrettyPrinter\Standard;
166 echo $prettyPrinter->prettyPrintFile($ast);
169 This gives us our original code, minus the `var_dump()` call inside the function:
179 For a more comprehensive introduction, see the documentation.
184 1. [Introduction](doc/0_Introduction.markdown)
185 2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
187 Component documentation:
189 * [Walking the AST](doc/component/Walking_the_AST.markdown)
191 * Modifying the AST from a visitor
192 * Short-circuiting traversals
193 * Interleaved visitors
194 * Simple node finding API
195 * Parent and sibling references
196 * [Name resolution](doc/component/Name_resolution.markdown)
197 * Name resolver options
198 * Name resolution context
199 * [Pretty printing](doc/component/Pretty_printing.markdown)
200 * Converting AST back to PHP code
201 * Customizing formatting
202 * Formatting-preserving code transformations
203 * [AST builders](component/AST_builders.markdown)
204 * Fluent builders for AST nodes
205 * [Lexer](doc/component/Lexer.markdown)
207 * Token and file positions for nodes
209 * [Error handling](doc/component/Error_handling.markdown)
210 * Column information for errors
211 * Error recovery (parsing of syntactically incorrect code)
212 * [Constant expression evaluation](component/Constant_expression_evaluation.markdown)
213 * Evaluating constant/property/etc initializers
214 * Handling errors and unsupported expressions
215 * [JSON representation](component/JSON_representation.markdown)
216 * JSON encoding and decoding of ASTs
217 * [Performance](doc/component/Performance.markdown)
220 * Garbage collection impact
221 * [Frequently asked questions](component/FAQ.markdown)
222 * Parent and sibling references
224 [doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
225 [doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc