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

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

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

Loading…
Cancel
Save