-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.php
More file actions
146 lines (139 loc) · 4.36 KB
/
parser.php
File metadata and controls
146 lines (139 loc) · 4.36 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require_once __DIR__."/php-emul/PHP-Parser/lib/bootstrap.php";
use PhpParser\Node;
ini_set("memory_limit","-1");
function getAllPhpFiles($Path)
{
$Directory = new RecursiveDirectoryIterator($Path);
$Iterator = new RecursiveIteratorIterator($Directory);
$list=[];
foreach ($Iterator as $it)
if (pathinfo($it->getPathName(), PATHINFO_EXTENSION)=="php")
$list[]=$it->getPathName();
return $list;
}
class StatementCounter extends PHPParser_NodeVisitorAbstract
{
public $statements=0;
public $expressions=0;
public $includes=0;
public $ifs=0,$switches=0;
public function leaveNode(Node $node) {
if ($node instanceof Node\Stmt) # plain strings
// if ($node instanceof PHPParser_Node_Scalar_String) # plain strings
$this->statements++;
if ($node instanceof Node\Expr) # plain strings
$this->expressions++;
if ($node instanceof Node\Expr\Include_) # plain strings
$this->includes++;
if ($node instanceof Node\Stmt\If_) # plain strings
$this->ifs++;
if ($node instanceof Node\Stmt\Switch_) # plain strings
$this->switches++;
// elseif ($node instanceof PHPParser_Node_Scalar_Encapsed) # php encapsulated strings
// {
// foreach ($node->parts as $part)
// if (is_string($part)) self::add($part);
// }
}
public $stats=[];
function inc($name,$number=1)
{
$parts=explode("/",$name);
$ref=&$this->stats;
$last_part=array_pop($parts);
foreach ($parts as $part)
{
if (!isset($ref[$part]))
$ref[$part]=[];
$ref=&$ref[$part];
}
if (!isset($ref[$last_part]))
$ref[$last_part]=$number;
else
$ref[$last_part]+=$number;
}
function node_traverser($node)
{
if (is_array($node))
{
foreach ($node as $n)
$this->node_traverser($n);
return;
}
if ($node instanceof Node)
{
foreach ($node->getSubNodeNames() as $name)
$this->node_traverser($node->$name);
}
if ($node instanceof Node\Stmt)
{
$this->inc("statements/parsed");
}
if ($node instanceof Node\Expr)
{
if ($node instanceof Node\Expr\Exit_)
$this->inc("node/exit");
elseif ($node instanceof Node\Expr\Include_)
$this->inc("node/include");
}
if ($node instanceof Node\Stmt\If_)
{
$this->inc("node/if");
$this->inc("node/branches");
if (isset($node->elseifs))
$this->inc("node/branches",count($node->elseifs));
if (isset($node->else))
$this->inc("node/branches");
}
if ($node instanceof Node\Stmt\Switch_)
$this->inc("node/switch");
// if (isset($node->stmts))
// $this->node_traverser($node->stmts);
// if (isset($node->expr))
// $this->node_traverser($node->expr);
}
}
echo "Parsing, this might take up to several minutes...\n";
flush();
$targetdir=$argv[1];
if (is_file($targetdir))
$files=[$targetdir];
else
$files=(getAllPhpFiles($targetdir));
$parser = new PHPParser\Parser(new PHPParser\Lexer);
$traverser = new PHPParser\NodeTraverser;
// $prettyPrinter = new PHPParser_PrettyPrinter_Default;
$StatementCounter=new StatementCounter;
$traverser->addVisitor($StatementCounter);
$n=0;
$LOC=0;
foreach ($files as $file)
{
$n++;
// echo $file,PHP_EOL;
if (($n)%80==0)
echo PHP_EOL;
else
echo ".";
try {
$syntax_tree = $parser->parse(file_get_contents($file));
$StatementCounter->node_traverser($syntax_tree);
}
catch (PHPParser_Error $e)
{
echo PHP_EOL."ERROR: Unable to parse {$file}: ".$e->getMessage().PHP_EOL;
continue;
}
$traverser->traverse($syntax_tree);
$LOC+=count(file($file));
}
print_r($StatementCounter->stats);
echo PHP_EOL,
"Files: ",count($files),PHP_EOL,
"Statements: ",$StatementCounter->statements,PHP_EOL,
"Expressions: ",$StatementCounter->expressions,PHP_EOL,
"Ifs: ",$StatementCounter->ifs,PHP_EOL,
"Switches: ",$StatementCounter->switches,PHP_EOL,
"Includes: ",$StatementCounter->includes,PHP_EOL,
"LOC: ",$LOC,PHP_EOL;