Error Identifier: throws.unusedType
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 \InvalidArgumentException|\DomainException
*/
function doFoo(): void
{
throw new \InvalidArgumentException();
}
Why is it reported? #
The @throws PHPDoc tag declares an exception type that is never actually thrown within the function or method body. In the example above, DomainException is listed in @throws but only InvalidArgumentException is thrown.
Declaring unused throw types makes the @throws documentation inaccurate. Callers may add unnecessary catch blocks for exceptions that can never occur.
For methods, this rule is controlled by the checkTooWideThrowTypesInProtectedAndPublicMethods configuration option.
How to fix it #
Remove the unused exception type from the @throws tag:
/**
- * @throws \InvalidArgumentException|\DomainException
+ * @throws \InvalidArgumentException
*/
function doFoo(): void
{
throw new \InvalidArgumentException();
}
If the function does not throw any exceptions at all, use @throws void:
/**
- * @throws \DomainException
+ * @throws void
*/
function doBar(): void
{
}
How to ignore this error #
You can use the identifier throws.unusedType to ignore this error using a comment:
// @phpstan-ignore throws.unusedType
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.unusedType