Magento 2
Typehinting Variables
Free Preview
🤓 Typehints add IntelliSense to your code
/**
 * By typehinting variables, IntelliSense can help look up methods on a created
 * object. In this example, the typehinted object would be the object class
 * created from the factory.
 */

/** @var \Foo\Bar\Model\ResourceModel\Widget\Collection $widgetCollection */
$widgetCollection = $this->widgetCollectionFactory->create()
    ->addAttributeToFilter(Widget::ID, $this->getData('widget_id'));

// ->count is found with IntelliSense
if ($widgetCollection->count()) {
    $this->setData('content', $widgetCollection->getFirstItem());
}
🙄 Lack of typehints cause unintelligible method lookups
/**
 * When creating specific objects without typehinting your varialbes, the
 * IDE's IntelliSense will not be unable to look up related methods. This
 * makes debugging more difficult than it needs to be.
 */

$widgetCollection = $this->widgetCollectionFactory->create()
    ->addAttributeToFilter(Widget::ID, $this->getData('widget_id'));

// ->count shows "Method 'count' not found" within IDE
if ($widgetCollection->count()) {
    $this->setData('content', $widgetCollection->getFirstItem());
}
Want to learn more?

Visit M.academy to learn much more about Magento, Laravel, PHP, Javascript, & Docker.

M.academy logo