Download the PHP package ircmaxell/php-cfg without Composer
On this page you can find all versions of the php package ircmaxell/php-cfg. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ircmaxell/php-cfg
More information about ircmaxell/php-cfg
Files in ircmaxell/php-cfg
Package php-cfg
Short Description A Control Flow Graph implementation for PHP
License MIT
Informations about the package php-cfg
PHP-CFG
Pure PHP implementation of a control flow graph (CFG) with instructions in static single assignment (SSA) form.
The used SSA construction algorithm is based on "Simple and Efficient Construction of Static Single Assignment Form" by Braun et al. This algorithm constructs SSA form directly from the abstract syntax tree, without going through a non-SSA IR first. If you're looking for dominance frontiers, you won't find them here...
The constructed SSA form is minimal and pure (or is supposed to be).
Usage
To bootstrap the parser, you need to give it a PhpParser instance:
Then, just call parse on a block of code, giving it a filename:
While not strictly necessary, you likely should also run the Simplifier Visitor via the Traverser to optimize the CFG (remove redundant jumps and blocks, simplify Phi nodes as much as possible, etc). Other visitors exist to help find function and class declarations (PHPCfg\Visitor\DeclarationFinder), find function and method calls (PHPCfg\Visitor\CallFinder), and find all variables (PHPCfg\Visitor\VariableFinder).
You can also implement your own custom PHPCfg\Visitor and add it to the traverser in order to apply analysis or transforms to the CFG to achieve different results.
To dump the graph, simply use the built-in dumper:
CLI
You can leverage the CLI binary to generate debug traces of the CFG for any file, or for printing GraphViz visualizations.