Gleb 66078cb367 test | 5 gadi atpakaļ | |
---|---|---|
.. | ||
bin | 5 gadi atpakaļ | |
doc | 5 gadi atpakaļ | |
grammar | 5 gadi atpakaļ | |
lib | 5 gadi atpakaļ | |
test | 5 gadi atpakaļ | |
test_old | 5 gadi atpakaļ | |
.gitignore | 5 gadi atpakaļ | |
.travis.yml | 5 gadi atpakaļ | |
CHANGELOG.md | 5 gadi atpakaļ | |
LICENSE | 5 gadi atpakaļ | |
README.md | 5 gadi atpakaļ | |
UPGRADE-1.0.md | 5 gadi atpakaļ | |
UPGRADE-2.0.md | 5 gadi atpakaļ | |
UPGRADE-3.0.md | 5 gadi atpakaļ | |
UPGRADE-4.0.md | 5 gadi atpakaļ | |
composer.json | 5 gadi atpakaļ | |
phpunit.xml.dist | 5 gadi atpakaļ |
This is a PHP 5.2 to PHP 7.3 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 7.3).
Documentation for version 3.x (unsupported; for running on PHP >= 5.5; for parsing PHP 5.2 to PHP 7.2).
The main features provided by this library are:
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.
Component documentation: