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.
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
{
}
Public and protected methods and property hooks that can be overridden depend on the projectType configuration parameter. With the default open, classes outside the analysed paths might override them and throw the other exception, so they are reported only when checkTooWideThrowTypesInProtectedAndPublicMethods is enabled, and for methods only when they override a parent method. With closed, PHPStan checks them against all the methods that override them, and reports an exception type only when neither the method nor anything overriding it throws it.
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