Check if your PHP’s diff syntax isn’t breaking

Say you’re working on emacs, without any kind of syntax checking, and you don’t want to commit your code cause you might have syntax errors.

If you’re smart, the best way to know what files you actually changed, is using

svn diff | grep +++

(that is, if you’re using subversion, which I hope you are)

then you’d have to do,

php -l 

on each one of the files…

well, that looks like a script to me.

Add the following script to your path, name it… checkdiffsyntax

#!/bin/bash

#Put all the files that changed on a temp file and then dump them into a MYBLOOP_DIFF_FILES variable
TMP_FILE=/tmp/changed_${USER}
svn diff | grep +++ | awk {'print $2'} > ${TMP_FILE}; export MYBLOOP_DIFF_FILES=`cat ${TMP_FILE}`

for file in $MYBLOOP_DIFF_FILES
do
  php -l $file
done

rm ${TMP_FILE}

UPDATE
I learn how to use for as a one liner on bash, and also how to do this without dumping the filepaths to a file which gets fed into a string… now it just goes straight from awk into the for loop, sick. I will leave the example above for future reference in case you need how to put the contents of a file on a variable and then reading from them

New script looks like this now, one liner.

svn diff | grep +++ | for file in `awk {'print $2'}`; do echo ${file}; php -l ${file}; done

chmod 777 it, and you’re ready to go.

Should look like this when you run it

angel@foobar:~/public_html$ checkdiffsyntax
No syntax errors detected in members/ajax/mashup.php
No syntax errors detected in foo/mashups/twitter/twitter.php
No syntax errors detected in foo/classes/utils.php
No syntax errors detected in foo/classes/user.php

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.