Error Identifier: function.resultDiscarded
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);
#[\NoDiscard]
function computeHash(string $input): string
{
return hash('sha256', $input);
}
computeHash('test');
Why is it reported? #
The function is marked with the #[\NoDiscard] attribute, indicating that its return value should always be used. Calling such a function on a separate line without using the return value means the computation is wasted and likely indicates a programming error.
In the example above, computeHash() returns a hash string, but the result is not assigned to a variable or used in any way.
How to fix it #
Use the return value of the function call:
<?php declare(strict_types = 1);
-computeHash('test');
+$hash = computeHash('test');
If the return value is intentionally not needed, use a (void) cast to explicitly discard it:
<?php declare(strict_types = 1);
-(computeHash('test'));
+(void) (computeHash('test'));
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Functions\CallToFunctionStatementWithNoDiscardRule [1]