Vim: checking PHP code (and Python and...)

Do you hate it as much as I do when you are writing some PHP (or Python or whatever) code in your favorite editor, hit 'save', reload the page in the browser in the hopes that you will see the new most awesome feature you ever wrote, but instead you are greeted by an unfriendly PARSE_ERROR_YOU_FORGOT_A_CLOSING_BRACKET_YOU_STUPID-error? Right, it feels like you just built a rocket, but upon launch you discover you didn't foresee a doorknob and can't get in anymore...

Luckily, if you are using Vim there is a nice plugin to quickly catch these errors (and more). The plugin I'm talking about is Syntastic. The plugin is built as a generic framework to run and process the result of a syntax checker. The syntax checkers themselves are specific for each language. Most of the syntax checkers start an external syntax checker or linter and return the output to Syntastic, telling it how to parse it.

By default Syntastic will automatically run the correct checker every time you save the file. When Syntastic finds some errors or warnings in your code, it can show signs on the offending lines and open a window at the bottom with an overview of the problems (depending on your Syntastic configuration). Using the window with the errors (a Vim location list) you can quickly jump to the lines with errors and warnings. In my configuration Syntastic will automatically open the location list whenever there is a problem and will also close the Location list once the problem is fixed.

Vim showing some errors

Syntastic has support for over 50 languages, including PHP, Python, Ruby, CSS, Puppet, and so on. For a lot of languages, Syntastic comes with support for some alternative or supplementary syntax checkers. For PHP, Syntastic will first use the PHP built-in linter (`php -l`). If you also have PHP_CodeSniffer and/or PHP Mess Detector installed it will also run these tools on your file. For Python it depends on which tools you have installed. If you have only Python, Syntastic will use that and notify you of any syntax errors. Personally I like to use flake8 as it checks both your style against PEP8 rules and also checks your code with pyflakes.

One problem you can have with running extensive checks on your code is that you are overwhelmed by the number of errors and warnings. You have to tame the beast or you will drown in the errors and just ignore them. Luckily this is not verify difficult. When developing Drupal code the first step is to install the PHP_CodeSniffer ruleset for Drupal. This is part of the coder module. However, in my opinion this will still give you too many errors (seriously, is anyone still coding on a computer screen which only has 80 columns??). The easiest solution to this is to create your own ruleset. This is rather easy:

  1. First install the Drupal ruleset at the correct location in PHP_CodeSniffer. On a *nix system this can be done by symlinking: sudo ln -s /path/to/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal
  2. Create a new ruleset.xml and refer to the Drupal ruleset to import the entire ruleset.
  3. In your own ruleset.xml you can exclude rules from the imported ruleset to disable them.

Have a look at my Drupal Ruleset for PHP_CodeSniffer at github for inspiration. For PHP Mess detector the system is pretty much the same. Here is my ruleset for PHP Mess Detector at github.

After you have created your personalized rulesets you have to configure Syntastic to use your own rulesets. This can be done by setting the g:syntastic_phpcs_conf and g:syntastic_phpmd_rules variables from your .vimrc file. You can see my Syntastic configuration in my vimrc at github.

Comments

SPINX Inc.'s picture

This is amazing. Otherwise, finding an error in PHP is too hard. Well, I have just downloaded this editor but not checked yet. Thanks for sharing such a great code checked for PHP.

Alan Smith's picture

Thanks for sharing this useful plugins details. For writing length code it’s problematic to check the proper opening and closing brackets of the code and it creates an error. With syntactic we can help you to come out from the syntax error problem.

Add new comment