fix(php): 修复变量重新赋值检查和unset操作的错误处理

- 修复数组类型变量重新赋值检查逻辑,排除VAR类型
- 优化错误消息格式,移除多余的空格
- 为unset操作添加未定义变量检查
- 为unset操作添加不支持类型的操作数错误处理
- 添加未定义变量测试用例
- 添加不支持的unset类型测试用例
pull/1/head
韩天峰 5 months ago
parent 9f52d48770
commit cfcbfcdb2f
  1. 5
      phpunit/code/undefined-vars-01.php
  2. 5
      phpunit/code/unset-01.php
  3. 10
      phpunit/src/UndefineTest.php
  4. 9
      src/Php/CompilerBase.php

@ -0,0 +1,5 @@
<?php
function main()
{
unset($u1, $u2);
}

@ -0,0 +1,5 @@
<?php
function main()
{
unset(var_dump('1'));
}

@ -0,0 +1,10 @@
<?php
class UndefineTest extends \BaseTest
{
public function testUnset()
{
$this->exec('The variable `$u1` is undefined', 'undefined-vars-01.php');
$this->exec('Unsupported unset type `Expr_FuncCall`', 'unset-01.php');
}
}

@ -1392,9 +1392,9 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->addLocalVar($var, $type);
} else {
if ($this->isTypedObject($var) and $type !== self::TYPE_OBJECT or
$this->getVarType($var) === self::TYPE_ARRAY and $type !== self::TYPE_ARRAY
$this->getVarType($var) === self::TYPE_ARRAY and ($type !== self::TYPE_ARRAY && $type !== self::TYPE_VAR)
) {
$this->fatalError($left, "Cannot re-assign variable `\${$var}` from " . $this->getVarType($var) . " to " . $type);
$this->fatalError($left, "Cannot re-assign variable `\${$var}` from " . $this->getVarType($var) . " to " . $type);
}
}
} elseif ($this->isPropertyFetch($left) and !$left->getAttribute('nativeProperty')) {
@ -3191,9 +3191,12 @@ class CompilerBase extends \PhpAot\Core\Translator
$lines[] = $object . '.unsetProperty(' . $propName . ');';
} elseif ($type === self::EXPR_VARIABLE) {
$name = $this->parseIdentifier($var);
if (!$this->hasVar($name)) {
$this->errorUndefinedVariable($var);
}
$lines[] = "{$name}.unset();";
} else {
abort($var);
$this->fatalError($var, "Unsupported unset type `{$type}`");
}
}

Loading…
Cancel
Save