|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Analyzer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Error; |
|
6
|
|
|
use PhpParser\NodeDumper; |
|
7
|
|
|
use PhpParser\ParserFactory; |
|
8
|
|
|
use PhpParser\Node; |
|
9
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
10
|
|
|
use PhpParser\Node\Stmt\ClassMethod_; |
|
11
|
|
|
use PhpParser\NodeTraverser; |
|
12
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
13
|
|
|
|
|
14
|
|
|
// const STATE_NONE = 0; |
|
15
|
|
|
// const STATE_CLASS_DECLARATION = 1; |
|
16
|
|
|
|
|
17
|
|
|
const VIS_PUBLIC = 0; |
|
18
|
|
|
const VIS_PRIVATE = 1; |
|
19
|
|
|
|
|
20
|
|
|
class Analyzer extends NodeVisitorAbstract { |
|
21
|
|
|
private $declarations; |
|
22
|
|
|
private $base_path; |
|
23
|
|
|
private $current_path; |
|
24
|
|
|
private $parser; |
|
25
|
|
|
|
|
26
|
|
|
function __construct( $base_path ) { |
|
27
|
|
|
$this->parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 ); |
|
28
|
|
|
$this->declarations = array(); |
|
29
|
|
|
$this->base_path = $this->slashit( $base_path ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function slashit( $path ) { |
|
33
|
|
|
$path .= ( substr( $path, -1 ) == '/' ? '' : '/' ); |
|
34
|
|
|
return $path; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function add_declaration( $declaration ) { |
|
38
|
|
|
$this->declarations[] = $declaration; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function print_declarations() { |
|
42
|
|
|
print_r( $this->declarations ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function scan() { |
|
46
|
|
|
$exclude = array( '.git', 'vendor', 'tests', 'docker', 'bin', 'scss', 'images', 'docs', 'languages', 'node_modules' ); |
|
47
|
|
|
$filter = function ( $file, $key, $iterator ) use ( $exclude ) { |
|
48
|
|
|
if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) { |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
return $file->isFile(); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
$inner_iterator = new \RecursiveDirectoryIterator( $this->base_path, \RecursiveDirectoryIterator::SKIP_DOTS ); |
|
55
|
|
|
|
|
56
|
|
|
$iterator = new \RecursiveIteratorIterator( |
|
57
|
|
|
new \RecursiveCallbackFilterIterator( $inner_iterator, $filter ) |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$display = array( 'php' ); |
|
61
|
|
|
foreach ( $iterator as $file ) { |
|
62
|
|
|
if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $display ) ) { |
|
|
|
|
|
|
63
|
|
|
echo "$file\n"; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function file( $file_path ) { |
|
69
|
|
|
$this->current_path = $file_path; |
|
70
|
|
|
$source = file_get_contents( $file_path ); |
|
71
|
|
|
try { |
|
72
|
|
|
$ast = $this->parser->parse( $source ); |
|
73
|
|
|
} catch ( Error $error ) { |
|
|
|
|
|
|
74
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// $dumper = new NodeDumper; |
|
79
|
|
|
// echo $dumper->dump($ast) . "\n"; |
|
80
|
|
|
|
|
81
|
|
|
$traverser = new NodeTraverser(); |
|
82
|
|
|
$traverser->addVisitor( $this ); |
|
83
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
|
|
84
|
|
|
$this->print_declarations(); |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function enterNode( Node $node ) { |
|
89
|
|
|
// print_r($node); |
|
90
|
|
|
if ( $node instanceof Node\Stmt\Class_ ) { |
|
|
|
|
|
|
91
|
|
|
$this->add_declaration( new Class_Declaration( $this->current_path, $node->getLine(), $node->name->name ) ); |
|
92
|
|
|
} |
|
93
|
|
|
if ( $node instanceof Node\Stmt\Property && $node->isPublic() ) { |
|
|
|
|
|
|
94
|
|
|
$this->add_declaration( new Class_Property_Declaration( $this->current_path, $node->getLine(), $node->props[0]->name->name ) ); |
|
95
|
|
|
} |
|
96
|
|
|
if ( $node instanceof Node\Stmt\ClassMethod && $node->isPublic() ) { |
|
|
|
|
|
|
97
|
|
|
$method = new Class_Method_Declaration( $this->current_path, $node->getLine(), $node->name->name, $node->isStatic() ); |
|
98
|
|
|
foreach ( $node->getParams() as $param ) { |
|
99
|
|
|
$method->add_param( $node->var->name, $node->default, $node->type, $node->byRef, $node->variadic ); |
|
100
|
|
|
} |
|
101
|
|
|
$this->add_declaration( $method ); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
class Declaration { |
|
107
|
|
|
public $path; |
|
108
|
|
|
public $line; |
|
109
|
|
|
public $name; |
|
110
|
|
|
|
|
111
|
|
|
function __construct( $path, $line, $name ) { |
|
112
|
|
|
$this->path = $path; |
|
113
|
|
|
$this->line = $line; |
|
114
|
|
|
$this->name = $name; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
class Class_Declaration extends Declaration { |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* We only log public class methods, whether they are static, and their parameters |
|
123
|
|
|
*/ |
|
124
|
|
|
class Class_Method_Declaration extends Declaration { |
|
125
|
|
|
public $static; |
|
126
|
|
|
|
|
127
|
|
|
function __construct( $path, $line, $name, $static ) { |
|
128
|
|
|
$this->static = $static; |
|
129
|
|
|
$this->params = array(); |
|
|
|
|
|
|
130
|
|
|
parent::__construct( $path, $line, $name ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
function add_param( $name, $default, $type, $byRef, $variadic ) { |
|
134
|
|
|
$this->params[] = (object) compact( 'name', 'default', 'type', 'byRef', 'variadic' ); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* We only log public class variables |
|
140
|
|
|
*/ |
|
141
|
|
|
class Class_Property_Declaration extends Declaration { |
|
142
|
|
|
} |
|
143
|
|
|
|