Error Identifier: return.void
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 doFoo(): void
{
return 'hello';
}
Why is it reported? #
A function or method with a void return type declaration attempts to return a value. In PHP, functions declared with void must not return any value. A bare return; statement is allowed, but return $value; is not.
This is a violation of PHP’s type system and will cause a fatal error at runtime in strict mode.
How to fix it #
Either remove the returned value:
function doFoo(): void
{
- return 'hello';
+ // perform side effects only
}
Or change the return type to match the returned value:
-function doFoo(): void
+function doFoo(): string
{
return 'hello';
}
How to ignore this error #
You can use the identifier return.void to ignore this error using a comment:
// @phpstan-ignore return.void
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.void