nikic / php-parser
A PHP parser written in PHP
Installs: 646 325 755
Dependents: 1 570
Suggesters: 28
Security: 0
Stars: 16 995
Watchers: 229
Forks: 1 094
Open Issues: 39
Requires (Dev)
- ircmaxell/php-yacc: ^0.0.7
- phpunit/phpunit: ^9.0
- v5.3.0
- v5.2.0
- v5.1.0
- dev-master / 5.0.x-dev
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0rc1
- v5.0.0beta1
- v5.0.0alpha3
- v5.0.0alpha2
- v5.0.0alpha1
- 4.x-dev
- v4.19.4
- v4.19.3
- v4.19.2
- v4.19.1
- v4.19.0
- v4.18.0
- v4.17.1
- v4.17.0
- v4.16.0
- v4.15.5
- v4.15.4
- v4.15.3
- v4.15.2
- v4.15.1
- v4.15.0
- v4.14.0
- v4.13.2
- v4.13.1
- v4.13.0
- v4.12.0
- v4.11.0
- v4.10.5
- v4.10.4
- v4.10.3
- v4.10.2
- v4.10.1
- v4.10.0
- v4.9.1
- v4.9.0
- v4.8.0
- v4.7.0
- v4.6.0
- v4.5.0
- v4.4.0
- v4.3.0
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.1
- v4.1.0
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v4.0.0beta1
- v4.0.0alpha3
- v4.0.0alpha2
- v4.0.0alpha1
- 3.x-dev
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0beta2
- v3.0.0beta1
- v3.0.0alpha1
- 2.x-dev
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v2.0.0beta1
- v2.0.0alpha1
- 1.x-dev
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0beta2
- v1.0.0beta1
- 0.9.x-dev
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- dev-tokens-experiment
This package is auto-updated.
Last update: 2024-09-29 15:02:41 UTC
README
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
Documentation for version 5.x (current; for running on PHP >= 7.4; for parsing PHP 7.0 to PHP 8.4, with limited support for parsing PHP 5.x).
Documentation for version 4.x (supported; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.3).
Features
The main features provided by this library are:
- Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
- Invalid code can be parsed into a partial AST.
- The AST contains accurate location information.
- Dumping the AST in human-readable form.
- Converting an AST back to PHP code.
- Formatting can be preserved for partially changed ASTs.
- Infrastructure to traverse and modify ASTs.
- Resolution of namespaced names.
- Evaluation of constant expressions.
- Builders to simplify AST construction for code generation.
- Converting an AST into JSON and back.
Quick Start
Install the library using composer:
php composer.phar require nikic/php-parser
Parse some PHP code into an AST and dump the result in human-readable form:
<?php use PhpParser\Error; use PhpParser\NodeDumper; use PhpParser\ParserFactory; $code = <<<'CODE' <?php function test($foo) { var_dump($foo); } CODE; $parser = (new ParserFactory())->createForNewestSupportedVersion(); try { $ast = $parser->parse($code); } catch (Error $error) { echo "Parse error: {$error->getMessage()}\n"; return; } $dumper = new NodeDumper; echo $dumper->dump($ast) . "\n";
This dumps an AST looking something like this:
array(
0: Stmt_Function(
attrGroups: array(
)
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
attrGroups: array(
)
flags: 0
type: null
byRef: false
variadic: false
var: Expr_Variable(
name: foo
)
default: null
)
)
returnType: null
stmts: array(
0: Stmt_Expression(
expr: Expr_FuncCall(
name: Name(
name: var_dump
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: foo
)
byRef: false
unpack: false
)
)
)
)
)
)
)
Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
use PhpParser\Node; use PhpParser\Node\Stmt\Function_; use PhpParser\NodeTraverser; use PhpParser\NodeVisitorAbstract; $traverser = new NodeTraverser(); $traverser->addVisitor(new class extends NodeVisitorAbstract { public function enterNode(Node $node) { if ($node instanceof Function_) { // Clean out the function body $node->stmts = []; } } }); $ast = $traverser->traverse($ast); echo $dumper->dump($ast) . "\n";
This gives us an AST where the Function_::$stmts
are empty:
array(
0: Stmt_Function(
attrGroups: array(
)
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
attrGroups: array(
)
type: null
byRef: false
variadic: false
var: Expr_Variable(
name: foo
)
default: null
)
)
returnType: null
stmts: array(
)
)
)
Finally, we can convert the new AST back to PHP code:
use PhpParser\PrettyPrinter; $prettyPrinter = new PrettyPrinter\Standard; echo $prettyPrinter->prettyPrintFile($ast);
This gives us our original code, minus the var_dump()
call inside the function:
<?php function test($foo) { }
For a more comprehensive introduction, see the documentation.
Documentation
Component documentation:
- Walking the AST
- Node visitors
- Modifying the AST from a visitor
- Short-circuiting traversals
- Interleaved visitors
- Simple node finding API
- Parent and sibling references
- Name resolution
- Name resolver options
- Name resolution context
- Pretty printing
- Converting AST back to PHP code
- Customizing formatting
- Formatting-preserving code transformations
- AST builders
- Fluent builders for AST nodes
- Lexer
- Emulation
- Tokens, positions and attributes
- Error handling
- Column information for errors
- Error recovery (parsing of syntactically incorrect code)
- Constant expression evaluation
- Evaluating constant/property/etc initializers
- Handling errors and unsupported expressions
- JSON representation
- JSON encoding and decoding of ASTs
- Performance
- Disabling Xdebug
- Reusing objects
- Garbage collection impact
- Frequently asked questions
- Parent and sibling references