How to fix Method Not Found In Class orange warning in PHPStorm

little-miss-helpfulGrrr, how annoying.

My screen is full of orange, but I know these methods are valid.

Your project may be vast in scope, you may have a looming deadline for 5pm tonight, or you may just be a perfectionist who hates the thought of your IDE thinking you are making mistakes.

A good example of this is when using a foreach loop.

You have something like:

foreach( $myEntity as $entity ) {
  $entity->myValidMethod();
}

And PHPStorm says, erm, you make big mistake sir!

Now you have a valid bit of code that’s flagged up in orange as Method ‘myValidMethod’ not found in class My\Name\Space\Path\MyEntity.

Boo, hiss, and such.

It’s simple to fix though – even though it’s taken me over 100 words to get here.

Just add a comment, as such:

/* @var $entity \My\Name\Space\Path\MyEntity */
foreach( $myEntity as $entity ) {
  $entity->myValidMethod();
}

Super.

Now you no longer have a false positive.

Time to get back to meeting your deadline.

Be Sociable, Share!

5 thoughts on “How to fix Method Not Found In Class orange warning in PHPStorm”

  1. Great tip! Was having problems doing refactoring and “Find Usages” in PHPStorm and it wouldn’t include objects that weren’t explicitly constructed. All objects in my Slim routes that used DI and in all foreach statements were unreachable. Thanks for helping me prevent future errors in my code!

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.