How to deal with "method not found in class" warning for magically implemented methods?

PhpPhpstorm

Php Problem Overview


I am sitting on a large codebase that contains several classes that expose functionality through magically implemented methods (using __call and __callStatic). For example:

class Foo {
    public function __call($name, $parameters) {
        echo "You called $name().\n";
    }
}

$f = new Foo;
$f->test();   // runs fine, but PhpStorm flags as a warning

The problem is that PhpStorm thinks that the test() method does not exist, so it gives a warning at the call site. This is a little annoying, as of course the code will run and behave as expected.

I have already tuned down the severity by checking the "downgrade severity if __magic methods are present in class" option, but I would prefer to either:

  1. completely disable this functionality for specific classes only, or
  2. work with the IDE rather than against it -- provide it with the information I already have so our views agree

Is any of the above possible? If so, how?

Additional bonus question: consider the case where method calls are being chained.

$f = new Foo;
$f->test()->chain()->moreChain();   // potentially runs fine

Assuming that the magic call to $f->test() returns something appropriate the subsequent (possibly, but not necessarily, also magic) calls will work fine. However, since there is no way that I know of to tell the IDE what test() returns it flags the rest of the call chain as full of missing methods too. And to make matters worse, the "downgrade severity" setting does not apply to these warnings since the IDE does not know what class these intermediate objects are supposed to be.

Is there a solution that can also cover this case?

Update

Even though documenting the magic methods with @method annotations seems to work, I have to assume that there are currently several problems with this approach because it only took me a little work to come upon these related bugs:

I do hope they fix them in a reasonable time frame.

Php Solutions


Solution 1 - Php

Well, you can go to the preference menu, under Inspections, go to Undefined -> Undefined Method and check Downgrade severity if __magic methods are present.

That would make the flag less severe, (instead of Warning, as Info), which would still give you a green light on your document check.

There's nothing else I'm aware of aside from having @property or @method PHPDoc notations on the target class for every method that's likely to be used.

enter image description here

Solution 2 - Php

Rather than globally turning off inspections by downgrading the severity of the inspection, you can add a comment to the single line to ignore just that particular inspection.

    /** @noinspection PhpUndefinedMethodInspection */
    Assertion::NullOrAssertionDoesNotExist();

Solution 3 - Php

Building on what Madara said, I found it wouldn't downgrade down far enough for my liking no matter what severity I set it to, so I made a new Severity for undefined method that has no attributes whatsoever, and turned off the downgrade checkbox (see image below)

enter image description here

If I hover over my magic methods now, it still brings up the popup message, but otherwise it doesn't distract me. One step better than just turning off inspection, because at least this way an actual undefined method can still be detected by hovering over the name

Solution 4 - Php

You can use a dynamic variable :

$test = 'test';
$chaine = $f->$test();   // no phpStorm flag, & the code works

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonView Question on Stackoverflow
Solution 1 - PhpMadara's GhostView Answer on Stackoverflow
Solution 2 - PhpRichard A QuadlingView Answer on Stackoverflow
Solution 3 - PhpdaronjayView Answer on Stackoverflow
Solution 4 - PhpQassoulView Answer on Stackoverflow