PHPStorm - Improve autocompletion for Magento

PHPStorm - Improve autocompletion for Magento

PHPStorm knows a lot, but unfortunately it does not know the objects that Magento 1 factory methods return. This affects the autocomplete and go to declaration on a lot of methods but, with a small shell script, we can teach PHPStorm the proper class map. The shell script is located in the following GitHub repository: Vinai/phpstorm-magento-mapper

This article features a step by step guide on how to install and use the script to generate the class map and,in the second part, it will present ways to improve autocompletion using the @var PHPDoc comment syntax.

I know there is a PHPStorm plugin called Magicento that will do this and a lot more. But if you don’t feel like spending money on it, this is a quick and easy solution.

Supported Factory Methods

Mage::getModel()
Mage::getSingleton()
Mage::getResourceModel()
Mage::getResourceSingleton()
Mage::getBlockSingleton()
Mage::helper()
Mage_Core_Model_Factory::getModel()
Mage_Core_Model_Factory::getSingleton()
Mage_Core_Model_Factory::getResourceModel()
Mage_Core_Model_Factory::getHelper()
Mage_Core_Block_Abstract::helper()
Mage_Core_Model_Layout::createBlock()
Mage_Core_Model_Layout::getBlockSingleton()
Mage_Core_Block_Abstract::getHelper()

This script respects class rewrites. This means that if you rewrite the Mage_Catalog_Model_Product with Evozon_Extra_Model_Product for example, in the map the catalog/product class will point to Evozon_Extra_Model_Product.

Step by step guide

Step 1: Copy the shell folder from Vinai/phpstorm-magento-mapper GitHub repository into your Magento root folder.

Step 2: Execute the following command into your Magento root folder:

> php shell/generate-phpstorm-map.php --file .phpstorm.meta.php

If you are on Windows and you did not add the PHP executable into the PATH, you will need to run the command with the full path to your php.exe file like this:

> cd C:\projects\magento
> C:\php\php.exe shell/generate-phpstorm-map.php --file .phpstorm.meta.php

You are done. Now the go to declaration and methods autocomplete should work. If you still have problems, try File > “Invalidate Caches / Restart…”

If you want to keep the map up to date, you will need to rerun the script every time you add a class or configure a rewrite.

Also, this file can be committed in Git. This way, all colleagues that use PHPStorm will have the autocomplete working. If you have any Git conflicts on this, just accept any changes and run the shell script again. 

Special thanks to Vinai Kopp who developed this shell script.

Other ways to improve autocomplete or go to declaration

You need to know that some code can cause problems on autocomplete or go to declaration. I’ll list some examples of code that can cause problems, but also offer a solution to fix them.

1. Add @var PHPDoc comment into template files (phtml)

In template files (phtml), if you want to know what methods $this object has, you can add the @var PHPDoc comment to point PHPStorm to the correct class. This way, you will be able to use methods autocomplete inside your template files.

/** @var Mage_Catalog_Block_Product_List $this */

NetBeans recognizes this with the following /* @var $varName varType */ syntax (note that there's just one * at the beginning of the comment). If you have teammates that use NetBeans, use this format instead.

add_var_phpdoc_into_template.png

If you know that a template is used by two or multiple blocks, you can add them into @var PHPDoc comment like this:

/** @var Mage_Review_Block_Product_View|Mage_Review_Block_Product_View $this */

Please add this into your edited files (excluding the core/base template that you should not edit). It will help you and other developers in the future.

2. Collection used in foreach loop

If you use a collection object in a foreach loop, you will see that objects inside it have problems with autocomplete or go to declaration. To fix this, you can add a @var PHPDoc comment like this:

/** @var Mage_Catalog_Model_Resource_Product_Collection|Mage_Catalog_Model_Product[] $itemCollection */

The same syntax can be used in @return or @param PHPDoc comments.

Collection_used_in_foreach.png

3. Autocomplete on a method chain

$productFinalPrice = Mage::getModel('catalog/product')->load(1)->getFinalPrice();

Trying to go to getFinalPrice() method is not going to work.

This is because the load() method that is implemented in the Mage_Core_Model_Abstract class has the @return Mage_Core_Model_Abstract in the PHPDOC comment. If the Magento core team had used @return $this instead, this would have worked.

To fix this, you can reformat the code and use a @var PHPDoc comment to let PHPStorm know the correct class for this.

<?php 
/** @var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->load(1);
$productFinalPrice = $product->getFinalPrice();
?>

method_calls_autocomplate.png

Now the Ctrl+Click on getFinalPrice() method and the autocomplete will work.


NO COMMENTS

Tell us what you think

Fields marked with " * " are mandatory.

We use cookies to offer you the best experience on our website. Learn more

Got it