From cfcbfcdb2f70d321b3b1e80c68a589cc28ac468a Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 1 Apr 2026 19:39:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E9=87=8D=E6=96=B0=E8=B5=8B=E5=80=BC=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=92=8Cunset=E6=93=8D=E4=BD=9C=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复数组类型变量重新赋值检查逻辑,排除VAR类型 - 优化错误消息格式,移除多余的空格 - 为unset操作添加未定义变量检查 - 为unset操作添加不支持类型的操作数错误处理 - 添加未定义变量测试用例 - 添加不支持的unset类型测试用例 --- phpunit/code/undefined-vars-01.php | 5 +++++ phpunit/code/unset-01.php | 5 +++++ phpunit/src/UndefineTest.php | 10 ++++++++++ src/Php/CompilerBase.php | 9 ++++++--- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 phpunit/code/undefined-vars-01.php create mode 100644 phpunit/code/unset-01.php create mode 100644 phpunit/src/UndefineTest.php diff --git a/phpunit/code/undefined-vars-01.php b/phpunit/code/undefined-vars-01.php new file mode 100644 index 00000000..715ac8c2 --- /dev/null +++ b/phpunit/code/undefined-vars-01.php @@ -0,0 +1,5 @@ +exec('The variable `$u1` is undefined', 'undefined-vars-01.php'); + $this->exec('Unsupported unset type `Expr_FuncCall`', 'unset-01.php'); + } +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 36598586..561675f5 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.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}`"); } }