2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Function Finder [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || ################################################################### ||
9 \*=====================================================================*/
11 $path = '/Server/htdocs/sportal/';
12 $filelist = fetch_flat_listing($path);
14 foreach ($filelist AS $filename)
16 $fpath = $path . $filename;
17 $file = file_get_contents($fpath);
18 $templist = fetch_methods($file);
20 $elements['methods']['classes'] = array_merge($elements['methods']['classes'], $templist['methods']['classes']);
21 $elements['methods']['functions'] = array_merge($elements['methods']['functions'], $templist['methods']['functions']);
22 $elements['limiters']["$fpath"] = $templist['limiters'];
24 $perfile["$fpath"] = $templist['methods'];
27 foreach ($elements['methods']['functions'] AS $name)
29 $count['functions']["$name"] = 0;
32 foreach ($elements['methods']['classes'] AS $name => $methods)
34 foreach ($methods AS $method)
36 $count['classes']["$name"]["$method"] = 0;
40 foreach ($filelist AS $filename)
42 $fpath = $path . $filename;
44 $file = file_get_contents($fpath);
45 $count = fetch_calls($file, $elements['methods'], $elements['limiters']["$fpath"], $count);
50 // ###################################################################
51 // ###################################################################
52 // ###################################################################
54 function fetch_listing($path, $basepath = '', $unset = 1)
63 if (substr($path, (strlen($path) - 1), 1) != '/')
68 if ($handle = opendir($path))
70 while (($file = readdir($handle)) !== false)
72 if (substr($file, 0, 1) != '.' AND $file != 'CVS')
74 if (is_dir($path . $file))
76 $filelist["$basepath"][] = "$file/";
77 fetch_listing("$path$file", "$basepath$file/", 0);
81 $filelist["$basepath"][] = $file;
90 // ###################################################################
92 function fetch_flat_listing($path)
94 $filelist = fetch_listing($path);
96 foreach ($filelist AS $basepath => $files)
98 foreach ($files AS $file)
100 if (substr($file, (strlen($file) - 1), 1) != '/' AND preg_match('#\.php$#', $file) AND !strpos($basepath, '3rdparty') AND !strpos($basepath, 'phpdoc') AND !strpos($basepath, 'jpgraph'))
102 $flatlist[] = "./$basepath$file";
109 // ###################################################################
111 function fetch_calls($file, $methodlist, $limiters, $count)
113 static $objects, $extends;
116 $file = preg_replace('#/\*(.*?)\*/#s', '', $file);
119 $tokens = token_get_all($file);
122 static $validtokens, $othertokens;
123 if (!is_array($validtokens))
125 $validtokens = array('T_EXTENDS', 'T_FUNCTION', 'T_VARIABLE', 'T_STRING', 'T_NEW', 'T_DOUBLE_COLON', 'T_PAAMAYIM_NEKUDOTAYIM', 'T_OBJECT_OPERATOR');
126 $othertokens = array('(', ')', ';');
132 // actual function calls
135 // remove invalid tokens
137 foreach ($tokens AS $id => $bit)
139 unset($tokens["$id"]);
142 if (in_array($token_name = token_name($bit[0]), $validtokens))
145 $gt["$i"][2] = $token_name;
152 if (in_array($bit, $othertokens))
156 if ($bit == '(' OR $bit == ')')
158 $gt["$i"][2] = 'P_' . ($bit == '(' ? 'OPEN' : 'CLOSE');
170 // get what we're looking for
171 $triggers = array('new' => array(), 'popen' => array());
172 foreach ($methodlist['classes'] AS $name => $methods)
174 $triggers['new'][] = $name;
175 $triggers['popen'] = array_merge($triggers['popen'], $methods);
177 $triggers['popen'] = array_merge($triggers['popen'], $methodlist['functions']);
179 // find a list of all the defined objects
180 foreach ($gt AS $id => $bit)
182 $prevbit = $gt[ $id - 1 ];
183 $nextbit = $gt[ $id + 1 ];
185 if ($bit[2] == 'T_NEW')
187 if ($prevbit[2] == 'T_VARIABLE' AND $nextbit[2] == 'T_STRING' AND in_array($nextbit[1], $triggers['new']))
189 $objects["$prevbit[1]"] = $nextbit[1];
192 if ($bit[2] == 'T_EXTENDS')
194 if ($prevbit[2] == 'T_STRING' AND $nextbit[2] == 'T_STRING' AND in_array($prevbit[1], $triggers['new']) AND in_array($prevbit[1], $triggers['new']))
196 $extends["$prevbit[1]"] = $nextbit[1];
205 $functlist = array();
206 foreach ($gt AS $id => $bit)
209 $prevbit = $gt[ $id - 1 ];
210 $nextbit = $gt[ $id + 1 ];
212 // handle object calls
213 if ($token == 'T_DOUBLE_COLON' OR $token == 'T_PAAMAYIM_NEKUDOTAYIM' OR $token == 'T_OBJECT_OPERATOR')
218 if (isset($objects["$prevbit[1]"]))
220 $classname = $objects["$prevbit[1]"];
222 else if (in_array($prevbit[1], $triggers['new']))
224 $classname = $prevbit[1];
226 // we've got an extension!
227 else if ($prevbit[1] == 'parent')
229 // find a class that the call could be in
230 foreach ($limiters AS $class => $limits)
232 if ($bit[3] > $limits[0] AND $bit[3] < $limits[1])
238 // get the parent of that object
239 if (isset($methodlist['classes']["$refclass"]))
241 $tempclassname = $extends["$refclass"];
242 if (isset($methodlist['classes']["$tempclassname"]))
244 $classname = $tempclassname;
248 // we've got an inner-object call
249 else if ($prevbit[1] == '$this')
251 // find a class that the call could be in
252 foreach ($limiters AS $class => $limits)
254 if ($bit[3] > $limits[0] AND $bit[3] < $limits[1])
260 if (isset($methodlist['classes
']["$refclass"]))
262 $classname = $refclass;
266 // object name validation
269 // method call validation
270 if (in_array($nextbit[1], $methodlist['classes
']["$classname"]))
272 $count['classes
']["$classname"]["$nextbit[1]"]++;
277 else if ($token == 'T_STRING
' AND $nextbit[2] == 'P_OPEN
' AND $prevbit[2] != 'T_FUNCTION
' AND $prevbit[2] != 'T_DOUBLE_COLON
' AND $prevbit[2] != 'T_PAAMAYIM_NEKUDOTAYIM
' AND $prevbit[2] != 'T_OBJECT_OPERATOR
' AND !in_array($bit[1], $triggers['new']))
281 $functlist["$id"] = $bit[1];
284 else if ($token == 'P_OPEN
' AND $incall)
286 array_push($stack, $id);
288 // close parens in call
289 else if ($token == 'P_CLOSE
' AND $incall)
291 // find the most recent function
292 $end = array_pop($stack) - 1;
294 // see if we can increment a function
295 if (in_array($functlist["$end"], $methodlist['functions
']))
297 $count['functions
'][ $functlist["$end"] ]++;
300 // we're done with all functions
and the semi colon is next
301 if (count($stack) == 0 AND $nextbit[2] == 'SC')
311 // ###################################################################
313 function fetch_methods($file)
316 $file = preg_replace('#/\*(.*?)\*/#s', '', $file);
319 $tokens = token_get_all($file);
322 static $validtokens, $othertokensopen, $othertokensclose, $othertokens;
323 if (!is_array($validtokens))
325 $validtokens = array('T_CLASS', 'T_FUNCTION', 'T_STRING');
326 $othertokensopen = array('{', '${', 'T_CURLY_OPEN', 'T_DOLLAR_OPEN_CURLY_BRACES');
327 $othertokensclose = array('}');
328 $othertokens = array_merge($othertokensopen, $othertokensclose);
334 // actual named items
337 // remove invalid tokens
339 foreach ($tokens AS $id => $bit)
341 unset($tokens["$id"]);
344 // if we have a T_CLASS, T_FUNCTION, or T_STRING
345 if (in_array($token_name = token_name($bit[0]), $validtokens))
348 $gt["$i"][2] = $token_name;
353 else if (in_array($token_name = token_name($bit[0]), $othertokens))
356 $gt["$i"][1] = $bit[1];
357 $gt["$i"][2] = 'B_' . (in_array($token_name, $othertokensopen) ? 'OPEN' : 'CLOSE');
362 // if we have an opening or closing brace
363 else if (in_array($bit, $othertokens))
365 // make up some token information
368 $gt["$i"][2] = 'B_' . (in_array($bit, $othertokensopen) ? 'OPEN' : 'CLOSE');
375 $elements = array('classes' => array(), 'functions' => array());
381 foreach ($gt AS $id => $bit)
384 $nextbit = $gt[ $id +
1 ];
386 if ($token == 'T_CLASS')
389 $class = $nextbit[1];
391 $elements['classes']["$class"] = array();
393 $limiters["$class"][0] = $bit[3];
395 else if ($token == 'T_FUNCTION')
401 $elements['classes']["$class"][] = $nextbit[1];
405 $elements['functions'][] = $nextbit[1];
408 else if ($token == 'B_OPEN')
410 array_push($stack, $id);
412 else if ($token == 'B_CLOSE')
416 // breaking out of a method
417 if ($inclass AND $infunction)
419 if (count($stack) < 2)
424 // breaking out of a class
425 else if ($inclass AND !$infunction)
427 if (count($stack) < 1)
430 $limiters["$class"][1] = $bit[3];
433 // breaking out of a function
434 else if (!$inclass AND $infunction)
436 if (count($stack) == 0)
445 return array('methods' => $elements, 'limiters' => $limiters);
448 /*=====================================================================*\
449 || ###################################################################
452 || ###################################################################
453 \*=====================================================================*/