1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Analyzer; |
4
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
6
|
|
|
use PhpParser\NodeTraverser; |
7
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
8
|
|
|
|
9
|
|
|
class Declarations extends PersistentList { |
10
|
|
|
|
11
|
|
|
private $parser; |
12
|
|
|
|
13
|
|
|
function __construct() { |
14
|
|
|
$this->parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 ); |
15
|
|
|
parent::__construct(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
private function slashit( $path ) { |
19
|
|
|
$path .= ( substr( $path, -1 ) == '/' ? '' : '/' ); |
20
|
|
|
return $path; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Scan every PHP in the root |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function scan( $root, $exclude = array() ) { |
27
|
|
|
if ( is_dir( $root ) ) { |
28
|
|
|
return $this->scan_dir( $this->slashit( $root ), $exclude ); |
29
|
|
|
} elseif( is_file( $root ) ) { |
30
|
|
|
return $this->scan_file( $this->slashit( dirname( $root ) ), $root ); |
31
|
|
|
} else { |
32
|
|
|
throw new \Exception( 'input_error', "Expected $root to be a file or directory" ); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function scan_dir( $root, $exclude = array() ) { |
37
|
|
|
|
38
|
|
|
if ( is_null( $exclude ) || ! is_array( $exclude ) ) { |
39
|
|
|
throw new Exception( "Exclude must be an array" ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$filter = function ( $file, $key, $iterator ) use ( $exclude ) { |
43
|
|
|
if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) { |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
return $file->isFile(); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
$inner_iterator = new \RecursiveDirectoryIterator( $root, \RecursiveDirectoryIterator::SKIP_DOTS ); |
50
|
|
|
|
51
|
|
|
$iterator = new \RecursiveIteratorIterator( |
52
|
|
|
new \RecursiveCallbackFilterIterator( $inner_iterator, $filter ) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$valid_extensions = array( 'php' ); |
56
|
|
|
foreach ( $iterator as $file ) { |
57
|
|
|
if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $valid_extensions ) ) { |
|
|
|
|
58
|
|
|
$this->scan_file( $root, $file ); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function scan_file( $root, $file_path ) { |
64
|
|
|
$file_path_relative = str_replace( $root, '', $file_path ); |
65
|
|
|
|
66
|
|
|
$source = file_get_contents( $file_path ); |
67
|
|
|
try { |
68
|
|
|
$ast = $this->parser->parse( $source ); |
69
|
|
|
} catch ( Error $error ) { |
|
|
|
|
70
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// $dumper = new NodeDumper; |
75
|
|
|
// echo $dumper->dump($ast) . "\n"; |
76
|
|
|
|
77
|
|
|
$traverser = new NodeTraverser(); |
78
|
|
|
$nameResolver = new NameResolver(); |
79
|
|
|
$traverser->addVisitor( $nameResolver ); |
80
|
|
|
|
81
|
|
|
// Resolve names |
82
|
|
|
$ast = $traverser->traverse( $ast ); |
83
|
|
|
|
84
|
|
|
// now scan for public methods etc |
85
|
|
|
$traverser = new NodeTraverser(); |
86
|
|
|
$declaration_visitor = new Declarations\Visitor( $file_path_relative, $this ); |
87
|
|
|
$traverser->addVisitor( $declaration_visitor ); |
88
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function load( $file_path ) { |
92
|
|
|
$row = 1; |
93
|
|
|
if ( ( $handle = fopen( $file_path , "r" ) ) !== FALSE ) { |
94
|
|
|
while ( ( $data = fgetcsv( $handle, 1000, "," ) ) !== FALSE ) { |
95
|
|
|
$num = count( $data ); |
|
|
|
|
96
|
|
|
list( $type, $file, $line, $class_name, $name, $static, $params_json ) = $data; |
97
|
|
|
|
98
|
|
|
switch( $type ) { |
99
|
|
|
case 'class': |
100
|
|
|
$this->add( new Declarations\Class_( $file, $line, $class_name ) ); |
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
case 'property': |
104
|
|
|
$this->add( new Declarations\Class_Property( $file, $line, $class_name, $name, $static ) ); |
105
|
|
|
break; |
106
|
|
|
|
107
|
|
View Code Duplication |
case 'method': |
108
|
|
|
$params = json_decode( $params_json ); |
109
|
|
|
$declaration = new Declarations\Class_Method( $file, $line, $class_name, $name, $static ); |
110
|
|
|
if ( is_array( $params ) ) { |
111
|
|
|
foreach( $params as $param ) { |
112
|
|
|
$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic ); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->add( $declaration ); |
117
|
|
|
|
118
|
|
|
break; |
119
|
|
|
|
120
|
|
View Code Duplication |
case 'function': |
121
|
|
|
$params = json_decode( $params_json ); |
122
|
|
|
$declaration = new Declarations\Function_( $file, $line, $name ); |
123
|
|
|
if ( is_array( $params ) ) { |
124
|
|
|
foreach( $params as $param ) { |
125
|
|
|
$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic ); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->add( $declaration ); |
130
|
|
|
|
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
$row++; |
134
|
|
|
} |
135
|
|
|
fclose( $handle ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |