feat(php): 添加对 throw 语句的支持并改进 switch case 验证

- 在 AstNodeType 中添加 isThrowExpr 方法用于检测 throw 表达式
- 修改 CompilerBase 中的 switch case 验证逻辑,支持以 throw 结尾的 case
- 更新 parseThrow 方法以正确处理不同类型的异常对象
- 将最低 PHP 版本兼容性要求从 8.0 更新到 8.4
pull/1/head
韩天峰 5 months ago
parent fa2a1d286f
commit 7eed77138a
  1. 2
      bin/gen_stub.php
  2. 8
      src/Php/AstNodeType.php
  3. 15
      src/Php/CompilerBase.php

@ -4252,7 +4252,7 @@ class FileInfo {
public bool $generateClassEntries = true;
private bool $isUndocumentable = false;
private bool $legacyArginfoGeneration = false;
private ?int $minimumPhpVersionIdCompatibility = PHP_80_VERSION_ID;
private ?int $minimumPhpVersionIdCompatibility = PHP_84_VERSION_ID;
/** @param array<int, DocCommentTag> $fileTags */
public function __construct(array $fileTags) {

@ -122,6 +122,14 @@ trait AstNodeType
return $expr instanceof Node\Stmt\Break_;
}
protected function isThrowExpr(NodeAbstract $expr): bool
{
if ($expr instanceof Node\Stmt\Expression) {
$expr = $expr->expr;
}
return $expr instanceof Expr\Throw_;
}
protected function isExitExpr(NodeAbstract $expr): bool
{
if ($expr instanceof Node\Stmt\Expression) {

@ -3351,8 +3351,12 @@ class CompilerBase extends \PhpAot\Core\Translator
$stmts = $stmts[0]->stmts;
}
$lastExpr = end($stmts);
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 (!$this->isReturnExpr($lastExpr)
and !$this->isExitExpr($lastExpr)
and !$this->isBreakExpr($lastExpr)
and !$this->isThrowExpr($lastExpr)
) {
$this->fatalError($case, 'switch case must end with return/break/exit/throw, ' . $lastExpr->getType() . ' given');
}
if ($condList) {
@ -3959,10 +3963,11 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseThrow(mixed $expr): string
{
if (!$this->isVarExpr($expr->expr) and $expr->expr->getType() != self::EXPR_NEW) {
$this->fatalError($expr, 'The throw statement only accepts a object variable');
$ex = $this->convertToObject($expr->expr);
} else {
$ex = $this->parseIdentifier($expr->expr);
}
return 'php::throwException(' . $this->parseIdentifier($expr->expr) . ')';
return 'php::throwException(' . $ex . ')';
}
protected function parseTryCatch(mixed $v): string

Loading…
Cancel
Save