feat(closure): 完善闭包 use 语法支持

- 添加对非变量表达式的错误检查,只允许变量名
- 实现闭包 use 引用类型的就地变量创建功能
- 在当前作用域中为引用变量设置正确的类型标识
- 改进普通变量的未定义变量错误检查逻辑
- 确保闭包 use 语法的类型安全性和变量可见性
pull/1/head
韩天峰 5 months ago
parent a50e889751
commit 9526f9992b
  1. 17
      src/Php/Generator/ClosureGenerator.php

@ -86,12 +86,25 @@ trait ClosureGenerator
if ($uses) { if ($uses) {
foreach ($uses as $useItem) { foreach ($uses as $useItem) {
$var = $this->parseIdentifier($useItem->var); $var = $this->parseIdentifier($useItem->var);
if ($this->isVarExpr($useItem->var) and !$this->hasVar($var)) { if (!$this->isVarExpr($useItem->var)) {
$this->errorUndefinedVariable($useItem->var); $this->fatalError($useItem->var, 'Incorrect Closure use syntax, only variable names are allowed');
} }
if ($useItem->byRef) { if ($useItem->byRef) {
// 闭包的 use 语法,若为引用类型,可以就地创建变量
if ($useCurrentScope) {
if (!isset($oriLocalVars[$var])) {
$oriLocalVars[$var] = self::TYPE_REF;
}
} else {
if (!$this->hasVar($var)) {
$this->addLocalVar($var, self::TYPE_REF);
}
}
$useVars[] = $this->convertToRef($useItem->var); $useVars[] = $this->convertToRef($useItem->var);
} else { } else {
if (!$this->hasVar($var)) {
$this->errorUndefinedVariable($useItem->var);
}
$useVars[] = $var; $useVars[] = $var;
} }
} }

Loading…
Cancel
Save