« Chocolate Covered SQL | Main | 3. Find x. »

How to quickly lint PHP files in an upcoming svn commit

Nothing more embarassing than committing awesome code with a parse error. Quick

php -l
will take care of that. But if your repository has files under many directories and you've made changes to many files, it can be a pain to php -l all of them as you can only pass one file to "php -l" at a time.

Here's a quick shortcut:

svn stat | grep 'php' | awk '{print "php -l " $2}' | sh

"svn stat" will list all files that have been changed. The grep will filter the result and only show PHP files. The awk command takes the 2nd column of the output and does "php -l" on every file in that column.

Granted, this is convenient but painful to type and error prone. Solution: save it in a file svnlint.sh, then add an alias:

alias svnlint='sh ~/svnlint.sh'

At this point, before every:

svn commit
do:
svnlint

Your PHP files will be free of syntax errors (so you can focus on those "real" bugs...)

Comments (5)

Dave:

If you wanted to be mean you could setup a pre-commit handler to run something like what you've specified, and reject commits that don't compile!

Of course, things might not work out 100%. I've not tried it!

Some people are tying CodeSniffer into their commits too: http://blogs.lib.ncsu.edu/page/web?entry=codesniffer_pear_package

I've integrated it into my "build" process in Eclipse and will add it to my Phing script shortly.

I put the following in ~/bin/svnlint (don't forget to chmod +x it):

#!/bin/sh
svn stat | egrep '(php)|(phtml)' | awk '{print "php -l " $2}' | sh

You can keep going adding extensions to the regex, if you like...

Fantastic. I knew I needed to use awk somehow, but couldn't get it right. Thanks.

And to continue on Kevin's path, '\.(php)|(phtml)$' seems sensible.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

Maggie Nelson is a software developer in New York City. She likes open source (especially PHP) but is willing to break her "free is good" philosophy for Oracle.


Subscribe to my feed

Copyright 2008 Maggie Nelson

New York PHP Community Member

Powered by
Movable Type 3.34