Maggie Nelson

How to quickly lint PHP files in an upcoming svn commit

by maggie on May.15, 2007, under entry

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…)

5 comments for this entry:
  1. 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!

  2. Keith Casey

    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.

  3. Kevin Cox

    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…

  4. Braden

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

  5. Braden

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

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Tags

RSS Planet PHP

  • Module Bootstraps in Zend Framework: Do's and Don'ts
    I see a number of questions regularly about module bootstraps in Zend Framework, and decided it was time to write a post about them finally. In Zend Framework 1.8.0, we added Zend_Application, which is intended to (a) formalize the bootstrapping process, and (b) make it re-usable. One aspect of it was to allow bootstrapping of individ […]
    Matthew Weier O'Phinney
  • Webinar - New in Zend Framework 1.10!
    Zend Framework 1.10 sports a ton of new features, as well as completely revamped online documentation. In this webinar, Matthew Weier O’Phinney, Framework Project Lead, and Ralph Schindler, Zend Framework Software Engineer, will present a synopsis of the new features, discuss the new online documentation system and how it’s built, and answer your questions a […]
    Zend Developer Zone
  • ConFoo PHP 5.3 == Awesome! Slides
    Finally managed to upload my slides from my ConFoo PHP 5.3 == Awesome! talk. Slides Thanks for all the attendees, especially those who asked questions […]
    Ilia Alshanetsky
  • ConFoo PHP 5.3 == Awesome! Slides
    Finally managed to upload my slides from my ConFoo PHP 5.3 == Awesome! talk. Slides Thanks for all the attendees, especially those who asked questions […]
    Ilia Alshanetsky
  • Slides from my Confoo.ca talk now online
    I’ve just uploaded the slides from my confoo.ca talk “Making software management tools work for you”. You can download the slides from here. Thanks again to everyone who attended! Please give me feedback on joind.in. […]
    John Mertic