refactor(php): 优化变量赋值类型检查逻辑

- 移除数组类型重分配检查的特殊处理
- 添加全局翻译器变量用于测试环境
- 引入通用变量赋值表达式检查方法
- 改进错误消息中的变量名显示格式
- 统一变量类型转换错误处理流程
pull/1/head
韩天峰 3 months ago
parent 619687d563
commit 5d150a2b02
  1. 3
      phpunit/bootstrap.php
  2. 11
      src/Php/CompilerBase.php

@ -5,13 +5,16 @@ use PhpAot\Php\CompilerTest;
use PhpAot\Php\Exception\TestError;
require __DIR__ . '/../bin/bootstrap.php';
require __DIR__ . '/../src/gen_stub.php';
class BaseTest extends TestCase
{
protected function exec(string $expected, string $file): void
{
try {
global $translator;
$compiler = CompilerTest::create(ROOT_PATH);
$translator = $compiler;
$testFile = __DIR__ . '/code/' . $file;
$compiler->addFiles([$testFile]);
$compiler->prepareFile($testFile);

@ -1551,10 +1551,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($left, "Cannot re-assign typed object `\${$var}` from `{$leftClass}` to `{$rightClass}`");
}
} else {
if ($this->getVarType($var) === self::TYPE_ARRAY) {
$this->fatalError($left, "Cannot re-assign `\${$var}` from object to array");
}
// TODO 右值是一个类型对象,但左值是一个 var ,许可,但无法标记对象类型
$this->checkVarAssignExpr($left, $this->getVarType($var), self::TYPE_OBJECT);
}
} else {
if ($this->isFuncCallExpr($right) and $this->isNameExpr($right->name)) {
@ -5448,7 +5445,11 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($this->isNativeType($toType) and $this->isNativeType($fromType)) {
return true;
}
$this->fatalError($left, "Cannot re-assign variable from `{$fromType}` to `{$toType}`");
$varName = 'variable';
if ($this->isVarExpr($left)) {
$varName = '`$' . $this->parseIdentifier($left) . '`';
}
$this->fatalError($left, "Cannot re-assign $varName from `{$fromType}` to `{$toType}`");
}
protected function mustNoCall(NodeAbstract $node): void

Loading…
Cancel
Save