fix(php): 解决数组取消设置时空维度的错误处理

- 在数组维度获取为空时添加致命错误提示
- 添加对 unset($arr[]) 语法的错误检测
- 新增对应的单元测试验证空数组维度取消设置的错误情况
- 创建测试文件验证数组取消设置的边界情况
pull/1/head
韩天峰 4 months ago
parent 31fcc94c2e
commit feef37d28f
  1. 6
      phpunit/code/unset-array-dim-fetch-empty.php
  2. 7
      phpunit/src/UndefineTest.php
  3. 5
      src/Php/CompilerBase.php

@ -0,0 +1,6 @@
<?php
function main()
{
$arr = [];
unset($arr[]);
}

@ -7,4 +7,9 @@ class UndefineTest extends \BaseTest
$this->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');
}
}

@ -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;
}
}
}
Loading…
Cancel
Save