website/errors/throws.notThrowable.md
<?php declare(strict_types = 1);
/**
* @throws \DateTimeImmutable
*/
function doFoo(): void
{
throw new \RuntimeException();
}
The @throws PHPDoc tag specifies a type that is not a subtype of Throwable. In PHP, only instances of Throwable (which includes Exception and Error and their subclasses) can be thrown. Declaring a non-throwable type in @throws is incorrect because that type can never actually be thrown.
Change the @throws tag to reference a type that implements Throwable:
/**
- * @throws \DateTimeImmutable
+ * @throws \RuntimeException
*/
function doFoo(): void
{
throw new \RuntimeException();
}
If the function does not throw any exceptions, use @throws void:
/**
- * @throws \DateTimeImmutable
+ * @throws void
*/
function doFoo(): void
{
}