diff --git a/src/Php/AstNodeType.php b/src/Php/AstNodeType.php index a8801385..c33204b3 100644 --- a/src/Php/AstNodeType.php +++ b/src/Php/AstNodeType.php @@ -78,4 +78,14 @@ trait AstNodeType { return $expr instanceof Expr\Match_; } + + protected function isConstFetch(NodeAbstract $expr): bool + { + return $expr instanceof Expr\ConstFetch; + } + + protected function isAssignOp(NodeAbstract $expr): bool + { + return $expr instanceof Node\Expr\AssignOp or $expr instanceof Node\Expr\Assign; + } } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 65ff247f..4f2980a0 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3864,9 +3864,7 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isVarExpr($expr->var) and !$this->hasVar($var)) { $this->addLocalVar($var, $this->detectExprType($expr->expr)); } - return 'if (!' . $isset . ') {' . PHP_EOL . - $this->getIndent() . $var . ' = ' . $right . ';' . PHP_EOL . - '}' . PHP_EOL; + return '(' . $isset . '?' . $var . ':(' . $var . ' = ' . $right . '))'; } protected function isReturnStmtInLastLine(array $stmts): bool diff --git a/tests/aot/assign_coalesce_002.phpt b/tests/aot/assign_coalesce_002.phpt new file mode 100644 index 00000000..2b4af19a --- /dev/null +++ b/tests/aot/assign_coalesce_002.phpt @@ -0,0 +1,39 @@ +--TEST-- +assign coalesce 002 +--FILE-- + +--EXPECT-- +string(5) "world" +string(5) "hello" +string(5) "world" +string(5) "hello"