TextMate tip: Validate PHP syntax when saving

The JavaScript Tools bundle for TextMate automatically runs JavaScript Lint on your files when saving and shows a little tooltip if there are any errors (like the missing semicolon below).

It’s very easy to enable similar functionality for PHP files. The default TextMate PHP bundle already comes with a ‘Validate Syntax’ command but it’s bound to Ctrl+Shift+V. Here’s what you need to change:

  1. Open up the Bundle Editor by hitting Ctrl+Option+Apple+B or opening the Bundles menu and selecting Bundle Editor > Show Bundle Editor.
  2. Find the PHP bundle in the list on the left and open it up.
  3. Select the ‘Validate Syntax’ command and then change it’s ‘Save’ option to ‘Current File’ in the dropdown on the right.
  4. Change the keyboard shortcut to Apple+S (or whatever you use to save) in the lower right. Your settings should now look like this.
  5. Close the Bundle Editor (it saves your changes automatically).

Below is an example of what you’ll see when saving. It will even move your cursor to the line with the error!

Update (2008-5-23)

You can prevent the tooltip from displaying when there are no syntax errors by changing the command to:

#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/textmate'
version = %x{#{ENV['TM_PHP'] || 'php'} -v}.split[0..2].join(' ')
#puts "Running syntax check with " + version + "…"
result = `#{ENV['TM_PHP'] || 'php'} -d display_errors=on -l`
puts result.gsub('in -', '') unless result =~ /No syntax errors/
TextMate.go_to :line => $1 if result =~ /line (\d+)/