2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2009 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
22 $path = '/Server/htdocs/sportal/';
23 $filelist = fetch_flat_listing($path);
25 foreach ($filelist as $filename)
27 $fpath = $path . $filename;
28 $file = file_get_contents($fpath);
29 $templist = fetch_methods($file);
31 $elements['methods']['classes'] = array_merge($elements['methods']['classes'], $templist['methods']['classes']);
32 $elements['methods']['functions'] = array_merge($elements['methods']['functions'], $templist['methods']['functions']);
33 $elements['limiters']["$fpath"] = $templist['limiters'];
35 $perfile["$fpath"] = $templist['methods'];
38 foreach ($elements['methods']['functions'] as $name)
40 $count['functions']["$name"] = 0;
43 foreach ($elements['methods']['classes'] as $name => $methods)
45 foreach ($methods as $method)
47 $count['classes']["$name"]["$method"] = 0;
51 foreach ($filelist as $filename)
53 $fpath = $path . $filename;
55 $file = file_get_contents($fpath);
56 $count = fetch_calls($file, $elements['methods'], $elements['limiters']["$fpath"], $count);
61 // ###################################################################
62 // ###################################################################
63 // ###################################################################
65 function fetch_listing($path, $basepath = '', $unset = 1)
74 if (substr($path, (strlen($path) - 1), 1) != '/')
79 if ($handle = opendir($path))
81 while (($file = readdir($handle)) !== false)
83 if (substr($file, 0, 1) != '.' && $file != 'CVS')
85 if (is_dir($path . $file))
87 $filelist["$basepath"][] = "$file/";
88 fetch_listing("$path$file", "$basepath$file/", 0);
92 $filelist["$basepath"][] = $file;
101 // ###################################################################
103 function fetch_flat_listing($path)
105 $filelist = fetch_listing($path);
107 foreach ($filelist as $basepath => $files)
109 foreach ($files as $file)
111 if (substr($file, (strlen($file) - 1), 1) != '/' && preg_match('#\.php$#', $file) && !strpos($basepath, '3rdparty') && !strpos($basepath, 'phpdoc') && !strpos($basepath, 'jpgraph'))
113 $flatlist[] = "./$basepath$file";
120 // ###################################################################
122 function fetch_calls($file, $methodlist, $limiters, $count)
124 static $objects, $extends;
127 $file = preg_replace('#/\*(.*?)\*/#s', '', $file);
130 $tokens = token_get_all($file);
133 static $validtokens, $othertokens;
134 if (!is_array($validtokens))
136 $validtokens = array('T_EXTENDS', 'T_FUNCTION', 'T_VARIABLE', 'T_STRING', 'T_NEW', 'T_DOUBLE_COLON', 'T_PAAMAYIM_NEKUDOTAYIM', 'T_OBJECT_OPERATOR');
137 $othertokens = array('(', ')', ';');
143 // actual function calls
146 // remove invalid tokens
148 foreach ($tokens as $id => $bit)
150 unset($tokens["$id"]);
153 if (in_array($token_name = token_name($bit[0]), $validtokens))
156 $gt["$i"][2] = $token_name;
163 if (in_array($bit, $othertokens))
167 if ($bit == '(' || $bit == ')')
169 $gt["$i"][2] = 'P_' . ($bit == '(' ? 'OPEN' : 'CLOSE');
181 // get what we're looking for
182 $triggers = array('new' => array(), 'popen' => array());
183 foreach ($methodlist['classes'] as $name => $methods)
185 $triggers['new'][] = $name;
186 $triggers['popen'] = array_merge($triggers['popen'], $methods);
188 $triggers['popen'] = array_merge($triggers['popen'], $methodlist['functions']);
190 // find a list of all the defined objects
191 foreach ($gt as $id => $bit)
193 $prevbit = $gt[ $id - 1 ];
194 $nextbit = $gt[ $id + 1 ];
196 if ($bit[2] == 'T_NEW')
198 if ($prevbit[2] == 'T_VARIABLE' && $nextbit[2] == 'T_STRING' && in_array($nextbit[1], $triggers['new']))
200 $objects["$prevbit[1]"] = $nextbit[1];
203 if ($bit[2] == 'T_EXTENDS')
205 if ($prevbit[2] == 'T_STRING' && $nextbit[2] == 'T_STRING' && in_array($prevbit[1], $triggers['new']) && in_array($prevbit[1], $triggers['new']))
207 $extends["$prevbit[1]"] = $nextbit[1];
216 $functlist = array();
217 foreach ($gt as $id => $bit)
220 $prevbit = $gt[ $id - 1 ];
221 $nextbit = $gt[ $id + 1 ];
223 // handle object calls
224 if ($token == 'T_DOUBLE_COLON' || $token == 'T_PAAMAYIM_NEKUDOTAYIM' || $token == 'T_OBJECT_OPERATOR')
229 if (isset($objects["$prevbit[1]"]))
231 $classname = $objects["$prevbit[1]"];
233 else if (in_array($prevbit[1], $triggers['new']))
235 $classname = $prevbit[1];
237 // we've got an extension!
238 else if ($prevbit[1] == 'parent')
240 // find a class that the call could be in
241 foreach ($limiters as $class => $limits)
243 if ($bit[3] > $limits[0] && $bit[3] < $limits[1])
249 // get the parent of that object
250 if (isset($methodlist['classes']["$refclass"]))
252 $tempclassname = $extends["$refclass"];
253 if (isset($methodlist['classes']["$tempclassname"]))
255 $classname = $tempclassname;
259 // we've got an inner-object call
260 else if ($prevbit[1] == '$this')
262 // find a class that the call could be in
263 foreach ($limiters as $class => $limits)
265 if ($bit[3] > $limits[0] && $bit[3] < $limits[1])
271 if (isset($methodlist['classes
']["$refclass"]))
273 $classname = $refclass;
277 // object name validation
280 // method call validation
281 if (in_array($nextbit[1], $methodlist['classes
']["$classname"]))
283 $count['classes
']["$classname"]["$nextbit[1]"]++;
288 else if ($token == 'T_STRING
' && $nextbit[2] == 'P_OPEN
' && $prevbit[2] != 'T_FUNCTION
' && $prevbit[2] != 'T_DOUBLE_COLON
' && $prevbit[2] != 'T_PAAMAYIM_NEKUDOTAYIM
' && $prevbit[2] != 'T_OBJECT_OPERATOR
' && !in_array($bit[1], $triggers['new']))
292 $functlist["$id"] = $bit[1];
295 else if ($token == 'P_OPEN
' && $incall)
297 array_push($stack, $id);
299 // close parens in call
300 else if ($token == 'P_CLOSE
' && $incall)
302 // find the most recent function
303 $end = array_pop($stack) - 1;
305 // see if we can increment a function
306 if (in_array($functlist["$end"], $methodlist['functions
']))
308 $count['functions
'][ $functlist["$end"] ]++;
311 // we're done with all functions
and the semi colon is next
312 if (count($stack) == 0 && $nextbit[2] == 'SC')
322 // ###################################################################
324 function fetch_methods($file)
327 $file = preg_replace('#/\*(.*?)\*/#s', '', $file);
330 $tokens = token_get_all($file);
333 static $validtokens, $othertokensopen, $othertokensclose, $othertokens;
334 if (!is_array($validtokens))
336 $validtokens = array('T_CLASS', 'T_FUNCTION', 'T_STRING');
337 $othertokensopen = array('{', '${', 'T_CURLY_OPEN', 'T_DOLLAR_OPEN_CURLY_BRACES');
338 $othertokensclose = array('}');
339 $othertokens = array_merge($othertokensopen, $othertokensclose);
345 // actual named items
348 // remove invalid tokens
350 foreach ($tokens as $id => $bit)
352 unset($tokens["$id"]);
355 // if we have a T_CLASS, T_FUNCTION, or T_STRING
356 if (in_array($token_name = token_name($bit[0]), $validtokens))
359 $gt["$i"][2] = $token_name;
364 else if (in_array($token_name = token_name($bit[0]), $othertokens))
367 $gt["$i"][1] = $bit[1];
368 $gt["$i"][2] = 'B_' . (in_array($token_name, $othertokensopen) ? 'OPEN' : 'CLOSE');
373 // if we have an opening or closing brace
374 else if (in_array($bit, $othertokens))
376 // make up some token information
379 $gt["$i"][2] = 'B_' . (in_array($bit, $othertokensopen) ? 'OPEN' : 'CLOSE');
386 $elements = array('classes' => array(), 'functions' => array());
392 foreach ($gt as $id => $bit)
395 $nextbit = $gt[ $id +
1 ];
397 if ($token == 'T_CLASS')
400 $class = $nextbit[1];
402 $elements['classes']["$class"] = array();
404 $limiters["$class"][0] = $bit[3];
406 else if ($token == 'T_FUNCTION')
412 $elements['classes']["$class"][] = $nextbit[1];
416 $elements['functions'][] = $nextbit[1];
419 else if ($token == 'B_OPEN')
421 array_push($stack, $id);
423 else if ($token == 'B_CLOSE')
427 // breaking out of a method
428 if ($inclass && $infunction)
430 if (count($stack) < 2)
435 // breaking out of a class
436 else if ($inclass && !$infunction)
438 if (count($stack) < 1)
441 $limiters["$class"][1] = $bit[3];
444 // breaking out of a function
445 else if (!$inclass && $infunction)
447 if (count($stack) == 0)
456 return array('methods' => $elements, 'limiters' => $limiters);