-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdemo.php
executable file
·60 lines (46 loc) · 1.38 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
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/
use PhpParser\ParserFactory;
require __DIR__ . '/vendor/autoload.php';
$graphviz = false;
list($fileName, $code) = getCode($argc, $argv);
$parser = new PHPCfg\Parser((new ParserFactory())->createForNewestSupportedVersion());
$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]];
}
return [$argv[1], file_get_contents($argv[1])];
}
return [__FILE__, <<<'EOF'
<?php
function foo(array $a) {
$a[] = 1;
}
EOF
];
}