Error Identifier: return.missing
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 getGreeting(string $name): string
{
$greeting = 'Hello, ' . $name;
}
Why is it reported? #
A function or method declares a non-void return type but does not always return a value. Every code path must end with a return statement when the function has a return type. Falling off the end of the function without returning a value causes PHP to return null, which violates the declared return type.
When the function has a native return type, this error is not ignorable because PHP will throw a TypeError at runtime.
How to fix it #
Add the missing return statement:
function getGreeting(string $name): string
{
$greeting = 'Hello, ' . $name;
+ return $greeting;
}
Or change the return type if the function is not meant to return a value:
-function getGreeting(string $name): string
+function getGreeting(string $name): void
{
$greeting = 'Hello, ' . $name;
+ echo $greeting;
}
How to ignore this error #
You can use the identifier return.missing to ignore this error using a comment:
// @phpstan-ignore return.missing
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.missing