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.
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.
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.
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.
Next, we'll work on PHP syntax. PhpStorm conveniently comes with a predefined style for Drupal that we can use as a starting point.
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.
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
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.
Now you should get some nice warnings as reminders for keeping up the coding standards.
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.
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.