3

I have grown accustomed to the better PHPDoc coverage of Magento 2, so now it bothers me when the magic getters and setters are highlighted in red in my PhpStorm project files. In classes that I define, I will typically enumerate magic methods using @method in the class PHPDoc. However, I am trying to figure out how to hint them on object instances that I don’t control such as event observers. Consider the following example:

class Example implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* @var \Magento\Cms\Model\Page $page */
        $page = $observer->getPage();
    }
}

I realize I can use $observer->getData('page'), but if I wish to use the magic getter is there a way to prevent PhpStorm from showing the warning:

Method 'getPage' not found in \Magento\Framework\Event\Observer

2
  • Do you know if there any recommendation against using magic getters/setters because of this? Or did you find a way?
    – Qtax
    Sep 27, 2019 at 11:30
  • 1
    @Qtax - Nope just living with it. Since this has no functional impact, I think it’s just up to personal preference. There’s definitely no official recommendation against magic methods. Sep 27, 2019 at 15:55

1 Answer 1

0

I discovered a sort of workaround for this problem. In my IDE settings under Editor > Inspections > PHP > Undefined > Undefined method there is an option called “Downgrade severity if __magic methods are present” which when I turn it on dismisses these warnings. Preference Pane

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.