Parsing a php script for documentation purposes

354 views Asked by At

I've not been able to find any inexpensive tools to document all the vars/consts/functs/classes/includes in a PHP script, so I'm writing my own. (I use a lot of open source scripts that need to be modified. Plus, it would be nice to have a directory for my own projects.)

I've kludged one together using php string functions, finding all the constants, then traversing each line of the script, loading the line number in an array, then the includes/requires, then the variables, etc. This gets about 90% of what I want. If I were to improve it, though, I would probably have change the approach to dealing with the script character by character, just like the php compiler probably does. Anyone have a better idea how to solve this problem? I can't believe I have to invent this wheel...

I suppose there are programming environments that have this functionality built in, but I'm old school and low budget. And, yes, OO and tight functions eliminate the need to document most vars, but there is still need to see the include/require tree, document globals, see where all the instances of a class are used and how., etc.

1

There are 1 answers

1
birchy On

It looks like my best bet is to use the built in Tokenizer (http://php.net/manual/en/ref.tokenizer.php) in php.

$theTokens = token_get_all(file_get_contents($currentFile)) ;
echo '<table>' ;
for ($i=0;$i<count($theTokens); $i++) 
    {
        if (count($theTokens[$i])==3)
        {
            echo '<tr><td>'.token_name($theTokens[$i][0]).'</td><td>'.$theTokens[$i][1].'</td><td>'.$theTokens[$i][2].'</td><tr>' ;
        }
    }
echo '</table>' ;

This ids variables, but processing is required to identify arrays, class ids, etc.