Error Identifier: throws.notThrowable
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);
/**
* @throws \DateTimeImmutable
*/
function doFoo(): void
{
throw new \RuntimeException();
}
Why is it reported? #
The @throws PHPDoc tag specifies a type that is not a subtype of Throwable. In PHP, only instances of Throwable (which includes Exception and Error and their subclasses) can be thrown. Declaring a non-throwable type in @throws is incorrect because that type can never actually be thrown.
How to fix it #
Change the @throws tag to reference a type that implements Throwable:
/**
- * @throws \DateTimeImmutable
+ * @throws \RuntimeException
*/
function doFoo(): void
{
throw new \RuntimeException();
}
If the function does not throw any exceptions, use @throws void:
/**
- * @throws \DateTimeImmutable
+ * @throws void
*/
function doFoo(): void
{
}
How to ignore this error #
You can use the identifier throws.notThrowable to ignore this error using a comment:
// @phpstan-ignore throws.notThrowable
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.notThrowable
Rules that report this error #
- PHPStan\Rules\PhpDoc\InvalidThrowsPhpDocValueRule [1]