Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Php Parser | 16,141 | 93,498 | 945 | 6 days ago | 80 | September 04, 2022 | 36 | bsd-3-clause | PHP | |
A PHP parser written in PHP | ||||||||||
Eslint Plugin Vue | 4,083 | 32,952 | 32,844 | a day ago | 136 | September 16, 2022 | 140 | mit | JavaScript | |
Official ESLint plugin for Vue.js | ||||||||||
Ts Morph | 3,771 | 530 | 916 | 23 days ago | 75 | September 03, 2022 | 193 | mit | TypeScript | |
TypeScript Compiler API wrapper for static analysis and programmatic code changes. | ||||||||||
Ast Grep | 2,085 | 15 hours ago | 52 | mit | Rust | |||||
⚡A fast and polyglot tool for code searching, linting, rewriting at large scale. Written in Rust | ||||||||||
Pyt | 2,005 | 11 | 1 | 2 years ago | 15 | November 01, 2018 | 20 | gpl-2.0 | Python | |
A Static Analysis Tool for Detecting Security Vulnerabilities in Python Web Applications | ||||||||||
Spoon | 1,512 | 174 | 37 | 9 hours ago | 197 | June 14, 2022 | 294 | other | Java | |
Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API. | ||||||||||
Laser | 389 | 2 | 3 years ago | 2 | August 19, 2011 | 15 | agpl-3.0 | Ruby | ||
Static analysis and style linter for Ruby code. | ||||||||||
Pyanalyze | 251 | 2 | 5 days ago | 11 | April 13, 2022 | 39 | apache-2.0 | Python | ||
A Python type checker | ||||||||||
Bellybutton | 245 | 1 | 9 months ago | 11 | November 27, 2020 | 9 | mit | Python | ||
Custom Python linting through AST expressions | ||||||||||
Klara | 199 | 2 years ago | 4 | September 19, 2021 | other | Python | ||||
Automatic test case generation for python and static analysis library |
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and manipulation.
Documentation for version 5.x (in development; for running on PHP >= 7.1; for parsing PHP 7.0 to PHP 8.2, with limited support for parsing PHP 5.x).
Documentation for version 4.x (stable; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.2).
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())->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.
Component documentation: