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_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

[] $itemCollection */

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

PHP Magento syntax

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 Magento method chain

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

 Claudiu Marginean.

Original article

2019-03-18T13:29:34+03:00October 20th, 2016|Categories: PHP|Tags: , , |