Error Identifier: staticMethod.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 static function log(string $message): bool
{
return file_put_contents('log.txt', $message) !== false;
}
}
(void) Logger::log('Hello');
Why is it reported? #
The static method call is wrapped in a (void) cast, but the method does not require its return value to be used. The (void) cast is meant for explicitly discarding the return value of methods marked with #[\NoDiscard] to suppress the staticMethod.resultDiscarded error. When a method already allows its return value to be discarded, using (void) is unnecessary.
How to fix it #
Remove the (void) cast and call the method directly:
<?php declare(strict_types = 1);
-(void) Logger::log('Hello');
+Logger::log('Hello');
If the return value is needed, assign it to a variable:
<?php declare(strict_types = 1);
-(void) Logger::log('Hello');
+$success = Logger::log('Hello');
How to ignore this error #
You can use the identifier staticMethod.inVoidCast to ignore this error using a comment:
// @phpstan-ignore staticMethod.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: staticMethod.inVoidCast
Rules that report this error #
- PHPStan\Rules\Methods\CallToStaticMethodStatementWithNoDiscardRule [1]