-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeDependency.php
More file actions
141 lines (123 loc) · 4.1 KB
/
CodeDependency.php
File metadata and controls
141 lines (123 loc) · 4.1 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
<?php
/**
* Find function and class dependencies in PHP source code
*
* This class can determine all classes and functions used by one or more PHP scripts. This is useful
* to determine if scripts can be run in certain environments.
*
* @author Glen Scott <glen@glenscott.co.uk>
*/
class CodeDependency {
const php_file_match = '/^.+\.php|.+\.inc$/i';
/**
* record ALL function calls found
*
* @var array
*/
private $found_functions;
/**
* record ALL function definitions found
*
* @var array
*/
private $defined_custom_funcs;
/**
* record ALL classes instantiated
*
* @var array
*/
private $found_classes;
private $buffer_start;
private $current_func;
private $function_start;
private $new_operator;
private $method_call;
public function __construct() {
$this->found_functions = array();
$this->defined_custom_funcs = array();
$this->found_classes = array();
}
public function findDependenciesByDirectory( $all_functions, $dir ) {
$Regex = new RegexIterator( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ),
self::php_file_match,
RecursiveRegexIterator::GET_MATCH );
foreach ( $Regex as $r ) {
$this->buffer_start = false;
$this->current_func = '';
$this->function_start = false;
$this->new_operator = false;
$this->method_call = false;
foreach ( token_get_all( file_get_contents( $r[0] ) ) as $token ) {
if ( ! is_string( $token ) ) {
$this->processTokenArray( $token );
}
else {
$this->processTokenString( $token );
}
}
}
}
public function getFoundFunctions() {
return $this->found_functions;
}
public function getDefinedCustomFuncs() {
return $this->defined_custom_funcs;
}
public function getFoundClasses() {
return $this->found_classes;
}
private function normalise_function_name( $func ) {
return strtolower( $func );
}
private function processTokenArray( $token ) {
list ( $id, $text ) = $token;
if ( $id == T_STRING ) {
$this->buffer_start = true;
$this->current_func = $text;
if ( $this->function_start ) {
$this->defined_custom_funcs[] = $this->normalise_function_name( $this->current_func );
$this->function_start = false;
}
}
elseif ( $id == T_FUNCTION ) {
$this->function_start = true;
$this->new_operator = false;
}
elseif ( $id == T_NEW ) {
$this->new_operator = true;
}
elseif ( $id == T_WHITESPACE ) {
// ignore whitespace
}
elseif ( $id == T_OBJECT_OPERATOR ) {
$this->method_call = true;
}
else {
$this->buffer_start = false;
$this->new_operator = false;
$this->method_call = false;
}
}
private function processTokenString( $token ) {
if ( $token == '(' ) {
if ( $this->buffer_start ) {
if ( ! $this->new_operator && ! $this->method_call ) {
// got function
if ( ! in_array( $this->normalise_function_name( $this->current_func ), $this->found_functions ) ) {
$this->found_functions[] = $this->normalise_function_name( $this->current_func );
}
}
else {
// got object instantiation
if ( ! in_array( $this->current_func , $this->found_classes ) ) {
$this->found_classes[] = $this->current_func;
}
}
$this->buffer_start = false;
}
}
elseif ( $token == ')' ) {
$this->buffer_start = false;
}
}
}