phabel / php-parser
A PHP parser written in PHP
Requires
- php: >=7.0
- ext-tokenizer: *
Requires (Dev)
- ircmaxell/php-yacc: ^0.0.7
- phpunit/phpunit: ^6.5 || ^7.0 || ^8.0 || ^9.0
- 94.13.2
- v94.10.7
- v94.10.6
- v94.10.5
- 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
- dev-master / 4.9.x-dev
- 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-master-80
- dev-master-74
- dev-master-73
- dev-master-72
- dev-master-71
- dev-master-70
- dev-master-56
- dev-tokens-experiment
- dev-nameString
- dev-attributes
This package is auto-updated.
Last update: 2024-11-15 20:31:38 UTC
README
This is a PHP 5.2 to PHP 8.0 parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
Documentation for version 4.x (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.0).
Documentation for version 3.x (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
Features
The main features provided by this library are:
- Parsing PHP 5, 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.
- Experimental: 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)->create(ParserFactory::PREFER_PHP7); 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(
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
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(
parts: array(
0: var_dump
)
)
args: array(
0: Arg(
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(
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
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
- Lexer options
- Token and file positions for nodes
- Custom 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