Error Identifier: throws.notCovariantWithImplicitVoid
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
{
public function process(): void
{
}
}
class ChildClass extends ParentClass
{
/**
* @throws \RuntimeException
*/
public function process(): void
{
throw new \RuntimeException('Error.');
}
}
Why is it reported? #
A child method declares a @throws type, but the parent method does not have a @throws PHPDoc tag. When the exceptions.implicitThrows config parameter is set to false, a missing @throws tag is treated the same as @throws void, meaning the method promises not to throw any exceptions. A child method that declares exceptions in its @throws tag violates this contract.
This rule enforces that @throws types follow the same covariance rules as other type declarations: a child method cannot throw more exception types than its parent allows.
How to fix it #
Remove the @throws tag from the child method if the parent does not allow throwing exceptions:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
- /**
- * @throws \RuntimeException
- */
public function process(): void
{
- throw new \RuntimeException('Error.');
}
}
Or add a @throws tag to the parent method to explicitly allow exceptions:
<?php declare(strict_types = 1);
class ParentClass
{
+ /**
+ * @throws \RuntimeException
+ */
public function process(): void
{
}
}
How to ignore this error #
You can use the identifier throws.notCovariantWithImplicitVoid to ignore this error using a comment:
// @phpstan-ignore throws.notCovariantWithImplicitVoid
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.notCovariantWithImplicitVoid
Rules that report this error #
- PHPStan\Rules\Exceptions\MethodThrowTypeCovarianceRule [1]