Error Identifier: cast.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);
$a = (void)(1 + 1);
Why is it reported? #
The (void) cast (PHP 8.5+) can only be used as a standalone statement to explicitly discard a return value. It cannot be used within an expression because (void) does not produce a value – assigning it, passing it as an argument, or nesting it inside another expression is not valid.
How to fix it #
Use the (void) cast as a standalone statement instead of within an expression:
<?php declare(strict_types = 1);
-$a = (void)(1 + 1);
+(void)(1 + 1);
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\Cast\VoidCastRule [1]