Tech Blog :: Alias to validate PHP and stage to Git in one step


Dec 28 '10 1:50am
Tags

Alias to validate PHP and stage to Git in one step

One of the biggest faux pas that can happen working with PHP apps like Drupal (or any interpreted language for that matter) is deploying invalid syntax that brings down a whole site. So I like to run my PHP files through the PHP validator before committing. I'm working with Git, so this alias (put in .bash_profile for example) validates PHP files and then stages them to Git for committing (or doesn't stage them if there are errors). I'm including the extensions .php, .module, .install, and .inc as PHP files (please let me know if I'm missing any common ones):

[Update 1/9/11: I replaced the original alias syntax with a new function syntax that works more reliably.]

function git-v-add {
 git st --short | while read FILE; do echo $FILE | awk '{ print $2 }' | grep -E "(\.module|\.php|\.install|\.inc)$" | xargs php -l || return 1;
 done && \
 git add -A && git st
}

Then just run git-v-add && git commit.

This is awesome. I will definitely be adding to my workflow.

Post new comment

Don't bother putting in spam links. They'll be set to rel=nofollow and will be removed and reported as spam shortly after submitting.

The content of this field is kept private and will not be shown publicly.
CAPTCHA
Are you human?