refactor(php): 优化变量赋值表达式解析逻辑

- 提取变量类型获取逻辑到局部变量以提高可读性
- 移除冗余的类型检查转换逻辑
- 简化赋值操作中的类型处理流程
- 保持原有的类型推断功能不变
- 改进代码结构以增强维护性
pull/1/head
韩天峰 3 months ago
parent c5f38a3d05
commit 1111cc0c27
  1. 14
      src/Php/Parser/AssignOpTrait.php

@ -236,7 +236,8 @@ trait AssignOpTrait
$finalVarType = $this->isNativeType($type) ? $this->getNativeType($type) : $type;
$this->addLocalVar($var, $finalVarType);
} else {
$this->checkVarAssignExpr($left, $this->getVarType($var), $type);
$finalVarType = $this->getVarType($var);
$this->checkVarAssignExpr($left, $finalVarType, $type);
}
}
} elseif ($this->isPropertyFetch($left) and !$left->getAttribute('nativeProperty')) {
@ -252,17 +253,6 @@ trait AssignOpTrait
return $this->parseAssignArrayDim($left, $right);
}
// When the RHS is untyped (TYPE_VAR) but the LHS variable already has a known
// native type, use the LHS type for the conversion so that e.g.
// php::Int i; ... i = n; (n is php::Var)
// becomes i = php::toInt(n);
if ($finalVarType === self::TYPE_VAR && $this->hasVar($var)) {
$varType = $this->getVarType($var);
if ($varType !== self::TYPE_VAR) {
$finalVarType = $varType;
}
}
$rightExpr = $this->parseAssignRightExpr($right);
$leftExprType = $this->detectTypeOfExpr($left);
$rightExprType = $this->detectTypeOfExpr($right);

Loading…
Cancel
Save