PHP static analyzer - Work in progress
WORK IN PROGRESS
PHP static analysis.
This analyzer is different because it isn’t meant to find bugs in your code. It is intended as a framework for building your analyzer.
To summarize it in a few sentences:
The goal is to create an open-source equivalent of PhpStorm’s code analyzer. That framework could be used to write very advanced code analyzers, to query a codebase for specific searches (e.g. find all code that calls deprecated classes/methods), to bring PhpStorm’s power into GitHub pull requests or even to improve other IDEs.
How it works:
model a codebase using an Abstract Syntax Tree (aka AST)
The PHP-AST extension is used as a base to parse the PHP code (requires PHP 7).
However this project does not reuse nodes from PHP-AST: it implements new nodes. The reasons are:
apply “visitors” on the AST
Visitors traverse the tree to enrich it with more data and logic. They can, for example, resolve fully qualified name of classes or functions based on the namespace of the file. They can also detect errors in the code. Or again they can try to guess the types of all variables and methods (type inference).
Some default “visitors” are (will be) implemented to cover most use cases. However this package is intended as a framework: you can write custom visitors to enrich even more the information on a codebase (e.g. to add support for framework specific stuff like Laravel facades, Doctrine’s entity manager, etc.).
Serialize the AST
The AST (enriched with more data by visitors, or not) can be entirely serialized to JSON.
That allows to incrementally parse a large codebase and re-parse only files that have changed.
That also allows 3rd party tools to read the AST, e.g. to improve the autocompletion of an editor/IDE, or to build a code browser in HTML/Javascript that understands the code (e.g. Ctrl+Click
in a browser).
Build “real” applications on top of the AST
This project is meant as a base to write more useful applications for end users, as such it’s just a library. If you are interested, open an issue.
No, for now the goal is to have something working. Then it will be made as fast as possible, just don’t expect that to be a priority at first.
composer require mnapoli/php-static-analyzer
The amount of work is huge. If you want to help, hopefully this todo will help you find something to do.
PHP-AST node types to support (list taken from here). Our nodes are in src/Node/
.
[ ] ZEND_AST_USE
[ ] The project should also contain an AST of PHP’s built-in functions and classes. It could be pre-computed and stored serialized to avoir recomputing it every time.
FqnVisitor
DeprecationVisitor
1
, 'abc'
, …)return
statements of functions and methodscatch (... $e)
type-hintingnew
operatoryield
operatorThis project is released under the MIT license.