Last updated May 31, 2013. Created by kalman.hosszu on April 5, 2013.
Edited by kbrinner, makangus, Heine. Log in to edit this page.

The original article was created by Angus Mak of Lullabot! Check it on http://www.lullabot.com/articles/phpstorm-drupal

Debugging Drupal modules and themes (or Drupal core itself) can be challenging without a good IDE. After using numerous IDE and text editors, PhpStorm has earned its place as my primary IDE for almost anything Drupal-related. 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.

Keymap

Although not related to Drupal, the Keymap is the first thing I change. By default, PhpStorm comes with keyboard shortcuts I find very 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 its "Spaces" tab, make sure the "After type cast" option is selected.

Spaces

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

  • Keep control statements in one line should be unchecked
  • Place braces in class declaration should be unchecked
  • Place braces in function declaration should be unchecked
  • Always force braces for if() statements should be checked
  • Force braces for while() statements should be checked
  • Else on new line for if() statements should be checked
  • 'While' on new line for do...while() statements should be checked
  • 'Catch' on new line for for try() statements should be checked
  • Chop down Array initializer if long should be checked
  • New line after ( for Array initializer should be checked
  • Place ) on new line for Array initializer should be checked

Wrapping and braces

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

I like to install the PHP CodeSniffer to give myself extra warnings about some Drupal Coding Standards violations. To set that up for yourself, 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 some nice warnings as reminders for keeping up the coding standards.

Highlight

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 our 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

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

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

Comments

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

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

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

Lovely write up but there is one small error.

In the wrapping and braces tab, in the function declaration parameters section, you must also check the "Keep ')' and '{' on one line" setting.

Roger Bailey