In my post about Choosing a Coding Standard, Keith from Casey Software (http://www.caseysoftware.com) responded and talked about CodeSniffer. We had heard a little about this at the conference, but it sorta got lost in the shuffle of things once we got back. The other day I downloaded it and installed it on our dev server to see how it works.
For our purposes, we only cared about validating against the PEAR standard, and since that comes installed by default, it was really easy to get it up and going. CodeSniffer will check most aspects of the standard against your code, displaying error messages and warnings accordingly. Here is the output from a sample file:
FILE: index.php
——————————————————————
FOUND 2 ERROR(S) AND 1 WARNING(S) AFFECTING 3 LINE(S)
——————————————————————
[LINE 8] ERROR: Expected “} else {\n” found “}\nelse {\n”.
[LINE 13] ERROR: Class name does not begin with a captial letter.
[LINE 18] WARNING: Line exceeds 85 characters. It is recommended
that the line be shortened.
——————————————————————
As you can see, its pretty easy to follow and very helpful to someone who is just learning a coding standard.
With CodeSniffer in place, I wanted to figure out a good way to integrate it into our normal routine. If we tried to force ourselves to run all our code through a separate application like CodeSniffer, we probably would only do it for about a week and then get tired of it and never do it again. So I decided to try to build a pre-commit hook into our Subversion repository to check the code against CodeSniffer.
This was not as easy as I thought it would be, but after I got it all figured out, it ended up being a simple little script. For the first draft, I think it is pretty good. It extracts all added or updated *.php files from the transaction and runs them, then outputs any errors or warnings to stderr so Subversion can read them.
If you are interested in downloading the file, it is available from our website (http://itdapps.ncsu.edu/downloads.php). It is written for the Windows world right now, but it should be easy to convert it to whatever. It’s just on Windows because that is what I was testing. Also note that this is entirely experimental and very little testing has been done thus far, but any feedback would be appreciated….if its good feedback
