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);
/** @return bool */
function alwaysTrue(): bool
{
if (rand(0, 1)) {
return true;
}
return true;
}
Why is it reported? #
The function or method declares bool as its return type, but PHPStan has determined that only one of the two boolean values (true or false) is ever actually returned. The return type is wider than necessary because one of the boolean values is never returned.
In the example above, every code path returns true, so the return type should be narrowed from bool to true.
How to fix it #
Narrow the return type to the specific boolean literal type that is actually returned (PHP 8.2+):
-function alwaysTrue(): bool
+function alwaysTrue(): true
{
if (rand(0, 1)) {
return true;
}
return true;
}
On older PHP versions, the true standalone type is not available natively. Use a PHPDoc @return tag instead:
-/** @return bool */
+/** @return true */
function alwaysTrue(): bool
Learn more: PHPDoc Types
With bleeding edge enabled, running PHPStan with --fix narrows bool to true or false, in the native return type where the PHP version allows it, and in the @return PHPDoc tag otherwise.
Public and protected methods that can be overridden depend on the projectType configuration parameter. With the default open, classes outside the analysed paths might override them and return the other value, so they are not reported unless checkTooWideReturnTypesInProtectedAndPublicMethods is enabled. With closed, PHPStan checks them against all the methods that override them, reports bool only when neither the method nor anything overriding it returns the other value, and the fix keeps the overriding methods compatible.
🔧 This error is auto-fixable. PHPStan can fix it for you. Run the analysis with the --fix option to rewrite the affected code:
vendor/bin/phpstan analyse --fix
To also fix the errors you have ignored, use --fix-ignored. Learn more about --fix »
How to ignore this error #
You can use the identifier return.tooWideBool to ignore this error using a comment:
// @phpstan-ignore return.tooWideBool
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.tooWideBool
Rules that report this error #
- PHPStan\Rules\TooWideTypehints\TooWideArrowFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideClosureReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideFunctionReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodParameterOutTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule [1]
- PHPStan\Rules\TooWideTypehints\TooWidePropertyTypeRule [1]
- PHPStan\Rules\TooWideTypehints\TooWideTypeInOverridableMemberRule [1]