ircmaxell/php-cfg 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ircmaxell/php-cfg

最新稳定版本:V0.8.1

Composer 安装命令:

composer require ircmaxell/php-cfg

包简介

A Control Flow Graph implementation for PHP

README 文档

README

Build Status Latest Stable Version

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:

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

Then, just call parse on a block of code, giving it a filename:

$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).

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.

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

To dump the graph, simply use the built-in dumper:

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

CLI

You can leverage the CLI binary to generate debug traces of the CFG for any file, or for printing GraphViz visualizations.

bin/php-cfg dot -o output.dot path/to/file.php

统计信息

  • 总下载量: 84.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 248
  • 点击次数: 1
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 248
  • Watchers: 11
  • Forks: 45
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-10