Code Completion for CodeIgniter in phpStorm

phpStorm Logo Open source projects can sometimes be a bit of a pain in phpStorm.
Here I am going to show how to fix code completion for CodeIgniter in phpStorm.

Common Issues

Undefined fields or methods not found.

No code completion.

This one is complained about a lot, undefined vars in views.

When we are done, all of these issues will have solutions.

I have created a document that has CI_Controller class and CI_Model.
The goal here is to not modify CodeIgniter core code, that is always a bad idea.

I wont post the code here because it is quite long.
But I will give links to the repo and Gist on GitHub.
You can decide how to download it.

The setup

  • Download the CI_phpStorm.php file with your preferred method.
  • Put it in the root of your CodeIgniter project (where the index.php is)
  • In phpStorm project nav pain, go to (expand) system/core/
  • Right click on Controller.php and Mark as Plain Text
  • Do the same for Model.php

Marking those files as plain text stops phpStorm from indexing them as sources.

Now if you go to a controller in your project like the default welcome.php, you should have code completion and the earlier issues fixed.

Doc Block Info Popups

Property Completion

Method completion.

As you can see from the above images that all code completion is now working in the controllers.
You should have most of the same completion in models with some annotations here and there.

Code Completion in Views and fixing undefined vars.

Example controller code.

	public function index()
		// Example view vars
		$data['test'] = 'Testing vars in CodeIgniter!     This is from $data["test"].';
		$this->load->view('welcome_message', $data);

We added a data array to the view the CI way.
Each index in the array is another variable.

The view…

		<p style="font-weight: bold;">
			echo $test;

In phpStorm $test will be highlighted as an undefined var.
To fix this we use phpDoc annotations.

<p style="font-weight: bold;">
 * $data array holds the $test value
 * @see Welcome::index()
 * @var Welcome $test
echo $test;

Documenting this way not only fixes the phpStorm error/warning but also gives us documentation popup for $test.
Also the @see will link to the location it was created, in this case index method in the Welcome class.

The var is now defined and shows it is.

Ctrl+ Click on this method link will bring you right to the method where $test is defined.

This should get you headed in the right direction working with CodeIgniter in phpStorm.
The fundamentals are pretty much the same when working with any project in phpStorm.

Once you become use to it, its easy.
Proper phpDocs and annotations also better explain what your code is doing when others look at it.

Good luck and until next time Happy Coding

  • jonatanfroes

    does it work w/ HMVC?

    • Jeff Behnke

      It will, but it will most likely need to be adjusted for it depending on how the HMVC is setup.

      So its going to depend on the HMVC.
      I worked with a couple CI CMS’s about 6 months ago that were HMVC and was able to get most code completion working.
      But each was different to setup.

  • leschirmeur

    Hi Jeff!

    Great stuff, thanks very much.

    However, I am still getting warning messages for code like
    “$query->row_array()” where phpstorm does not regonize the row_array() method.

    Any ideas what I am doing wrong here?

    Thanks very much and kind regards

    • Jeff Behnke

      My comment system is broke at the moment. Comments are all messed up.
      Anyway, assuming your referring to CI, mark your query with an var annotation.

      /** @var $query CI_DB_mysql_result */
      $query = $this->db->query("SELECT * FROM links WHERE user_id = '$user_id' ORDER BY id DESC LIMIT 45");



      will work.
      This is because $this->db uses multiple dynamic instances and we can only assign one at a time.

      In my phpStorm helper file I assigned this->db to CI_DB_active_record
      For the initial methods and properties.
      After that when you assign the var $query to that instance you need to re-annotate it to get the drivers methods, IE

      /** @var $query CI_DB_mysql_result */


      /** @var $query CI_DB_mysql_result */
      $query = $this->db->query("SELECT * FROM links WHERE user_id = '$user_id' ORDER BY id DESC LIMIT 45");
      $rows  = $query->result_array();

      You might get lost trying to reply, like I said my comment system is a mess.
      Should have it fixed tomorrow though.

      Hope the above helps. It should.

  • Pingback: Panda()

  • Илья

    Wow. It’s great.

  • CallMeWhy

    So GOOD! TKS~