Merge pull request '如果else分支只有注释,那就不渲染这个分支' (#11) from empty-else into master

Reviewed-on: #11
pull/14/head
韩天峰 2 months ago
commit 14f3a5a311
  1. 12
      src/Php/CompilerBase.php
  2. 19
      tests/aot/control_flow/empty-else.phpt

@ -4255,7 +4255,7 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
protected function parseIfChain(array $arms, ?Node\Stmt\Else_ $else, int $index): string
{
if (!isset($arms[$index])) {
if (!$else) {
if (!$else || $this->isEmptyStmtList($else->stmts)) {
return '';
}
return $this->parseBlockStmts($else->stmts);
@ -4273,6 +4273,16 @@ class CompilerBase extends \PhpAot\Core\Translator implements PropertyAccessCont
return $code;
}
protected function isEmptyStmtList(array $stmts): bool
{
foreach ($stmts as $stmt) {
if (!$stmt instanceof Node\Stmt\Nop) {
return false;
}
}
return true;
}
/**
* 逻辑比较的运算,必须返回 bool 类型.
*/

@ -0,0 +1,19 @@
--TEST--
empty else with comments
--FILE--
<?php
function main() {
if (true) {
echo 'success';
} else {
// 1
# 1
/**
* 1
* 2
*/
}
}
?>
--EXPECT--
success
Loading…
Cancel
Save