From 52b1effffba2f0d9a6a217d81c2fbd1d2e468711 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 16 Mar 2026 19:08:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20exit?= =?UTF-8?q?=20=E8=AF=AD=E5=8F=A5=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 AstNodeType 中新增 isReturnExpr、isBreakExpr 和 isExitExpr 方法 - 修改 CompilerBase 中 switch case 的验证逻辑 - 支持 exit 语句作为 switch case 的结束语句 - 更新错误消息以包含 exit 语句的验证 - 重构条件判断使用新的辅助方法进行类型检查 --- src/Php/AstNodeType.php | 18 ++++++++++++++++++ src/Php/CompilerBase.php | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Php/AstNodeType.php b/src/Php/AstNodeType.php index e7d27598..e4917fa5 100644 --- a/src/Php/AstNodeType.php +++ b/src/Php/AstNodeType.php @@ -111,4 +111,22 @@ trait AstNodeType { return $expr instanceof VariadicPlaceholder; } + + protected function isReturnExpr(NodeAbstract $expr): bool + { + return $expr instanceof Node\Stmt\Return_; + } + + protected function isBreakExpr(NodeAbstract $expr): bool + { + return $expr instanceof Node\Stmt\Break_; + } + + protected function isExitExpr(NodeAbstract $expr): bool + { + if ($expr instanceof Node\Stmt) { + $expr = $expr->expr; + } + return $expr instanceof Node\Expr\Exit_; + } } diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 83cceb91..a9641ff4 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3214,8 +3214,8 @@ class CompilerBase extends \PhpAot\Core\Translator $stmts = $stmts[0]->stmts; } $lastExpr = end($stmts); - if (!($lastExpr instanceof Node\Stmt\Return_ or $lastExpr instanceof Node\Stmt\Break_)) { - $this->fatalError($case, 'switch case must end with return or break, given ' . $lastExpr->getType()); + if (!$this->isReturnExpr($lastExpr) and !$this->isExitExpr($lastExpr) and !$this->isBreakExpr($lastExpr)) { + $this->fatalError($case, 'switch case must end with return or break or exit, ' . $lastExpr->getType() . ' given'); } if ($condList) {