Method 'myStaticMethod' not found in string - how to annotate (or anything else?) calling method to get rid of it? Follow
I have class hierarchy in which there is an abstract class (AbstractObject) with a static method returning an API entry point URI for asking it for objects of concrete type (say Address). This static method is used to enforce setting entry point in concrete child classes. I also has a Service class that uses mentioned Address::class in its method to get an actual entry point that should be requested for the client. When Address::class is passed to a Service's method PhpStorm could not resolve this static method in the (in fact!) string. You can see the concept here . The code works well. But how to get rid of that annoying yellow wavy underline for getEntryPoint() method?
Please sign in to leave a comment.
The /** @var AbstractObject $fqcn */ PHPDoc comment is the most easiest & correct way for now (if you still want some validation/navigation or refactoring/usage support if you decide to rename the method in the future etc)
PhpStorm does not interpret the possible value of string variable/parameter .. so it does not know if class name or some random string will be passed there. Therefore it's better to show a Warning (which can be suppressed) rather than silently miss potential error. Such warning you can bypass by providing correct typehint (like you did) or even suppress for that line (special comment that will tell IDE to ignore that particular warning in that line).
Another possible solution (which is more safer in general but a bit more expensive as it's an additional runtime check which you app/library may simply not need) .. is to use some guard logic that IDE understands -- e.g. method_exists(). For example:
Other than that: https://youtrack.jetbrains.com/issue/WI-37563 I guess.
Thanks, Andriy, for the reply! I was just confused by the fact that annotation should point to an instance of the class being written in annotation. But $fqcn is not an instance! And since it's not an instance but we play with static methods annotation mentioned does its job well. Thanks again for nice method_exist() trick!