Exemplo n.º 1
0
<?php

require "vendor/autoload.php";
use PhpParser\ParserFactory;
$parser = new PHPCfg\Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7));
$declarations = new PHPCfg\Visitor\DeclarationFinder();
$calls = new PHPCfg\Visitor\CallFinder();
$variables = new PHPCfg\Visitor\VariableFinder();
$traverser = new PHPCfg\Traverser();
$traverser->addVisitor($declarations);
$traverser->addVisitor($calls);
$traverser->addVisitor(new PHPCfg\Visitor\Simplifier());
$traverser->addVisitor($variables);
$code = <<<'EOF'
<?php
function foo(array $a) {
	$a[] = 1;
}
EOF;
$block = $parser->parse($code, __FILE__);
$traverser->traverse($block);
$dumper = new PHPCfg\Printer\Text();
echo $dumper->printCFG(array($block));
Exemplo n.º 2
0
$parser = new PHPCfg\Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7));
$declarations = new PHPCfg\Visitor\DeclarationFinder();
$calls = new PHPCfg\Visitor\CallFinder();
$variables = new PHPCfg\Visitor\VariableFinder();
$traverser = new PHPCfg\Traverser();
$traverser->addVisitor($declarations);
$traverser->addVisitor($calls);
$traverser->addVisitor(new PHPCfg\Visitor\Simplifier());
$traverser->addVisitor($variables);
$script = $parser->parse($code, __FILE__);
$traverser->traverse($script);
if ($graphviz) {
    $dumper = new PHPCfg\Printer\GraphViz();
    echo $dumper->printScript($script);
} else {
    $dumper = new PHPCfg\Printer\Text();
    echo $dumper->printScript($script);
}
function getCode($argc, $argv)
{
    if ($argc >= 2) {
        if (strpos($argv[1], '<?php') === 0) {
            return ['command line code', $argv[1]];
        } else {
            return [$argv[1], file_get_contents($argv[1])];
        }
    } else {
        return [__FILE__, <<<'EOF'
<?php
function foo(array $a) {
    $a[] = 1;
Exemplo n.º 3
0
use PhpParser\ParserFactory;
set_time_limit(2);
$code = <<<'EOF'
<?php

$a = 1;

for ($i = 0; false; $i++) {
	$a = $a + 1;
}

return $a;

EOF;
require 'vendor/autoload.php';
$astTraverser = new PhpParser\NodeTraverser();
$astTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver());
$parser = new PHPCfg\Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $astTraverser);
$traverser = new PHPCfg\Traverser();
$traverser->addVisitor(new PHPCfg\Visitor\Simplifier());
$typeReconstructor = new PHPTypes\TypeReconstructor();
$dumper = new PHPCfg\Printer\Text();
$block = $parser->parse($code, __FILE__);
$traverser->traverse($block);
$state = new PHPTypes\State([$block]);
$typeReconstructor->resolve($state);
echo $dumper->printCFG($state->blocks);
echo "\nOptimizing\n";
$optimizer = new PHPOptimizer\Optimizer();
$blocks = $optimizer->optimize($state->blocks);
echo $dumper->printCFG($blocks);