-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.php
executable file
·53 lines (35 loc) · 1.12 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
/**
* This file is part of PHP-Optimizer, a PHP CFG Optimizer for PHP code
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/
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();
$script = $parser->parse($code, __FILE__);
$traverser->traverse($script);
$state = new PHPTypes\State($script);
$typeReconstructor->resolve($state);
echo $dumper->printScript($script);
echo "\nOptimizing\n";
$optimizer = new PHPOptimizer\Optimizer();
$optimizer->optimize($script);
echo $dumper->printScript($script);