views:

105

answers:

2

I need to get list of functions with their contents (not only the function name) from php file. I tried to use regex but it has lots of limitations. it doesn't parse all types of functions. for example it fails if the function has if and for loop statements.

in details: I have around 100 include files. each file has number of declared functions. some files has functions duplicated in other files. so what I want is to get list of all functions from specific file then put this list inside an array then I will be using array unique in order to remove the duplicates. I read about tokenizer but I really have no idea how to make it grab the declared function with its data. all I have is this:

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;

            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;

            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;

            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }

    return $functions;
}

unfortunately, this function lists only the function names.. what I need is to list the whole declared function with its structure.. so any ideas?

thanks in advance..

+2  A: 

If you got the function names from your get_defined_functions, consider using Reflection API for the remaining work.

Example:

include 'file-with-functions.php';
$reflector = new ReflectionFunction('foo'); // foo() being a valid function
$body = array_slice(
    file($reflector->getFileName()), // read in the file containing foo()
    $reflector->getStartLine(), // start to extract where foo() begins
    $reflector->getEndLine() - $reflector->getStartLine()); // offset

echo implode($body);

Like @nunthrey suggested, you can also use Zend_Reflection to get both: the functions in a file and their content. Example with Zend_Reflection:

$reflector = new Zend_Reflection_File('file-with-functions.php');
foreach($reflector->getFunctions() as $fn) {
    $function = new Zend_Reflection_Function($fn->name);
    echo $function->getContents();
}
Gordon
hi,thanks for the replies really appreciated..for first example you added. I cannot use include because the file I have contains duplicated functions. so when I include it to the main script I get duplicated declared function errors. for second example, I always get this error:"Fatal error: Class 'Zend_Reflection_File' not found"I'm using php v5.2.6do I need to add extensions to make this work?thanks,
ermac2014
@ermac2014 `Zend_Reflection` is a component of http://framework.zend.com/ - you don't need an extension to use it, but you need to download the framework.
Gordon
but this is php code right? I want to use it with php..
ermac2014
@ermac2014 should be right, yes. Why? Did it you it try it and it didn't work?
Gordon
doesn't work I told you before...I get this error: "Fatal error: Class 'Zend_Reflection_File' not found" I need an extension to make it work. which I have no idea which one
ermac2014
You don't need an extension. All you have to do is download Zend Framework. The error you give sounds much more like PHP cannot find the path to the class, which is in Zend/Reflection/File.php.
Gordon
+1  A: 

Try Zend_Reflection http://framework.zend.com/manual/en/zend.reflection.html

nunthrey