refactor(php): 重构PHP编译器中的静态属性赋值解析逻辑

- 将静态属性赋值解析提取为独立的parseAssignStaticProperty方法
- 修改parseAssign方法以使用新的辅助方法进行静态属性检查
- 添加函数定义存在性验证以避免未定义函数的致命错误
- 使用独立的函数名存在性检查替代直接数组访问
- 改进了代码结构和可读性
pull/1/head
韩天峰 7 months ago
parent 2963e0b4ad
commit b57ff37b18
  1. 29
      src/Php/CompilerBase.php

@ -992,18 +992,8 @@ class CompilerBase extends \PhpAot\Core\Translator
return implode(";\n" . $this->getIndent(), $list); return implode(";\n" . $this->getIndent(), $list);
} }
protected function parseAssign(Node\Expr\Assign $v): string protected function parseAssignStaticProperty($left, $right)
{ {
$left = $v->var;
$right = $v->expr;
if ($right->getType() === 'Expr_Assign') {
return $this->parseRightAssociativeAssign($left, $right);
}
if ($left->getType() === self::EXPR_ARRAY_DIM_FETCH) {
return $this->parseAssignArrayDim($left, $right);
}
if ($left->getType() === 'Expr_StaticPropertyFetch') {
$value = $this->trimBrackets($this->parseExpr($right)); $value = $this->trimBrackets($this->parseExpr($right));
$native = $this->parseNativeStaticPropertyFetch($left); $native = $this->parseNativeStaticPropertyFetch($left);
if ($native) { if ($native) {
@ -1014,6 +1004,20 @@ class CompilerBase extends \PhpAot\Core\Translator
return "php::setStaticProperty({$class}, {$propName}, {$value})"; return "php::setStaticProperty({$class}, {$propName}, {$value})";
} }
protected function parseAssign(Node\Expr\Assign $v): string
{
$left = $v->var;
$right = $v->expr;
if ($right->getType() === 'Expr_Assign') {
return $this->parseRightAssociativeAssign($left, $right);
}
if ($this->isArrayDimFetch($left)) {
return $this->parseAssignArrayDim($left, $right);
}
if ($this->isStaticPropertyFetch($left)) {
return $this->parseAssignStaticProperty($left, $right);
}
return $this->parseFinallyAssign($left, $right); return $this->parseFinallyAssign($left, $right);
} }
@ -2315,6 +2319,9 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function getArgInfo(Node $arg, string $funcName, int $index): ArgInfo protected function getArgInfo(Node $arg, string $funcName, int $index): ArgInfo
{ {
if (!isset($this->nativeFunctions[$funcName])) {
$this->fatalError($arg, "Function `{$funcName}` is undefined, you must adjust the order of function definition");
}
$funcDef = $this->nativeFunctions[$funcName]; $funcDef = $this->nativeFunctions[$funcName];
if (!array_key_exists($index, $funcDef->argInfoList)) { if (!array_key_exists($index, $funcDef->argInfoList)) {
$this->fatalError($arg, "Argument `{$index}` of function `{$funcName}` not found"); $this->fatalError($arg, "Argument `{$index}` of function `{$funcName}` not found");

Loading…
Cancel
Save