-1

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.

birchy
  • 509
  • 7
  • 16
  • 2
    http://www.phpdoc.org/ ? – Marc B Nov 19 '14 at 19:43
  • 2
    http://www.phpdoc.org/ –  Nov 19 '14 at 19:43
  • phpdoc is great...except it's picky about the version of php and I can't see where it shows line numbers and it generates ".php.html" files which my server thinks are php. You have to be Linux pro to understand how to install it. I got it sort of running using a machine translation of a Japanese blogger that encountered the same install error that I got. I'll try to learn how to get it to work better on my server, but I'm still needing line numbers. I suppose I could extract the elements phpdoc finds to create a list and then find all occurrences with my existing script. – birchy Nov 20 '14 at 01:54
  • I'd still use PHPDoc. The fact that your server treats `*.php.html`-files as PHP sounds pretty ridiculous and should probably be fixed anyway. Installation doesn't seem too daunting; http://www.phpdoc.org/docs/latest/getting-started/installing.html. I guess if you're not on PHP5.3.3 that could be a problem, but perhaps a better solution than to reinvent this wheel (as you put it) is to just upgrade your PHP version? – powerbuoy Nov 20 '14 at 02:28
  • Also, what exactly is your question? – powerbuoy Nov 20 '14 at 02:28
  • My question is primarily if anyone has alternate approaches to the coding other than creating my own php parser (char by char) and my approach so far of identifing identifiers in one pass and then taking an entire pass through the script(s), noting the line numbers where the identifiers occur. I'm leaving it vague, in hopes someone who has approached this before happens along. One just doesn't know what they don't know, so it never hurts to expose one's lack of finesse in a roomful of experts. – birchy Nov 20 '14 at 05:30
  • Upgrading php is a painful option. I have some very old projects that will likely break and that's a time commitment I'm not anxious to take on willingly. I'm going to attack the html-as-php execution in a separate question. For now, I just rename'd the files and the links to them. Also, phpdoc saved the docuntation files with the root owner/group and wouldn't display some of them in the browser. I had to chown them into submission. – birchy Nov 20 '14 at 05:37
  • If anyone knows of some pseudo-code for a php compiler parser, I'd be interested in studying it. – birchy Nov 20 '14 at 05:47

1 Answers1

0

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.

birchy
  • 509
  • 7
  • 16