Error Identifier: method.inVoidCast
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 Logger
{
public function log(string $message): bool
{
return file_put_contents('log.txt', $message) !== false;
}
}
$logger = new Logger();
(void) $logger->log('hello');
Why is it reported? #
The method call is wrapped in a (void) cast, but the method does not require its return value to be used. The (void) cast is intended for suppressing the “result discarded” error on methods marked with #[\NoDiscard] (or determined to require their return value to be used). When a method already allows discarding its return value, using (void) is unnecessary and misleading.
How to fix it #
Remove the (void) cast and call the method normally:
<?php declare(strict_types = 1);
$logger = new Logger();
-(void) $logger->log('hello');
+$logger->log('hello');
If the return value should be used, assign it to a variable:
<?php declare(strict_types = 1);
$logger = new Logger();
-(void) $logger->log('hello');
+$success = $logger->log('hello');
How to ignore this error #
You can use the identifier method.inVoidCast to ignore this error using a comment:
// @phpstan-ignore method.inVoidCast
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: method.inVoidCast
Rules that report this error #
- PHPStan\Rules\Methods\CallToMethodStatementWithNoDiscardRule [1]