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\Stmt\Function_; |
9
|
|
|
// use PhpParser\Node\Stmt\ClassMethod_; |
10
|
|
|
use PhpParser\NodeTraverser; |
11
|
|
|
// use PhpParser\NodeVisitorAbstract; |
12
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
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 { |
21
|
|
|
private $declarations; |
22
|
|
|
private $base_path; |
23
|
|
|
private $current_path; |
24
|
|
|
private $current_relative_path; |
25
|
|
|
private $parser; |
26
|
|
|
|
27
|
|
|
function __construct( $base_path ) { |
28
|
|
|
$this->parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 ); |
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
|
|
|
public function scan() { |
38
|
|
|
$declarations = new Declarations(); |
39
|
|
|
|
40
|
|
|
$exclude = array( '.git', 'vendor', 'tests', 'docker', 'bin', 'scss', 'images', 'docs', 'languages', 'node_modules' ); |
41
|
|
|
$filter = function ( $file, $key, $iterator ) use ( $exclude ) { |
42
|
|
|
if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
return $file->isFile(); |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
$inner_iterator = new \RecursiveDirectoryIterator( $this->base_path, \RecursiveDirectoryIterator::SKIP_DOTS ); |
49
|
|
|
|
50
|
|
|
$iterator = new \RecursiveIteratorIterator( |
51
|
|
|
new \RecursiveCallbackFilterIterator( $inner_iterator, $filter ) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$display = array( 'php' ); |
55
|
|
|
foreach ( $iterator as $file ) { |
56
|
|
|
if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $display ) ) { |
|
|
|
|
57
|
|
|
$this->file( $file, $declarations ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $declarations; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function file( $file_path, $declarations ) { |
65
|
|
|
$this->current_path = $file_path; |
66
|
|
|
$current_relative_path = str_replace( $this->base_path, '', $file_path ); |
67
|
|
|
|
68
|
|
|
$source = file_get_contents( $file_path ); |
69
|
|
|
try { |
70
|
|
|
$ast = $this->parser->parse( $source ); |
71
|
|
|
} catch ( Error $error ) { |
|
|
|
|
72
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// $dumper = new NodeDumper; |
77
|
|
|
// echo $dumper->dump($ast) . "\n"; |
78
|
|
|
|
79
|
|
|
$traverser = new NodeTraverser(); |
80
|
|
|
$nameResolver = new NameResolver(); |
81
|
|
|
$traverser->addVisitor( $nameResolver ); |
82
|
|
|
|
83
|
|
|
// Resolve names |
84
|
|
|
$ast = $traverser->traverse( $ast ); |
85
|
|
|
|
86
|
|
|
// now scan for public methods etc |
87
|
|
|
$traverser = new NodeTraverser(); |
88
|
|
|
$declaration_visitor = new Declarations\Visitor( $current_relative_path, $declarations ); |
89
|
|
|
$traverser->addVisitor( $declaration_visitor ); |
90
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function check_file_compatibility( $file_path ) { |
94
|
|
|
$source = file_get_contents( $file_path ); |
95
|
|
|
try { |
96
|
|
|
$ast = $this->parser->parse( $source ); |
97
|
|
|
} catch ( Error $error ) { |
|
|
|
|
98
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// $dumper = new NodeDumper; |
103
|
|
|
// echo $dumper->dump($ast) . "\n"; |
104
|
|
|
|
105
|
|
|
$traverser = new NodeTraverser(); |
106
|
|
|
$invocation_finder = new Invocation_Visitor( $this ); |
107
|
|
|
$traverser->addVisitor( $invocation_finder ); |
108
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
class Difference_Params { |
114
|
|
|
public $declaration; |
115
|
|
|
|
116
|
|
|
function __construct( $declaration ) { |
117
|
|
|
$this->declaration = $declaration; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function to_csv() { |
121
|
|
|
return 'params,' . implode( ',', $this->declaration->to_csv_array() ); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
*/ |