Search code examples
phphtmltodo

How to scan a project for "@todo" source code comments


Is there any way to scan a codebase for any TODO's and generate a list that can be displayed on a standard web page.

E.g.

@todo Deprecated function remove......... (functions.php [Line 12])

This needs to work on a local WAMP server.


Solution

  • On a Windows platform or if you wanted to use PHP itself, you could use...

    function getTodos($path) {
       $todos = array();
       $items = glob(rtrim($path, '/') . '/*');
    
       foreach($items as $item) {
    
           if (is_file($item) AND pathinfo($item, PATHINFO_EXTENSION) == 'php') {
               $fileContents = file_get_contents($item);
    
               $tokens = token_get_all($fileContents);
    
               foreach($tokens as $type = $token) {
                   if (($type == 'T_COMMENT' OR $type == 'T_ML_COMMENT')
                       AND preg_match_all('/^\s*(?P<todo>@todo.*?)\z/m', $token, $matches) {
                      $todos = array_merge($todos, $matches['todo']);
                   }
               }
    
           } else if (is_dir($item)) {
               $todos = array_merge$($todos, getTodos($item));
               continue;
           }       
    
       }
    
       return $lines;
    }
    

    I have not tested it, but it should work in theory. :)

    On *nix, you could use grep...

    $ grep -r \b@todo\b ./
    

    It's not perfect (it will find it within strings) but it should be good enough. :)