Error Identifier: throw.notThrowable
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);
throw 123;
Why is it reported? #
PHP only allows throwing objects that implement the Throwable interface. Attempting to throw a value of any other type (such as int, string, or an object that does not implement Throwable) will result in a fatal error at runtime.
In the example above, an integer is used in a throw statement, which is not a valid throwable value.
How to fix it #
Throw an instance of a class that implements Throwable, such as Exception or RuntimeException:
-throw 123;
+throw new \RuntimeException('Something went wrong');
If using a custom class, make sure it extends Exception or implements Throwable:
-class MyError
+class MyError extends \RuntimeException
{
}
throw new MyError();
How to ignore this error #
You can use the identifier throw.notThrowable to ignore this error using a comment:
// @phpstan-ignore throw.notThrowable
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: throw.notThrowable
Rules that report this error #
- PHPStan\Rules\Exceptions\ThrowExprTypeRule [1]