Avoid warnings for dynamically generated class? Follow
I'm working with a code base where exceptions are occasionally generated dynamically.
Unfortunately this leads to the situation where an exception is marked as not existing, although it will exist during runtime. Is there any way to let PhpStorm know that this class exists, short of defining it? Ideally some PHPDoc extension or something would be great.
Basically the code looks like this (the warning is on the line containing the throw, since PhpStorm can't figure out that this exception is actually dynamically created on the first line):
def_exception('AmountInvalidException');
// only allow numbers and dots
if (preg_match('#[^0-9\.]#', $amount))
{
throw new AmountInvalidException($this->t['amountinvalid']);
}
Please sign in to leave a comment.
Hi Christoffer,
Yes, it is possible. All standard (supported/known to PhpStorm) classes/functions/etc are done this way: you need what is called a stub file.
Just create a php file, define your class there (just like normall class but with empty bodies for functions) and then place it anywhere in your project (or attach frolder with it as Extermal Library (Settings | PHP --> include paths).