Autocomplete in Magento projects with Zend Studio or Eclipse PDT

Source code autocomplete is not very easy in Magento projects. But there is a way that allows to fully have these features.

Why is source code autocomplete so complicated to have in Magento projects?

As I mentionned before, there is multiples ways to overload native Magento API. The active overloading mechanism allows you to overload a class with another different class simply by setting up a configuration value. But usage of these overloading mechanims make difficult to provide a PHPDoc which allows autocomplete:

  • which tag @return do I need to used?
  • Which object is returned by the call of Mage::getModel(‘catalog/product’) instruction?

Very difficult for Varien to provide an answer: it depends on your configuration, and so, autocomplete is often very difficult to have.

However, there is a way to have it with Zend Studio or PDT: no more loosing time to search a function signature or a block content, you can have with Zend Studio or PDT the fully autocomplete of your code

All will be done with the @var tag

The @var tag for code source autocomplete

Is the @var tag a PHPDoc tag?

No: This tag is not a PHPDoc compliant tag, but an extension available to complete PHPDoc

Syntax of @var tag

This tag is defined as followed:

/* @var $variable Class_Type */

Be sure to provide a simple * instead of the two required in PHPDoc

Where to put the @var tag?

This tag must be placed after variable definition, on the same line or on the following ones: the editor will be able to match variables with related classes only after it has found this instruction.

So there is no difficulty to do that, just take time to well comment your source code

Usage example of @var tag in Magento source code

Template usage of the @var tag

Without any @var tag in our template, we can’t have any code autocomplete:

If we have the @var tag in top part of the template, now we are able to see all availables methods:

getModel and getSingleton instruction

Loading classes and collection is often boring without autocomplete

$collection = Mage::getModel('catalog/product')->getCollection();

using the @var tag will offer you all availables methods. try this code in a Magento project:

$collection = Mage::getModel('catalog/product')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */

Conclusion on @var tag

This tag allows you only with CTRL+Space shortcut to have every availables methods on objects referenced. I’m sure that you’ll find that this way will allow you to earn time in your projects