|
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 Analyzer { |
|
10
|
|
|
private $declarations; |
|
11
|
|
|
private $base_path; |
|
12
|
|
|
private $current_path; |
|
13
|
|
|
private $current_relative_path; |
|
14
|
|
|
private $parser; |
|
15
|
|
|
|
|
16
|
|
|
function __construct( $base_path ) { |
|
17
|
|
|
$this->parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 ); |
|
18
|
|
|
$this->base_path = $this->slashit( $base_path ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private function slashit( $path ) { |
|
22
|
|
|
$path .= ( substr( $path, -1 ) == '/' ? '' : '/' ); |
|
23
|
|
|
return $path; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function scan() { |
|
27
|
|
|
$declarations = new Declarations(); |
|
28
|
|
|
|
|
29
|
|
|
$exclude = array( '.git', 'vendor', 'tests', 'docker', 'bin', 'scss', 'images', 'docs', 'languages', 'node_modules' ); |
|
30
|
|
|
$filter = function ( $file, $key, $iterator ) use ( $exclude ) { |
|
31
|
|
|
if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) { |
|
32
|
|
|
return true; |
|
33
|
|
|
} |
|
34
|
|
|
return $file->isFile(); |
|
35
|
|
|
}; |
|
36
|
|
|
|
|
37
|
|
|
$inner_iterator = new \RecursiveDirectoryIterator( $this->base_path, \RecursiveDirectoryIterator::SKIP_DOTS ); |
|
38
|
|
|
|
|
39
|
|
|
$iterator = new \RecursiveIteratorIterator( |
|
40
|
|
|
new \RecursiveCallbackFilterIterator( $inner_iterator, $filter ) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$display = array( 'php' ); |
|
44
|
|
|
foreach ( $iterator as $file ) { |
|
45
|
|
|
if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $display ) ) { |
|
|
|
|
|
|
46
|
|
|
$this->file( $file, $declarations ); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $declarations; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function file( $file_path, $declarations ) { |
|
54
|
|
|
$this->current_path = $file_path; |
|
55
|
|
|
$current_relative_path = str_replace( $this->base_path, '', $file_path ); |
|
56
|
|
|
|
|
57
|
|
|
$source = file_get_contents( $file_path ); |
|
58
|
|
|
try { |
|
59
|
|
|
$ast = $this->parser->parse( $source ); |
|
60
|
|
|
} catch ( Error $error ) { |
|
|
|
|
|
|
61
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// $dumper = new NodeDumper; |
|
66
|
|
|
// echo $dumper->dump($ast) . "\n"; |
|
67
|
|
|
|
|
68
|
|
|
$traverser = new NodeTraverser(); |
|
69
|
|
|
$nameResolver = new NameResolver(); |
|
70
|
|
|
$traverser->addVisitor( $nameResolver ); |
|
71
|
|
|
|
|
72
|
|
|
// Resolve names |
|
73
|
|
|
$ast = $traverser->traverse( $ast ); |
|
74
|
|
|
|
|
75
|
|
|
// now scan for public methods etc |
|
76
|
|
|
$traverser = new NodeTraverser(); |
|
77
|
|
|
$declaration_visitor = new Declarations\Visitor( $current_relative_path, $declarations ); |
|
78
|
|
|
$traverser->addVisitor( $declaration_visitor ); |
|
79
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/* |
|
84
|
|
|
class Difference_Params { |
|
85
|
|
|
public $declaration; |
|
86
|
|
|
|
|
87
|
|
|
function __construct( $declaration ) { |
|
88
|
|
|
$this->declaration = $declaration; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function to_csv() { |
|
92
|
|
|
return 'params,' . implode( ',', $this->declaration->to_csv_array() ); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
*/ |
|
96
|
|
|
|