Error Identifier: throws.shouldBeVoid
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 ParentClass
{
/**
* @throws void
*/
public function doFoo(): void
{
}
}
class ChildClass extends ParentClass
{
/**
* @throws \RuntimeException
*/
public function doFoo(): void
{
throw new \RuntimeException();
}
}
Why is it reported? #
A child method declares a @throws type, but the parent method explicitly has @throws void in its PHPDoc. The @throws void annotation means the method promises not to throw any exceptions. A child method that throws exceptions violates this contract.
How to fix it #
Remove the exception throwing from the child method:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
- /**
- * @throws \RuntimeException
- */
public function doFoo(): void
{
- throw new \RuntimeException();
}
}
Or remove the @throws void annotation from the parent method if it should allow child methods to throw exceptions.
How to ignore this error #
You can use the identifier throws.shouldBeVoid to ignore this error using a comment:
// @phpstan-ignore throws.shouldBeVoid
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.shouldBeVoid
Rules that report this error #
- PHPStan\Rules\Exceptions\MethodThrowTypeCovarianceRule [1]