8

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?

SeanJA
  • 10,234
  • 5
  • 32
  • 42
  • you can use a global Constant `C_DEBUG` and wrap your debug tests in an `if C_DEBUG...` block. That won't litteraly *prevent* from comitting but if you don't forget to change `C_DEBUG` to false before a commit, it won't be displayed to the user – JMax Sep 26 '11 at 13:00
  • Ya, I was thinking of adding a meta function that would check if it was your user_id and then spit out the debug info... – SeanJA Sep 26 '11 at 13:03
  • I have a build tool that checks with a regex for `var_dump` and `print_r` as well as for commented-out code. Works pretty well, however it's a manual call prior to commit so the developer can check their own code before committing. – hakre Sep 26 '11 at 13:05
  • @JMax That still means training people to do something other than what they have been doing for years... – SeanJA Sep 26 '11 at 13:05
  • @hakre Any reason that build tool couldn't be set up as a precommit hook? – SeanJA Sep 26 '11 at 13:07
  • Because it's interactive. But as it's regex based and works pretty well, could be done. But mind the false positives you can have with static code analysis (even if you use the PHP tokennizer, `T_STRING` is correct btw., it's a string token (not a PHP string variable) containing the function name, do a syntax check first (lint)). – hakre Sep 26 '11 at 13:12
  • @hakre I see, I was expecting it to come out as `T_FUNCTION` instead as it is a function... – SeanJA Sep 26 '11 at 13:15
  • IIRC `T_FUNCTION` stands for the `function` keyword. – hakre Sep 26 '11 at 13:16
  • @hakre Ah, that makes sense, maybe this will work then – SeanJA Sep 26 '11 at 13:20

4 Answers4

0

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;
            }
        }
    }
}
SeanJA
  • 10,234
  • 5
  • 32
  • 42
0

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.

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
  • Heh, not really possible... 4 devs, I don't have control over their code. I want to prevent mistaken commits. – SeanJA Sep 26 '11 at 21:13
0

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

tobyS
  • 860
  • 1
  • 7
  • 15
  • 1
    An interesting option, we aren't using phpcs just yet. I plan to add that as a post commit hook at some point, no point in blocking if it doesn't follow the guidelines, but telling them it doesn't afterwards seems like a good idea. – SeanJA Sep 28 '11 at 14:29
0

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
Oceinic
  • 406
  • 4
  • 15