Using a Git pre-commit hook to do PHP syntax checking

Want to make sure you never check in code with PHP syntax errors? I know that you never make mistakes but I do and the Git pre-commit script shown below is a great way to guard against you wearing the “I broke the site” hat for the rest of the day.

First, a bit of background. Git runs a series of external hook scripts during the commit process. The first script is the “pre-commit” hook which can terminate the commit process by returning a non-zero return code. The output of the script is then shown to the user so they can take the necessary steps to allow the pre-commit script to return that its ok to continue. We are going to use this hook to run a syntax check over all the code using PHP’s built-in command line lint command.

Here is the pre-commit script:

# NOTE: change the path above to point to your PHP executable
# in *nix its usually /usr/bin/php

$output = array();
exec("find {$path} -name '*.php' | xargs -n1 php -l | grep -v 'No syntax errors detected'", $output);
exit(count($output) > 0 ? 1 : 0);

And here is how you use it:

  1. Save the script above in your .git/hooks/ directory with the filename of “pre-commit” (no extension) with the $path variable set to the root directory of your PHP files (if you are hosting Git under *nix you should set the permissions on the script so that you can execute it).
  2. Make a trivial breaking change to your code (like removing a semi-colon somewhere).
  3. Using the command line or your favorite Git gui commit the change.
  4. Wait a bit while the files are linted. You should see a message telling you which file and line contains the error you just introduced and the commit should fail.
  5. Fix your trivial breaking change and try the commit again. It should work this time.

One note: if you have a HUGE codebase you should switch to using this script which only performs the lint check on the files that are changing in the commit. I do most of my PHP development on a Window’s PC using a combination of Wamp and msysGit and that script caused a cascade of opening and closing cmd.exe windows to pop up which I found rather annoying. The script above only causes a single cmd.exe window to pop up during the lint check.

Leave a comment