Code completion in phpStorm with WordPress

phpStorm Logo I use phpStorm pretty much exclusively for PHP development, for over a year.
It simply has some awesome features.

But there are some things that people tend to complain about.
One of the biggest I am going to show you how to fix.

Current Helpers phpStorm Helpers

phpStorm by Jetbrains uses a stricter approach for code completion than most PHP IDE’s.
They index all constructs but for everything else phpStorm requires proper phpDoc annotations.

The reason behind this I think is simply for performance to keep it a bit litter and to have faster start-ups.

I will be doing a few of these examples for popular open source projects that have code completion issues do to their standards or simply not having phpDocs at all.

Once you get the hand of this, its pretty easy to do and works quite well.
Some of these issues are seen in all IDE’s not just phpStorm

So here we go, digging into the code.
The comments explain just about everything you need to know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @see https://gist.github.com/1105126
*/
 
// Also some more hints and fixes
 
// WP functions for adding to the admin bar
// This will provide code completion for admin_bar functions like add_menu()
/** @var $wp_admin_bar WP_Admin_Bar */
global $wp_admin_bar;
 
// Fixing dynamic includes, also so they link properly in the editors environment
require_once(get_template_directory() . '/inc/base.php');
//As is the above require will show an error in phpStorm "Cant resolve target....."
 
//To fix this is easy, this fixes the error and also allows control click on the base.php and opens it
/** @define "get_template_directory()" "/var/www/wp-dev/wp-content/themes/tridium" This is just for the IDE */
require_once(get_template_directory() . '/inc/base.php');
 
// These same methods can be used to fix most errors and completion in phpStorm

Basically for accessing WordPress’s functions and methods that are mostly used eg wpdb, you add this file to the WP root https://gist.github.com/1105126.

I prefer as in the comments of the file to have the class named wpdb and remove the extends.
Like this,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * @property wpdb  $ID
 * @property wpdb  $post_author
 * @property wpdb  $post_date
 * @property wpdb  $post_date_gmt
 * @property wpdb  $post_content
 * @property wpdb  $post_title
 * @property wpdb  $post_excerpt
 * @property wpdb  $post_status
 * @property wpdb  $comment_status
 * @property wpdb  $ping_status
 * @property wpdb  $post_password
 * @property wpdb  $post_name
 * @property wpdb  $to_ping
 * @property wpdb  $pinged
 * @property wpdb  $post_modified
 * @property wpdb  $post_modified_gmt
 * @property wpdb  $post_content_filtered
 * @property wpdb  $post_parent
 * @property wpdb  $guid
 * @property wpdb  $menu_order
 * @property wpdb  $post_type
 * @property wpdb  $post_mime_type
 * @property wpdb  $comment_count
 * @property wpdb  $filter
 * @property wpdb  $term_id
 * @property wpdb  $name
 *
 */
class wpdb {}

Then I drop that file in the WP root.
phpStorm will index the construct and use the listed properties in that file for code completion within the wpdb class.

You should not get duplicate class errors because we are not instantiating it.
Its just there for phpStorm.

I will be doing one of these for phpBB3 and also CodeIgniter so you may want to subscribe and follow future posts.
Current Helpers phpStorm Helpers

2 Responses to “Code completion in phpStorm with WordPress”

  1. mikhail.vink

    Hi! You actually can add PHP STUBS to PhpStorm in order to have this solution works out of the box. Check http://youtrack.jetbrains.com/issue/WI-174 – YouTrack issue WI-174. Just add new related issue and post all the required information. We’ll include it in future releases. Thank you!

Leave a Reply

You must be logged in to post a comment.