Error Identifier: throws.void
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
class MyException extends \Exception
{
}
/**
* @throws void
*/
function doFoo(): void
{
throw new MyException();
}
Why is it reported? #
The function or method has a @throws void PHPDoc tag, which declares that it does not throw any exceptions. However, PHPStan detected an explicit throw statement (or a call to a function that is known to throw) inside the function body. This is a contradiction – the code promises not to throw but actually does.
How to fix it #
If the function can throw exceptions, update the @throws tag to declare the thrown type:
/**
- * @throws void
+ * @throws MyException
*/
function doFoo(): void
{
throw new MyException();
}
If the function should not throw, remove the throw statement:
/**
* @throws void
*/
function doFoo(): void
{
- throw new MyException();
+ // handle the error differently
}
If the @throws void tag was added by mistake, remove it:
-/**
- * @throws void
- */
function doFoo(): void
{
throw new MyException();
}
How to ignore this error #
You can use the identifier throws.void to ignore this error using a comment:
// @phpstan-ignore throws.void
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: throws.void