Error Identifier: return.type
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(): int
{
return 'hello';
}
Why is it reported? #
The returned value does not match the declared return type. The function declares that it returns int, but the actual returned value is string. This is a type mismatch that will cause a TypeError at runtime in strict mode, or an implicit type coercion in non-strict mode that may lead to data loss or unexpected behavior.
How to fix it #
Return a value that matches the declared return type:
function doFoo(): int
{
- return 'hello';
+ return 42;
}
Or change the return type to match the returned value:
-function doFoo(): int
+function doFoo(): string
{
return 'hello';
}
How to ignore this error #
You can use the identifier return.type to ignore this error using a comment:
// @phpstan-ignore return.type
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.type