Better handle error with class that doesn't exist (but will at runtime)? Follow
I'm developing a third-party module that sits within a wider web service. Many of the classes and variables used (in PHP) are provided by the service. I've noticed I can declare a PHP DocBlock at the top of the page listing the variables passed in, and PHPStorm then treats them as existing.
Is there a way to do this for classes?
The classes sit within a namespace that doesn't exist in my project. For example:
Now I try to access className::methodName() and PHPStorm highlights it as a missing class. Unfortunately one of these methods comes up so much that the part on the right by the scroll bar is almost solidly yellow, which makes it useless for finding other issues.
Is there a way to tell PHPStorm that this class will exist at runtime, and not to worry about it? (preferably while still being informed about issues with other classes, etc).
Please sign in to leave a comment.
Probably, it is not the most elegant solution but you may suppress the "Undefined class" warning for the specific statement by adding a comment just before it:
Thanks for the idea, but it's not really practical. The class is used on almost every line of code.
I take it there's not an equivalent to how I can declare the variables passed into the script, so I think I'll have to disable the undefined class check for now.
Thanks for the help.
Well ... you can try this approach: create a file somewhere in the project (it will be used by the IDE only; so you can call it .ide-helper.php or alike) and declare all such "magic" classes there (even with needed methods: just have the body empty -- basically just what the stub files that PhpStorm uses do).
Another approach: why not link a folder with such a class as an External Library/3rd party code ("Settings/Preferences | PHP | Include Path"). Obviously, it will need to be located somewhere on your computer (main point: outside of the project).
Thanks for the suggestions! I'm new to PHPStorm and wasn't aware of the option to include the 3rd party code (though I had hoped something like this would exist). I have the git repository on my machine already as a reference, so this works well for me! Thanks so much :)