Royall Spence's Blog

Autocomplete Codeigniter Libraries in PHPStorm

At work today, I was given the unenviable task of fixing some problems in a particularly bad Codeigniter web service. Poor design decisions, inconsistent coding styles, and an overall attitude of carelessness on the part of the prior programmers presented enough obstacles on their own. Codeigniter managed to make matters worse by its method of injecting libraries.

It includes the library file, then assigns it as a member of the controller. The code’s confusing enough to look at since it’s written as though autoloading is not already a standard practice in PHP.

I was able to improve my experience in IntelliJ/PHPStorm by explicitly adding the libraries and models as members of my controller class, then annotating the type of each member. It’s likely Netbeans would benefit from this as well. Just add a PHPDoc @property annotation for the un-declared class members. Declaring the members explicitly seems like a logical approach, but it causes Codeigniter to crash for me. The implicit property declarations are enough to improve the experience:

/**
 * Class m_orders
 * @property m_packages $__pkg
 */
class m_orders extends CI_Model {

This is so easy to do that you ought to go ahead and do it to every Codeigniter project you work on. When someone does a git-blame, they will at least see that you added some useful lines instead of just sitting around helplessly. It’s just one way to leave the code a little cleaner every time you touch it.