Last updated June 11, 2015. Created on April 5, 2013.
Edited by TravisCarden, rivimey, milodesc, dasjo. Log in to edit this page.

There is a more up-to-date article on the phpstorm website.

This article adapted from the original article PhpStorm for Drupal by Angus Mak of Lullabot

Debugging Drupal modules and themes (or Drupal core itself) can be challenging without a good IDE. PhpStorm has earned its place as one of the primary IDE for almost any Drupal-related development. By default, PhpStorm is as Drupal friendly as most other IDEs. However, some of its default syntax and formatting settings conflict with the Drupal Coding Standards. Here are a few tips to make PhpStorm play even better with Drupal.

Contents of this Page

# Keymap

Although not related to Drupal, the Keymap is the first thing to change. By default, PhpStorm comes with keyboard shortcuts that can feel unnatural. Under Preferences, scroll down to select Keymap on the left, and select the keymap that suits your needs. Mac OS X 10.5+ feels the most intuitive to me. You can also further customize the keyboard shortcuts to each of the actions.
keymap

# Syntax and Formatting

Next, let's fix the code style. Drupal recommends less than 80 characters per line, and PhpStorm lets us set that as its default.

2_indent.png

This gives you a nice solid line in the editor showing where the 80 character limit is. I leave the "Wrap when typing reaches right margin" setting unchecked; otherwise, PhpStorm will automatically insert line breaks when I hit 80 characters. Drupal's coding standards do allow more than 80 characters in some situations, so it's best not to require it.

# Syntax

Next, we'll work on PHP syntax. PhpStorm conveniently comes with a predefined style for Drupal that we can use as a starting point.

Drupal style

PhpStorm's "Tabs and Indents" settings should all be set to two characters to match Drupal's indentation standards.

On the "Spaces" tab the defaults are good.

On the "Wrapping and Braces tab", make sure these settings are set correctly:

  • Change Keep control statements in one line to be unchecked
  • Confirm Place braces in class declaration should be End of line
  • Confirm Place braces in function declaration should be End of line
  • Confirm force braces for if() statements should be Always
  • Confirm Else on new line for if() statements should be checked
  • In the function declaration parameters section, you must also check the "Keep ')' and '{' on one line" setting.

Wrapping and braces

  • Confirm Force braces for while() statements should be Always
  • Change 'While' on new line for do...while() statements to be checked
  • Change 'Catch' on new line for for try() statements to be checked
  • Confirm Chop down Array initializer if long should be checked
  • Confirm New line after '(' for Array initializer should be checked
  • Confirm Place ')' on new line for Array initializer should be checked

Wrapping and brances

Finally, on the "Other" tab, be sure that "Convert True/False to uppercase" and "Convert Null to Uppercase" are checked as well. Those changes should be all we need to match Drupal's PHP syntax. You might also want to set up syntax for other languages like HTML, JavaScript, CSS and any CSS preprocessors you may use.

# PHP CodeSniffer

Use PHP CodeSniffer to give extra warnings about Drupal coding standards violations. Follow the instructions to install PHP CodeSniffer and the Drupal coding standards definitions. Once you have phpcs ready, set up PhpStorm's Code Sniffer settings to point at /usr/bin/phpcs. Also, make sure PhpStorm is set up to use the PHP Code Sniffer for code inspection in its "Inspections" settings.

Inspection

Now you should get warnings as reminders for keeping up the coding standards.

Highlight

The Drupal coder sniffer settings need to be located in the PHP/Codesniffer/Standards folder located at /usr/local/pear/share/pear/PHP/CodeSniffer/Standards (location may be differ per environment).

More information on how to configure CodeSniffer in PHPStorm.

Mac users who are using homebrew to update their packages can use the homebrew-php brews available at https://github.com/Homebrew/homebrew-php

On windows enviroment you should use path <your_xampp_folder>/php/phpcs.bat instead of /usr/bin/phpcs (in PhpStorm settings PHP -> Code Sniffer)

# Tips and Tricks

If changing the font size is not something you often do, I would also recommend turning off the "Mouse wheel zoom" feature. I noticed a lot of accidental zooming in and out when using Magic Mouse on OSX. Even when the Mouse Wheel zoom is turned off, I can still use Pinch-to-Zoom on the trackpad when I need to. You can also set up your own keyboard shortcut under Keymap in Preferences.

Editor

# Debugging

One of the biggest advantages to using an IDE like PhpStorm is integration with a good PHP debugger. If you want to XDebug to examine Drupal's internals, see Lullabot's article about configuring Xdebug. It should give you everything you need to set up PhpStorm for serious debugging.

# File Types

To have PhpStorm style Drupal files like .module, .profile, .admin.inc etc as .php files, Under PhpStorm's Preferences, IDE Settings, select File Types. To the right under Recognized Files Types, select PHP files (PHP) and then click the + sign in the Registered Patterns section. If you have followed the above instructions, *.module will have already been added, but you will likely want to add *.admin.inc, *.install, *.profile (among others). Hit Apply and OK to apply your new settings and your files will now all be styled nicely per PhpStorms styles for PHP.

file types for php storm

# Namespaced classes

In .module files PhpStorm by default autocompletes class names to their fully qualified class name ie. namespace + class name. You should instead add a use statement and use only the shortname of the class. To do this, under Editor » Auto import check "Enable auto-import in file scope". The other checkbox is for the same behavior inside namespaced classes. .

# Notes

Coding standards support built-in starting with version 7. See http://confluence.jetbrains.com/display/PhpStorm/Drupal+Development+usin... for a tutorial.

AttachmentSize
1_keymap.png122.35 KB
2_indent.png66.59 KB
4_drupal.png279.18 KB
6_spaces.png150.19 KB
7_wrapping_braces.png156.34 KB
8_wrapping_braces.png146.73 KB
11_inspection.jpg110.86 KB
12_highlight.png993.91 KB
13_editor.jpg154.17 KB
14_file_types.png106.04 KB
auto_import.png9.18 KB

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Elijah Lynn’s picture

-----------------------------------------------
The Future is Open!