PHP lint and style checking from Emacs

It’s pretty standard practice at most places I’ve worked to stick a PHP syntax check in the commit hook of the SVN/CVS server. It’s a simple way to reject commits that will break code. It’s also nice to run these checks locally, before you commit. It makes fixing the problems much easier.

To that end, I’ve hacked on compile-mode to get the functionality I want.

(defun php-lint ()
  "Performs a PHP lint check on the current file."
  (interactive)
  (let ((compilation-error-regexp-alist '(php))
        (compilation-error-regexp-alist-alist ()))
    (pushnew '(php "\\(syntax error.*\\) in \\(.*\\) on line \\([0-9]+\\)$" 2 3 nil nil 1)
             compilation-error-regexp-alist-alist)
    (compile (concat "php -l -f \"" (buffer-file-name) "\""))))

(defun phpcs ()
  "Performs a PHPCS lint-check on the current file."
  (interactive)
  (let ((compilation-error-regexp-alist '(php))
        (compilation-error-regexp-alist-alist ()))
    (pushnew '(php "\"\\([^\"]+\\)\",\\([0-9]+\\),\\([0-9]+\\),\\(\\(warning\\)\\|\\(error\\)\\),\\(.*\\)" 1 2 3 (5 . 6) 4)
             compilation-error-regexp-alist-alist)
    (compile (concat "phpcs --standard=Digg --report=csv \"" (buffer-file-name) "\""))))

When you run either of these functions, you get a nice compilation-mode window which lists off the errors in your code. Pressing C-x ` jumps you from one error to another. I recommend that you bind C-x ~ to go back one error:


(define-key osx-key-mode-map "\C-x~" 'previous-error)

Once the errors are corrected, it’s a snap to re-run the check and verify that your code is clean and errror-free.

2008/10/01
Previously On Atomized:

Participate