Setting up JetBrains PhpStorm for use as a Drupal IDE

1st Dec 2011 // By Chris // Drupal Planet

JetBrains PhpStorm, is, surprisingly, an incredible IDE, and works extremely well for Drupal development. We took some time to go through all the various custom settings and get things set up to obey Drupal's coding standards, so we thought we'd share.

First of all, JetBrains's PhpStorm IDE is payware. Second of all, I lost so many hours of my life to frustrations with Eclipse that I was quite happy to fork out the relatively low cost once I'd given it a trial. This is not a post to discuss IDEs or the merits of one platform above another, but suffice is to say that PhpStorm, against all odds, is now my preferred IDE on Linux. YMMV.

I'm going to assume you've set it up, you're using Ubuntu (mostly it won't matter what OS you're on for this blog post) and you're using the Sun version of Java. You are using the Sun version, right?!

Basic settings

Head into the settings from File > Settings.

First, go to Editor and in the Other section, set Strip trailing spaces on Save to All. Also, turn on Ensure blank line before end of file on Save.

Now, expand Editor and head to Editor > Apperance. Turn on Show line numbers unless you love not knowing what line you're on.

Head to Code Style > PHP and make sure Global settings is in use at the top, otherwise you'll have to set these options for each project you work on. Change Tab size to 2 (usually it defaults to 4) and also change Indent and Continuation indent to 2 too.

The rest of the settings in Code style > PHP are actually very sensible, and the only other thing you might want to consider is in the Other tab, turning on Indent code in PHP tags which is not a Drupal standards issue, but I do prefer code that way.

Next, check that under File encodings, the IDE encoding is on UTF-8. You will also want to make sure that under Code Style > General you have set Line separator (for new files) to Unix (this is especially important if you're using Windows) and set Right margin (columns) to 80.

Again, under Code Style > General, set Tab size, Indent, and Continuation indent to 2. Head into each section of Code Style, such as CSS, HTML, etc and make sure all indents are set to 2 on everything.

File types

Drupal has some unusual file extensions and you will need to configure these under PhpStorm to be able to edit them. Head to File Types, click PHP files in the upper pane, and add *.install*.module, *.profile, *.test, and *.theme.

Inspections

Inspections are a really powerful way of letting you know you're writing crap code, and will often catch things that most humans will miss. You absolutely don't want inspections to be showing you any false positives if possible, since this will create a 'boy who cried wolf' effect where you won't notice things that are genuinely wrong because you're so accustomed to looking at 'wrong' inspections.

Head to Inspections. For me, it's best to set up the global inspections and use them for all projects, so that's what we'll cover here, although it's also possible to have different inspections for different projects. First, choose Default in the drop-down at the top left (not Project default) and click Share profile.

Turn off the Spelling inspection, unless you really enjoy your IDE telling you that function names are not valid English words! Alternatively you can open it up, choose the Typo inspection, and configure it to only spell check in comments, but I prefer to just disable it and spell things properly myself.

Open up PHP > PHPDoc. You will probably want to turn off Missing @return statement and PHPDoc comment matches function/method signature, and turn on Missing PHPDoc comment. The reason is that under Drupal standards, you don't need to fully document hook implementations. If you leave on those inspections, you will get a lot of errors. On the other hand, you should never have any function that has no documentation for it, even if it's just to say it's implementing a hook, so turning on that inspection will help you out.

Head to PHP > Undefined and turn off undefined variable and undefined field. The reason is that, for whatever reason, it was decided that Drupal should use stdClass objects as data stores, rather than arrays, so you end up with things like $node->title, which, since $node is an object of type stdClass, and not a proper object, generates an undefined field warning. You might need to turn off more of these, so try it out and see how it goes.

Project-specific

Once you've actually opened up a project (which should be as simple as opening a directory, as PhpStorm can detect things like whether it's under git source control), you will want to set up some include paths. This only applies if, like us, you run Drupal multi-site, and your project is just the site itself, rather than the entire Drupal installation. If you added the entire Drupal installation as your project, you needn't worry about this, as it's taken care of for you.

In the project panel, right-click where it says External Libraries and choose Configure Include Paths. Now, add 3 new include paths. These will be /path/to/drupal6/includes, /path/to/drupal6/modules, and /path/to/drupal6/sites/all/modules. This will allow PhpStorm to know about all Drupal's internal functions and any functions inside modules like views where these are stored outside of the current project.

Enjoy

That should be it. You're now good to go develop a Drupal site, module, theme or whatever it is you're doing using PhpStorm. If I did miss anything off this list, do let me know in the comments and I can add it in.

Quick update: Thanks to Timon Royer for letting us know about his PhpStorm and Drupal video which runs you through some of the ways PhpStorm makes your life as a Drupal developer easier. He seems to be a mac user, but we can overlook that, right? ;)

About The Author

Chris's Profile Picture
Chris

Chris Cohen is a seasoned web developer who began in Perl, before moving on to Java, C# and currently PHP and Drupal. He regularly attends DrupalCon and is "Certified to Rock" (certifiedtorock.com).

22

Comments

dstol's picture

It would be great if this were added to the documentation on d.o. Check out http://drupal.org/node/147789 and add a child page called Configuring PHPStorm.

Chris's picture
Thanks for the suggestion! We'll look into this!
infojunkie's picture

PhpStorm does look interesting - I also had given up on Eclipse a long time ago and was working with Vim. Thanks for showing the settings!

ruffin's picture

If you like VIm, try the IdeaVIM plugin on PHPStorm. I was sold with PHPStorm out of the box, but being able to use VIm keystrokes inside has kept it 'Storm as my #1 tool.

The plugin's a little buggy -- occasionally, eg, undo undoes a ton more than it oughta -- but overall it's a great plus in the pro-PHPStorm column.

DjebbZ's picture

I created a project on Github containing snippets (aka Live Templates) of all core hooks (D7) for PHP Storm. Check it out and contribute ! https://github.com/DjebbZ/Drupal-PHPStorm-Live-Templates

Anonymous's picture

mm

scorchio's picture

PhpStorm is definitely my IDE of choice for Drupal development and version 3 just made me even more sure.

If you would like to work in PhpStorm with a nice color scheme, I can recommend Ethan Schoonover's Solarized profiles. You can find them here:
http://ethanschoonover.com/solarized
(Grab the IntelliJ IDEA one, it will work the same in PhpStorm.)

Will check out DjebbZ's snippets :)

Heine's picture

The Strip trailing whitespace should be set to "Modified Lines" if you are preparing a core feature patch. This prevents spurious changed lines.

Alternatively, file a whitespace patch first :).

I would not turn off undefined variable inspection, it's very helpful.

Also, there's no need to adhere strictly to the substandard Drupal code standards for in-house projects. Eg: I do include param and return types in PHPdoc; both the additional documentation and the automated checking by PHPstorm are very handy.

Drupal Theme Garden's picture

Nice article, but I have a question.
What's wrong with eclipse?

Chris's picture
That's a whole other story I believe. The post was quite clear in being an instructional post on PhpStorm, rather than a discussion or critique of different IDEs, so if you'd like to have that discussion, I'd prefer it wasn't here!
Sam's picture

This is probably a silly question, and something I'm doing wrong.

When I create a new project from a git repo on a remote server, everything sets up correctly. However, in the Project panel (directory navigation), I can only see files, and not folders (have to use the navigation bar which is annoying). How can I get it to show the whole directory structure?

Also, I cloned a full drupal installation, and it's telling me that all of Drupal's built-in functions can't be found. I also tried adding the folders mentioned in the include path, but still nothing.

Chris's picture
You will need to add the correct content types (like *.module) as PHP types in PhpStorm otherwise it won't scan those files and hence won't find Drupal's built-in functions. As for the other thing, do make sure you have "project" selected in the 'View as' drop-down. I can't think of a setting that would affect the display of directories, but the default is definitely to show them!
Sam's picture

This is indeed bizarre. I've definitely included all the correct extensions, am using the root at the project, and this is what I get:

http://cl.ly/0i1C2b2a083F0A0y2G2f

Seems like a nice IDE, but I can't even get started!

Thanks for the help.

Chris's picture
Sorry but none of us have much experience of PhpStorm on Mac! Could this possibly be a permissions issue or something?
Sam's picture

Thats a good thought. Changed to 777 and same problem. Interestingly enough, a different repo on the same remote server worked fine. I re-initialized the problem repo and now it's working well. No idea what the issue could have been!

Anyway, thanks for the writeup. Been searching for an IDE I actually like, maybe this one will be it.

Kevin's picture

PHPStorm is pretty sluggish and locks up often on OSX. Anyone know why?

Heine's picture

I did not experience this on OS X. If you want, post a support request on the phpstorm website (http://www.jetbrains.com/support/phpstorm/index.html). Do not forget to tell them about your system and project setup.

Make sure that you selected the correct scenario when you created a project. It is very important to not work on network-mounted drives without a local backing project with autosync.

In such case you should select the option "Web server is on remote host, files are accessible via network share or mounted drive." or you won't have a great experience.

Timon's picture

I've created a video that shows how to use PhpStorm with Drupal. The tutorial shows important PhpStorm features and how to use them in Drupal projects. Learn how to use the most common hotkeys for navigating and editing your code. http://youtu.be/J786StrcGmc?hd=1

Chris's picture

Thanks. Great video! I've linked it in the article as it's going to be really useful to anyone who wants to try PhpStorm for Drupal.

demonkoryu's picture

Thanks for this. IDEA/PhpStorm is one of the very few commercial web development tools I'm willing to pay for.

temaruk's picture

Great article! I would like to add that (at least in PHPStorm 3.x) according to the 'Undefined field' inspection's description:

"Check is not performed on objects of type "stdClass" or derived."

So no need to turn that off, right?

Anonymous's picture

Thanks for your Post..

Post new comment

The content of this field is kept private and will not be shown publicly.

New job vacancies button

Categories

Search