How to Deal with "Method Not Found in Class" Warning for Magically Implemented Methods

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

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.

Sample Image

Get rid of magic method warning

You can turn off warnings for magically accessing properties in the following way:

  1. Go to File -> Settings -> Editor -> Inspections
  2. Go to PHP -> Undefined -> Undefined field
  3. Uncheck the box for "Notify about access to a field via magic method"

I am not entirely sure if this will also hide the warning in your case, for accessing private members. If not, you could also use the @property PHPDoc tag to describe which fields are accessible through your magic __get method.

Check this page for more information

Method not found in class

PhpStorm cannot figure out what type your $this->cxn field is. You can help by providing typehint via simple PHPDoc comment:

/** @var Database */
private $cxn; //database object

Is there a way to indicate that a class has magic methods defined for every method on another class?

The proper solution is to use supported @method PHPDoc tags. This way it will also work in other editors/IDEs that support PHPDoc and understand such standard tag.

This approach requires every method to be listed separately. More on this in another StackOverflow question/answer: https://stackoverflow.com/a/15634488/783119.


In current PhpStorm versions you may use not-in-PHPDoc-specs (and therefore possibly PhpStorm-specific) @mixin tag.

Adding @mixing className in PHPDoc comment for your target class should do the job for you.

/**
* Class B
*
* @mixin A
*/
class B
{

Basically, @mixin tag does what actual PHP's traits do.

Please note that there is no guarantee that support for such tag will not be removed at some point in the future, although it's pretty unlikely.

Missing method implementations in PHP project

PHP has a few magic methods, and one of them is __call().

When you have an object that implements __call() (by itself or by one of the parent classes), you may call an inaccessible method on it, and the __call() method will be called instead. This happens, for example, when you call a private method from the outside, or when you call a method that was not defined in code.

When you use such calls to inaccessible methods, IDEs will most likely show a warning that the method does not exist, although the code itself will probably work at runtime. These warnings are quite annoying, so you can add a @method tag to your class, and the IDE will know that this method exists, and will not show a warning.

So, to support the code that you got from someone, take a look at the __call() method implementation. Be aware that this method may be implemented in one of the parent classes, so check them out as well.

Method not found on runtime

"Method not found" is a very specific error, which means a method it expected (i.e. was there at compile time) simply is not present. This usually means that the files you are deploying are different to what you think they are - specifically, I would wager that you are deploying the old version of the library (which lacks your additions).

Check the dlls deployed to the web-server against what you think they should be.



Related Topics



Leave a reply



Submit