/
pill_lib.php
309 lines (247 loc) · 9.13 KB
/
pill_lib.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/*
a PHP script that computes cyclomatic complexity of PHP source code
Copyright (C) 2009 Charles Rowe
Version 0.1a
This application is inspired by Lint, developed by Pasquale Ceres
http://ars.altervista.org/lint_php/lint_php.php
and also Saikuro developed by Zev Blut
http://saikuro.rubyforge.org/
pill_lib.php is part of the Pill program
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$DEFAULT_WARN_LEVEL = 10;
$DEFAULT_ERROR_LEVEL = 15;
$usage =<<<USAGESTRING
usage: ./$argv[0] [file or directory] -[rweoas]
r : recursive -- If a directory is specified, it will recursively check other directories for php files.
w : warn level -- Warning which marks a function with a warning to trigger its output. If no integer is specified,
the default level will be set to $DEFAULT_WARN_LEVEL
e : error level -- Error level which marks a function with an error to trigger its output. Ig no integer is specified,
the default level will be set to $DEFAULT_ERROR_LEVEL
a : show all -- show all complexity scores regardless of warn or error levels
asp : process the code for ASP style tagging '<% echo ' hi!'; %>'
cc : DEFAULT -- Booleans are not counted, case statements are counted
cc2 : STRICT -- booleans and case statements are counted
cc3 : MODIFIED -- booleans are counted, switch statements are counted, case statements ARE NOT counted
By default, if no options are specified, the flags -e,-s, and -cc will automatically be applied.
USAGESTRING;
function pill($text,$complexity_type){
$results = Array('Global Code' => 1);
$tokens = token_get_all($text);
$in_function = "";
$function_key = 'Global Code';
$brace_count = 0;
$total_function_count = 0;
for($i=0; $i< count($tokens); ) {
$token = $tokens[$i];
if (is_string($token)) {
$token = trim($token);
}else{
list ($token, $value) = $token;
}
if($token == T_FUNCTION){
$total_function_count++;
if($tokens[$i+1][0] == T_WHITESPACE && $tokens[$i+2][0] == T_STRING){
$in_function = 1;
$function_key = "function ".$tokens[$i+2][1];
$results[$function_key] = 1; //set the new function in results with a score of 1
$i+=2;
}elseif($tokens[$i+1][0] == T_WHITESPACE && $tokens[$i+2] == '&' && $tokens[$i+3][0] == T_STRING){
//handle functions with return references
$in_function = 1;
$function_key = "function &".$tokens[$i+3][1];
$results[$function_key] = 1; //set the new function in results with a score of 1
$i+=3;
}elseif($tokens[$i+1][0] == T_WHITESPACE && $tokens[$i+2] == '&' && $tokens[$i+3][0] == T_WHITESPACE && $tokens[$i+4][0] == T_STRING){
//handle functions with return references
$in_function = 1;
$function_key = "function & ".$tokens[$i+4][1];
$results[$function_key] = 1; //set the new function in results with a score of 1
$i+=4;
}else{
die("unknown function format on function #".$total_function_count."\n\n");
}
} //end if $token == T_FUNCTION
if($token == '{'){
$brace_count++;
}elseif($token == '}'){
$brace_count--;
if($brace_count == 0){
$in_function = 0;
$function_key = 'Global Code';
}//end if brace_count == 0
}// end elseif '}'
$results[$function_key] += count_valid_tokens($token, $complexity_type);
$i++;
}//end foreach
return $results;
}//end function pill
function count_valid_tokens($token,$complexity_type){
$complexity = 0;
switch ($token) {
case T_BOOLEAN_AND: if($complexity_type == 2){$complexity++;}break;
case T_BOOLEAN_OR: if($complexity_type == 2){$complexity++;}break;
case T_LOGICAL_AND: if($complexity_type == 2){$complexity++;}break;
case T_LOGICAL_OR: if($complexity_type == 2){$complexity++;}break;
case T_CASE: if($complexity_type < 3){$complexity++;}break;
case T_SWITCH: if($complexity_type == 3){$complexity++;}break;
case '?': //ternary operator
case T_CATCH:
case T_ELSEIF:
case T_FOR:
case T_FOREACH:
case T_IF:
case T_WHILE: //will cover do/while also
$complexity++;
break;
}//end switch
return $complexity;
}// end function count_valid_tokens
function traverse_directory($dir,$complexity_type,$do_recursion,$do_asp){
$file_results = Array();
foreach(scandir($dir) as $key => $file){
if (preg_match('/\.(php|php3|php4|php5)$/i',$file) ){
$file_scores = pill(harvest_file_contents("$dir/$file",$do_asp),$complexity_type);
$file_results = array_merge($file_results,attach_file_to_scores("$dir/$file",$file_scores));
}elseif($do_recursion && is_dir("$dir/$file") && !preg_match('/^\./', $file)){
$file_results = array_merge($file_results,traverse_directory("$dir/$file",$complexity_type,$do_recursion,$do_asp));
}
}//end foreach $file
return $file_results;
}//end traverse directory
function process_command_line($command_line){
global $usage,$DEFAULT_WARN_LEVEL, $DEFAULT_ERROR_LEVEL;
$options = Array( 'RECURSIVE' => false,
'WARN_LEVEL' => $DEFAULT_WARN_LEVEL,
'ERROR_LEVEL' => $DEFAULT_ERROR_LEVEL,
'DO_WARN' => false,
'DO_ERROR' => false,
'DO_ALL' => false,
'FILE_PATH' => $command_line[1],
'ASP' => false,
'CC_TYPE' => 1
);
if(count($command_line) < 2 ){
die($usage);
}elseif(count($command_line) == 2){
$options['DO_ERROR'] = true;
$options['SORT'] = true;
}
for($i=2;$i<count($command_line);){
switch($command_line[$i]){
case '-r': $options['RECURSIVE'] = true; break;
case '-w': $options['DO_WARN'] = true;
if(is_numeric($command_line[$i+1])){
$i++;
$options['WARN_LEVEL'] = $command_line[$i];
}
break;
case '-e': $options['DO_ERROR'] = true;
if(is_numeric($command_line[$i+1])){
$i++;
$options['ERROR_LEVEL'] = $command_line[$i];
}
break;
case '-a': $options['DO_ALL'] = true; break;
case '-asp': $options['ASP'] = true; break;
case '-cc1' :
case '-cc' : $options['CC_TYPE'] = 1;break;
case '-cc2' : $options['CC_TYPE'] = 2;break;
case '-cc3' : $options['CC_TYPE'] = 3;break;
default: die($usage);
}//end switch
$i++;
}//end for $i
return $options;
}//end function process_command_line
function score_files($file_path,$complexity_type,$do_recursion,$do_asp){
$results = Array();
if(is_file($file_path)){
$file_scores = pill(harvest_file_contents($file_path,$do_asp),$complexity_type);
$results = array_merge($results,attach_file_to_scores($file_path,$file_scores));
}elseif(is_dir($file_path)){
$results = traverse_directory($file_path,$complexity_type,$do_recursion,$do_asp);
}else{
die("\ncould not find $file_path\n");
}
return $results;
}//end function score_files
function attach_file_to_scores($file,$file_scores){
$results = Array();
foreach($file_scores as $function_name => $score){
array_push($results,Array('file'=>$file,'function'=>$function_name,'score'=>$score));
}
return $results;
}//end attach_file_to_functions
function cmp_scores($a,$b){
//sort by score and then by function name
if($b['score'] > $a['score']){
return 1;
}elseif($b['score'] < $a['score']){
return -1;
}else{
return strcmp($a['function'],$b['function']);
}
}
function sort_scores(&$scores){
return uasort($scores,"cmp_scores");
}
function categorize(&$scores,$error,$warn,$all){
$results = Array('error' => Array(), 'warn' => Array(), 'all' => Array());
foreach($scores as $score_key => $score_value){
if($error && $error <= $score_value['score']){
array_push($results['error'],$score_value);
}elseif($warn && $warn <= $score_value['score']){
array_push($results['warn'],$score_value);
}else{
array_push($results['all'],$score_value);
}
}
$scores = $results;
} //end categorize
function print_score_output($scores,$error,$warn,$all){
sort_scores($scores);
categorize($scores,$error,$warn,$all);
echo "\n";
if(count($scores['error']) > 0 && ($error || $all) ){
echo "Errors:\n";
print_block($scores['error']);
}
echo "\n";
if(count($scores['warn']) > 0 && ($warn || $all) ){
echo "Warnings:\n";
print_block($scores['warn']);
}
echo "\n";
if(count($scores['all']) > 0 && $all){
echo "Good:\n";
print_block($scores['all']);
}
}//end print_score_output
function print_block($scores){
foreach($scores as $score_key => $score_value){
$file = $score_value['file'];
$function_name = $score_value['function'];
$score = $score_value['score'];
echo sprintf("%5d - %s : %s\n",$score,$file,$function_name);
}
}
function harvest_file_contents($file,$process_asp){
$text = file_get_contents($file);
if($process_asp){
$text = preg_replace('/<%/','<?',$text);
$text = preg_replace('/%>/','?>',$text);
}
return $text;
}//end function harvest_file
?>