2017 © Pedro Peláez
 

library php-cfg

A Control Flow Graph implementation for PHP

image

ircmaxell/php-cfg

A Control Flow Graph implementation for PHP

  • Friday, July 6, 2018
  • by ircmaxell
  • Repository
  • 11 Watchers
  • 108 Stars
  • 1,537 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 21 Forks
  • 11 Open issues
  • 1 Versions
  • 13 % Grown

The README.md

Build Status Latest Stable Version, (*1)

PHP-CFG

Pure PHP implementation of a control flow graph (CFG) with instructions in static single assignment (SSA) form., (*2)

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..., (*3)

The constructed SSA form is minimal and pure (or is supposed to be)., (*4)

Usage

To bootstrap the parser, you need to give it a PhpParser instance:, (*5)

$parser = new PHPCfg\Parser(
    (new PhpParser\ParserFactory)->createForNewestSupportedVersion()
);

Then, just call parse on a block of code, giving it a filename:, (*6)

$script = $parser->parse(file_get_contents(__FILE__), __FILE__);

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)., (*7)

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., (*8)

$traverser = new PHPCfg\Traverser();
$traverser->addVisitor(new PHPCfg\Visitor\Simplifier());
$traverser->traverse($script);

To dump the graph, simply use the built-in dumper:, (*9)

$dumper = new PHPCfg\Printer\Text();
echo $dumper->printScript($script);

The Versions

06/07 2018

dev-master

9999999-dev

A Control Flow Graph implementation for PHP

  Sources   Download

BSD

The Requires

 

The Development Requires