-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTParser.php
68 lines (61 loc) · 1.79 KB
/
ASTParser.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
61
62
63
64
65
66
67
<?php
use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
class ASTParser
{
private $path;
private $files;
private $codes;
private $asts;
private $parser;
function __construct($path)
{
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$this->path = $path;
$this->files = [];
$this->codes = [];
$this->asts = [];
$this->scanFiles($this->path);
foreach ($this->files as $file)
$this->getContent($file);
$this->parse();
}
private function scanFiles($path)
{
$files = scandir($path);
foreach ($files as $fn)
{
if ($fn == ".." || $fn == ".")
continue;
else if (is_dir($path.'/'.$fn)) {
$this->scanFiles($path.'/'.$fn);
}
else if (preg_match("/.*\.php$/i", $fn))
array_push($this->files, $path.'/'.$fn);
}
}
private function getContent($file)
{
$content = file_get_contents($file);
array_push($this->codes, ["filename" => $file, "content" => $content]);
}
private function parse()
{
foreach ($this->codes as $code)
{
echo "Parsing ".$code["filename"]." to AST\n";
$content = $code["content"];
$nodes = $this->parser->parse($content);
$php_lines = explode("\n", $content);
array_push($this->asts, ["filename" => $code["filename"], "nodes" => $nodes, "php_lines" => $php_lines]);
//$this->asts[$code["filename"]] = ["filename" => $code["filename"], "nodes" => $nodes];
}
}
public function getASTs()
{
return $this->asts;
}
}