Error Identifier: return.never
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);
function terminate(): never
{
return;
}
Why is it reported? #
The function or method has a never return type, which means it must not return at all. A function declared as never is expected to always throw an exception, call exit()/die(), or enter an infinite loop. Having a return statement in such a function contradicts the never return type declaration.
How to fix it #
Either remove the return statement and ensure the function never returns:
<?php declare(strict_types = 1);
function terminate(): never
{
- return;
+ exit(1);
}
Or change the return type if the function is meant to return:
<?php declare(strict_types = 1);
-function terminate(): never
+function terminate(): void
{
return;
}
How to ignore this error #
You can use the identifier return.never to ignore this error using a comment:
// @phpstan-ignore return.never
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: return.never