Yii and PHPStorm

How to configure your PHPStorm to work together with Yii. Code inspection and type hinting are my main points.

PHPStorm

PHPStorm is an IDE similar to Eclipse or Netbeans which has an advanced PHP support. This program is not free and costs 70 € for a single developer. You can download a 30 day testversion from here: Phpstorm-download The installation under GNU/Linux is very easy since you only need to extract it and then you can run the binary.

Import project

You click File->new Project Then you name your project, select the directory and chose to create a new project. If you press OK, it notices that there is already code inside and ask you, if you like to import that one.. say yes. Go to file->settings. Click on PHP and add a PHP interpreter. Click on directories on the right you can exclude protected/runtime and assets. Then click on "add content root"-> choose your yii/framework path, on the right you can exclude cli. To exclude the yiilite.php you have to go on the most left menu under "File types" and add yiilite.php to "Ignore folders and directories".

Enhanced Code Completion

Your local model

To improve code completion, PHPStorm must know which types your methods return. Therefore all public static function model(\$classname=__CLASS__) should have a docstring above: /**@return MyClass|CActiveRecord*/. Also you should have all database attributes as @property at the beginning of the class in the docstring. I wrote an automated script for this here: autoadd @property.

array of objects

PHPStorm doesn't know the content when a function returns "@return array". You can help it with following code:

foreach (User::model()->findAll() as $model) /** @var $model User*/
    $model->doSomething();

views

PHPStorm also doesn't know how the view files are found and how the commands are passed. To help with this you can write following at the top of a view:

/**@var $this UserController*/
/**@var $user User*/
/**@var $count int*/

It seems like it is planned for Version 5 of PHPStorm.

Yii framework files

The problem with Yii framework files is, that many attributes like Yii::app()->log or methods aren't exposed as @docstring parameter. Also because of the way how the framework is configurable, it is possible that the log attribute returns different classes in different projects. I will post you my diff for the framework files.. then you get the idea.. some stuff is project specific but I think most things are valid in every project. yii_phpstorm.diff As you see in the diff, I say that YiiBase returns a CWebApplication - else it would think it is a more generic CApplication and the other things are quite straight forward I think.. in CWebUser I've added some properties, I set with setState inside my application.. Also note, that in more recent Yii versions there are already better docstrings where my patch might be outdated.. my patch is for yii version 1.1.5.

XDebug

In my opinion this was quite easy to setup and since I followed a very nice tutorial I won't write anything else than the link: jitremino.com. I've tried the same with eclipse and it took me hours just to have a bad designed interface for debugging.. the PHPStorm debugger is very intuitive and easy to setup.

Code Inspection

The main usage I have for PHPStorm. It helped me finding several bugs, like nonexistant attributes, uninitialized variables or just wrong code. Try it with rightclicking on your modules folder and then inspect code.. It also has features like automatically fix things and you can define very fine grained which files you want to inspect..

Resources

There are not many articles out there, but I will link them anyway: yii-wiki PHPStorm ticket about yii Comments contain some useful information.

Commentaires: