I need to get a list of functions with their contents (and not just the function name) from a php file. I tried to use regex, but it has many limitations. it does not analyze all types of functions. for example, it fails if the function has if statements for the loop as well.
in detail: I have about 100 included files. each file has a number of declared functions. some files are duplicated in other files. so I want to get a list of all functions from a specific file, and then put this list inside an array, then I will use a unique array to remove duplicates. I read about the tokenizer, but I really don't know how to get it to capture the declared function with its data. all i have is:
function get_defined_functions_in_file($file)
{
$source = file_get_contents($file);
$tokens = token_get_all($source);
$functions = array();
$nextStringIsFunc = false;
$inClass = false;
$bracesCount = 0;
foreach($tokens as $token) {
switch($token[0]) {
case T_CLASS:
$inClass = true;
break;
case T_FUNCTION:
if(!$inClass) $nextStringIsFunc = true;
break;
case T_STRING:
if($nextStringIsFunc) {
$nextStringIsFunc = false;
$functions[] = $token[1];
}
break;
case '(':
case ';':
$nextStringIsFunc = false;
break;
case '{':
if($inClass) $bracesCount++;
break;
case '}':
if($inClass) {
$bracesCount--;
if($bracesCount === 0) $inClass = false;
}
break;
}
}
return $functions;
}
, .
.
?
.