Error Identifier: ternary.resultUnused
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(bool $a, string $b, string $s): void
{
$a ? $b : $s;
}
Why is it reported? #
A ternary expression is used as a standalone statement, but its result is not assigned to a variable, returned, or passed as an argument. The expression evaluates to a value that is immediately discarded, which means it has no effect on the program.
This usually indicates one of the following:
- The result was meant to be assigned to a variable or returned.
- The intent was to use an
if/elsestatement for side effects instead.
How to fix it #
Assign the result to a variable or return it:
-$a ? $b : $s;
+$result = $a ? $b : $s;
Or use an if/else statement if the branches have side effects:
-$a ? doSomething() : doSomethingElse();
+if ($a) {
+ doSomething();
+} else {
+ doSomethingElse();
+}
How to ignore this error #
You can use the identifier ternary.resultUnused to ignore this error using a comment:
// @phpstan-ignore ternary.resultUnused
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: ternary.resultUnused
Rules that report this error #
- PHPStan\Rules\DeadCode\NoopRule [1]