| Number of watchers on Github | 5340 |
| Number of open issues | 24 |
| Average time to close an issue | 21 days |
| Main language | PHP |
| Average time to merge a PR | about 19 hours |
| Open pull requests | 10+ |
| Closed pull requests | 70+ |
| Last commit | over 1 year ago |
| Repo Created | over 8 years ago |
| Repo Last Updated | over 1 year ago |
| Size | 4.38 MB |
| Organization / Author | nikic |
| Latest Release | v4.0.0 |
| Contributors | 44 |
| Page Updated | 2018-03-13 |
| Do you use PHP-Parser? Leave a review! | |
| View open issues (24) | |
| View PHP-Parser activity | |
| View on github | |
| Fresh, new opensource launches 🚀🚀🚀 | |
|
Trendy new open source projects in your inbox!
View examples
|
|
This is a PHP 5.2 to PHP 7.2 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.2).
Documentation for version 3.x (stable; 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:
This is a new major version of the PHP-Parser library. The biggest new feature in this release is an experimental pretty-printing mode, which preserves formatting for parts of the code which have not been modified. As such, it should now be much easier to use PHP-Parser for automated code refactorings. See the pretty printer documentation for information on how to use this functionality
The following changelog lists all significant changes relative to PHP-Parser 3.1. For more information on backwards incompatible changes, please see the upgrading guide.
replaceNodes option to NameResolver, defaulting to true. If this option is disabled, resolved names will be added as resolvedName attributes, instead of replacing the original names.NodeFinder class, which can be used to find nodes based on a callback or class name. This is a utility to avoid custom node visitor implementations for simple search operations.ClassMethod::isMagic() method.BuilderFactory methods: val() method for creating an AST for a simple value, concat() for creating concatenation trees, args() for preparing function arguments.NameContext class, which encapsulates the NameResolver logic independently of the actual AST traversal. This facilitates use in other context, such as class names in doc comments. Additionally it provides an API for getting the shortest representation of a name.Node::setAttributes() method.JsonDecoder. This allows conversion JSON back into an AST.Name methods toLowerString() and isSpecialClassName().Identifier and VarLikeIdentifier nodes, which are used in place of simple strings in many places.getComments(), getStartLine(), getEndLine(), getStartTokenPos(), getEndTokenPos(), getStartFilePos() and getEndFilePos() methods to Node. These provide a more obvious access point for the already existing attributes of the same name.ConstExprEvaluator to evaluate constant expressions to PHP values.Expr\BinaryOp::getOperatorSigil(), returning + for Expr\BinaryOp\Plus, etc.Identifier (or VarLikeIdentifier) nodes. Please see the UPGRADE-4.0 file for an exhaustive list of affected nodes and some notes on possible impact.expr;) are now represented using a Stmt\Expression node. Previously these statements were directly represented as their constituent expression.name subnode of Param has been renamed to var and now contains a Variable rather than a plain string.name subnode of StaticVar has been renamed to var and now contains a Variable rather than a plain string.var subnode of ClosureUse now contains a Variable rather than a plain string.var subnode of Catch now contains a Variable rather than a plain string.alias subnode of UseUse is now null if no explicit alias is given. As such, use Foo\Bar and use Foo\Bar as Bar are now represented differently. The getAlias() method can be used to get the effective alias, even if it is not explicitly given.type subnode on Class, ClassMethod and Property nodes. Use flags instead.ClassConst::isStatic() method has been removed. Constants cannot have a static modifier.NodeTraverser no longer accepts false as a return value from a leaveNode() method. NodeTraverser::REMOVE_NODE should be returned instead.Node::setLine() method has been removed. If you really need to, you can use setAttribute() instead.Class_::VISIBILITY_MODIFER_MASK constant has been dropped in favor of Class_::VISIBILITY_MODIFIER_MASK.Serializer\XML, and Unserializer\XML, as well as the interfaces Serializer and Unserializer no longer exist.BuilderAbstract class has been removed. It's functionality is moved into BuilderHelpers. However, this is an internal class and should not be used directly.Autoloader class has been removed. It is now required to use the Composer autoloader.This is the first and likely last beta release for a new major version of the PHP-Parser library. The biggest new feature in this version is an experimental pretty-printing mode, which preserves formatting for parts of the code which have not been modified.
For more information on BC breaks in this release see the upgrading guide.
The following changelog only lists the changes from the previous alpha release. A complete changelog is also available.
Autoloader class has been removed. It is now required to use the Composer autoloader.