如果else分支只有注释,那就不渲染这个分支 #11

Merged
韩天峰 merged 2 commits from empty-else into master 2 months ago
  1. 12
      src/Php/CompilerBase.php
  2. 19
      tests/aot/control_flow/empty-else.phpt

@ -4203,7 +4203,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);
@ -4221,6 +4221,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