Error Identifier: match.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);
class Foo
{
public function doFoo(): void
{
}
public function doBar(int $i): void
{
$a = match ($i) {
1 => $this->doFoo(),
2 => $this->doFoo(),
default => $this->doFoo(),
};
}
}
Why is it reported? #
The result of a match expression is being used (assigned to a variable, passed as an argument, etc.), but all arms of the match return void. A void return type means the method does not return a meaningful value, so the result of this match expression is always void and cannot be meaningfully used.
This typically indicates a logic error – either the match arms should call methods that return values, or the match result should not be used.
How to fix it #
If the match is only used for side effects, use it as a standalone statement without assigning the result:
public function doBar(int $i): void
{
- $a = match ($i) {
+ match ($i) {
1 => $this->doFoo(),
2 => $this->doFoo(),
default => $this->doFoo(),
};
}
Alternatively, if a value is needed, call methods that return meaningful values:
- public function doFoo(): void
+ public function doFoo(): string
{
+ return 'result';
}
How to ignore this error #
You can use the identifier match.void to ignore this error using a comment:
// @phpstan-ignore match.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: match.void
Rules that report this error #
- PHPStan\Rules\Comparison\UsageOfVoidMatchExpressionRule [1]