PHPstorm and Drupal styling

Every programming language and subcommunity has rules regarding code styling. Drupal provides a set of style standards, but how do you know that your code is meeting them? If a collaborator contributes to your project, how do you know that they followed the guidelines?

This miniature guide will walk us through installing a Drupal style sniffer, automatic beautifier, and a continuous integration check for our code styling. The guide is specific for PHPStorm.

Adding the code sniffer

The official Drupal Code Sniffer guide should be sufficient for adding and configuring the code sniffer via composer. This guide will walk you through

  • installing Composer
  • installing the PHP Code Sniffer (PHPCS)
  • installing the coder module
  • registering the Drupal code standard with PHPCS
  • Adding the Drupal standard to PhpStorm.

Adding the code formatter

Unfortunately, the sniffer won't automatically fix the styling for you, it will only alert you to the problem. To automate fixing simple code "smells", we can configure PHPCBF (PHP code beautifier) to run as an external tool in PHPStorm.

I followed this guide by @hamrant to add the tool.

for the program path, use the output of which phpcbf.

Argument would be set to: --standard=Drupal $fileDir$/$FileName$ working directory: $ProjectFileDir$

phpcbf script configuration

(Note: If you get a weird error about your working directory, double check your working directory argument! I had whitespace in mine).

Once the tool is configured, you can map it to a shortcut to make it easier to use. I mapped my tool to run on apple -alt - ; , which is right next to my general beautifier (apple - alt - l).

setting up CI

Finally we want a continuous integration check so that when PRs are made, it runs the code sniffer and gives us feedback on style. I used Code Climate because its free for open source projects, but any service that lets you specify what files to check and what standards to use will work.

The configuration file to ensure that our Drupal codes are read against Drupal standards is simple: place this ..codeclimate.yml file in your project root.

version: "2"
    enabled: true
      file_extensions: "php,inc,module"
      standard: "Drupal"