Question

I would like to block debug functions var_dump, print_r, etc... from being commited to the repo so that QA can go over things and not report bugs like "There is a huge block of text on all of the pages!!"

I have tried regex (not a great idea... presumably).

I have also tried token_get_all but for some reason, it returns T_STRING for each of the debug functions, which I guess would work, but it seems odd...

Is there a third better way?

Was it helpful?

Solution

Based on my new understanding, this is what I have:

$debug_functions = array('print_r', 'var_dump', 'var_export');

foreach($files as $file=>$ext){
    $file_contents = file_get_contents($file);
    //break the content into tokens
    $tokens = token_get_all($file_contents);
    foreach($tokens as $t){
        //if the token id is an int (sometimes it isn't)
        if(is_int($t[0])){
            //if it matches our debug stuff...
            if($t[0] == T_STRING && (in_array($t[1], $debug_functions) || preg_match('/xdebug_.*?/', $t[1]))){
                echo 'Debug output '. $t[1] . ' found on line '. $t[2] . PHP_EOL;
            }
        }
    }
}

OTHER TIPS

Maybe not the answer you are looking for, but I highly recommend you remove all the print_r, var_dump etc from your code.

  1. Keep your code clean all the time
  2. Those tags are for debugging purposes only.
  3. when you are committing, you should ensure everything works as expected. Altering commit code or having different code on your machine than the live machine ensures of bugs and issues.

So remove those tags, don't keep them in your code, not even on a local machine.

You could write a sniff PHP_CodeSniffer and make SVN execute it as a pre-commit hook. This would reject committing such code.

Alternative way is not using var_dump and related functions at all. Coding better practices includes

  1. Unit testing with PHPUnit and
  2. Using a remote debugger, for example Xdebug
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top