Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP74.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PHP74 extends PHP {
OmitArgumentNames,
OmitConstantTypes,
ReadonlyClasses,
RemoveVoidCasts,
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteEnums,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP80.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PHP80 extends PHP {
EmulatePipelines,
OmitConstantTypes,
ReadonlyClasses,
RemoveVoidCasts,
RewriteBlockLambdaExpressions,
RewriteCloneWith,
RewriteDynamicClassConstants,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP81.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class PHP81 extends PHP {
use
EmulatePipelines,
RemoveVoidCasts,
RewriteBlockLambdaExpressions,
RewriteCallableClone,
RewriteCloneWith,
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/PHP82.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class PHP82 extends PHP {
use
EmulatePipelines,
RemoveVoidCasts,
RewriteBlockLambdaExpressions,
RewriteCallableClone,
RewriteCloneWith,
Expand Down
9 changes: 8 additions & 1 deletion src/main/php/lang/ast/emit/PHP83.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP84.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions src/main/php/lang/ast/emit/RemoveVoidCasts.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace lang\ast\emit;

/**
* Removes (void) casts for any PHP version < 8.5
*
* @see https://wiki.php.net/rfc/marking_return_value_as_important
*/
trait RemoveVoidCasts {

protected function emitCast($result, $cast) {
if ('void' === $cast->type->name()) {
$this->emitOne($result, $cast->expression);
} else {
parent::emitCast($result, $cast);
}
}
}
12 changes: 12 additions & 0 deletions src/test/php/lang/ast/unittest/emit/CastingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}'
));
}
}
Loading