Avoid warnings for dynamically generated class?

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']);
 }

1 comment
Comment actions Permalink

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).

  • You can see this thread for some additional explanation/example: http://devnet.jetbrains.net/message/5301861
  • Alternatively -- do Ctrl+Click on any standard function/class and you will be taken to one of the bundled with PhpStorm stub files so you can see how it is implemented there.
0

Please sign in to leave a comment.