From 14f862e5ed0ea3ace887bddcdcdba0e3f3b139cf Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 12 Jan 2026 20:20:10 +0100 Subject: [PATCH] Support (void)-casts See https://wiki.php.net/rfc/marking_return_value_as_important --- src/main/php/lang/ast/emit/PHP.class.php | 2 +- src/main/php/lang/ast/emit/PHP74.class.php | 1 + src/main/php/lang/ast/emit/PHP80.class.php | 1 + src/main/php/lang/ast/emit/PHP81.class.php | 1 + src/main/php/lang/ast/emit/PHP82.class.php | 1 + src/main/php/lang/ast/emit/PHP83.class.php | 9 ++++++++- src/main/php/lang/ast/emit/PHP84.class.php | 2 +- .../php/lang/ast/emit/RemoveVoidCasts.class.php | 17 +++++++++++++++++ .../ast/unittest/emit/CastingTest.class.php | 12 ++++++++++++ 9 files changed, 43 insertions(+), 3 deletions(-) create mode 100755 src/main/php/lang/ast/emit/RemoveVoidCasts.class.php diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php index abb0e119..4d413d72 100755 --- a/src/main/php/lang/ast/emit/PHP.class.php +++ b/src/main/php/lang/ast/emit/PHP.class.php @@ -259,7 +259,7 @@ protected function emitExpression($result, $expression) { } protected function emitCast($result, $cast) { - static $native= ['string' => true, 'int' => true, 'float' => true, 'bool' => true, 'array' => true, 'object' => true]; + static $native= ['string' => true, 'int' => true, 'float' => true, 'bool' => true, 'array' => true, 'object' => true, 'void' => true]; // Inline nullable checks using ternaries if ($cast->type instanceof IsNullable) { diff --git a/src/main/php/lang/ast/emit/PHP74.class.php b/src/main/php/lang/ast/emit/PHP74.class.php index b36e2b49..18c1a2ce 100755 --- a/src/main/php/lang/ast/emit/PHP74.class.php +++ b/src/main/php/lang/ast/emit/PHP74.class.php @@ -21,6 +21,7 @@ class PHP74 extends PHP { OmitArgumentNames, OmitConstantTypes, ReadonlyClasses, + RemoveVoidCasts, RewriteBlockLambdaExpressions, RewriteCloneWith, RewriteEnums, diff --git a/src/main/php/lang/ast/emit/PHP80.class.php b/src/main/php/lang/ast/emit/PHP80.class.php index 13f90ef7..f7985e48 100755 --- a/src/main/php/lang/ast/emit/PHP80.class.php +++ b/src/main/php/lang/ast/emit/PHP80.class.php @@ -24,6 +24,7 @@ class PHP80 extends PHP { EmulatePipelines, OmitConstantTypes, ReadonlyClasses, + RemoveVoidCasts, RewriteBlockLambdaExpressions, RewriteCloneWith, RewriteDynamicClassConstants, diff --git a/src/main/php/lang/ast/emit/PHP81.class.php b/src/main/php/lang/ast/emit/PHP81.class.php index 36fc0b49..b77415be 100755 --- a/src/main/php/lang/ast/emit/PHP81.class.php +++ b/src/main/php/lang/ast/emit/PHP81.class.php @@ -21,6 +21,7 @@ class PHP81 extends PHP { use EmulatePipelines, + RemoveVoidCasts, RewriteBlockLambdaExpressions, RewriteCallableClone, RewriteCloneWith, diff --git a/src/main/php/lang/ast/emit/PHP82.class.php b/src/main/php/lang/ast/emit/PHP82.class.php index 7e6df802..d963bf4a 100755 --- a/src/main/php/lang/ast/emit/PHP82.class.php +++ b/src/main/php/lang/ast/emit/PHP82.class.php @@ -21,6 +21,7 @@ class PHP82 extends PHP { use EmulatePipelines, + RemoveVoidCasts, RewriteBlockLambdaExpressions, RewriteCallableClone, RewriteCloneWith, diff --git a/src/main/php/lang/ast/emit/PHP83.class.php b/src/main/php/lang/ast/emit/PHP83.class.php index 4f62d440..0c3d29e6 100755 --- a/src/main/php/lang/ast/emit/PHP83.class.php +++ b/src/main/php/lang/ast/emit/PHP83.class.php @@ -19,7 +19,14 @@ * @see https://wiki.php.net/rfc#php_83 */ class PHP83 extends PHP { - use EmulatePipelines, RewriteCallableClone, RewriteCloneWith, RewriteBlockLambdaExpressions, RewriteProperties; + use + EmulatePipelines, + RemoveVoidCasts, + RewriteCallableClone, + RewriteCloneWith, + RewriteBlockLambdaExpressions, + RewriteProperties + ; public $targetVersion= 80300; diff --git a/src/main/php/lang/ast/emit/PHP84.class.php b/src/main/php/lang/ast/emit/PHP84.class.php index 2673be75..58a341eb 100755 --- a/src/main/php/lang/ast/emit/PHP84.class.php +++ b/src/main/php/lang/ast/emit/PHP84.class.php @@ -19,7 +19,7 @@ * @see https://wiki.php.net/rfc#php_84 */ class PHP84 extends PHP { - use EmulatePipelines, RewriteCallableClone, RewriteCloneWith, RewriteBlockLambdaExpressions; + use EmulatePipelines, RemoveVoidCasts, RewriteCallableClone, RewriteCloneWith, RewriteBlockLambdaExpressions; public $targetVersion= 80400; diff --git a/src/main/php/lang/ast/emit/RemoveVoidCasts.class.php b/src/main/php/lang/ast/emit/RemoveVoidCasts.class.php new file mode 100755 index 00000000..9aa8e950 --- /dev/null +++ b/src/main/php/lang/ast/emit/RemoveVoidCasts.class.php @@ -0,0 +1,17 @@ +type->name()) { + $this->emitOne($result, $cast->expression); + } else { + parent::emitCast($result, $cast); + } + } +} \ No newline at end of file diff --git a/src/test/php/lang/ast/unittest/emit/CastingTest.class.php b/src/test/php/lang/ast/unittest/emit/CastingTest.class.php index 45d0b585..36e5d763 100755 --- a/src/test/php/lang/ast/unittest/emit/CastingTest.class.php +++ b/src/test/php/lang/ast/unittest/emit/CastingTest.class.php @@ -161,4 +161,16 @@ public function run($value) { $value )); } + + #[Test, Values(['null', '$this', 'strlen("Test")'])] + public function void_cast($expr) { + Assert::true($this->run( + 'class %T { + public function run() { + (void)'.$expr.'; + return true; + } + }' + )); + } } \ No newline at end of file