From feef37d28ff71fa1a51697e0ba5f718d56cdf7c4 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 12 May 2026 18:23:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E8=A7=A3=E5=86=B3=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=8F=96=E6=B6=88=E8=AE=BE=E7=BD=AE=E6=97=B6=E7=A9=BA?= =?UTF-8?q?=E7=BB=B4=E5=BA=A6=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在数组维度获取为空时添加致命错误提示 - 添加对 unset($arr[]) 语法的错误检测 - 新增对应的单元测试验证空数组维度取消设置的错误情况 - 创建测试文件验证数组取消设置的边界情况 --- phpunit/code/unset-array-dim-fetch-empty.php | 6 ++++++ phpunit/src/UndefineTest.php | 7 ++++++- src/Php/CompilerBase.php | 5 ++++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 phpunit/code/unset-array-dim-fetch-empty.php diff --git a/phpunit/code/unset-array-dim-fetch-empty.php b/phpunit/code/unset-array-dim-fetch-empty.php new file mode 100644 index 00000000..67d9b0f1 --- /dev/null +++ b/phpunit/code/unset-array-dim-fetch-empty.php @@ -0,0 +1,6 @@ +exec('The variable `$u1` is undefined', 'undefined-vars-01.php'); $this->exec('Unsupported unset type `Expr_FuncCall`', 'unset-01.php'); } -} + + public function testUnsetEmptyArrayDimFetch() + { + $this->exec('Cannot use [] for array unset', 'unset-array-dim-fetch-empty.php'); + } +} \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index c4aca94c..0c4386a1 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4037,6 +4037,9 @@ class CompilerBase extends \PhpAot\Core\Translator $lines = []; foreach ($vars as $var) { if ($this->isArrayDimFetch($var)) { + if ($var->dim === null) { + $this->fatalError($var, 'Cannot use [] for array unset'); + } $array = $this->parseIdentifier($var->var); $dim = $this->parseIdentifier($var->dim); $lines[] = $array . '.offsetUnset(' . $dim . ');'; @@ -5781,4 +5784,4 @@ class CompilerBase extends \PhpAot\Core\Translator return $tmpVar; } -} +} \ No newline at end of file