Error Identifier: missingType.checkedException
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 OrderService
{
/** @throws \RuntimeException */
public function placeOrder(): void
{
throw new \InvalidArgumentException('Invalid order data');
}
}
Why is it reported? #
A function or method throws a checked exception that is not listed in its @throws PHPDoc tag. When checked exceptions are configured, PHPStan enforces that all thrown exception types are declared in the @throws tag.
In the example above, the method declares @throws \RuntimeException but throws \InvalidArgumentException, which extends \LogicException, not \RuntimeException. The thrown exception type is not covered by the declared @throws type.
This rule only applies when checked exceptions are configured in the PHPStan configuration.
How to fix it #
Add the missing exception type to the @throws tag:
<?php declare(strict_types = 1);
class OrderService
{
- /** @throws \RuntimeException */
+ /** @throws \RuntimeException|\InvalidArgumentException */
public function placeOrder(): void
{
throw new \InvalidArgumentException('Invalid order data');
}
}
Or use a common parent exception type that covers both:
<?php declare(strict_types = 1);
class OrderService
{
- /** @throws \RuntimeException */
+ /** @throws \Exception */
public function placeOrder(): void
{
throw new \InvalidArgumentException('Invalid order data');
}
}
Or catch the exception inside the method so it does not propagate:
<?php declare(strict_types = 1);
class OrderService
{
/** @throws \RuntimeException */
public function placeOrder(): void
{
+ try {
throw new \InvalidArgumentException('Invalid order data');
+ } catch (\InvalidArgumentException $e) {
+ throw new \RuntimeException('Order failed', 0, $e);
+ }
}
}
How to ignore this error #
You can use the identifier missingType.checkedException to ignore this error using a comment:
// @phpstan-ignore missingType.checkedException
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: missingType.checkedException