fix(php): 修复父类引用和命名参数错误处理问题

- 在parent类型声明前检查classDef是否存在,避免在类外部使用parent关键字时报错
- 改进命名参数缺失默认值时的错误节点定位逻辑
- 为parent类引用添加类上下文检查,防止在类外部使用parent关键字
pull/1/head
韩天峰 3 months ago
parent dec9849c69
commit 4ff12b58d1
  1. 15
      src/Php/CompilerBase.php

@ -1012,6 +1012,9 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($typeName === 'self') {
$class = $this->getFullClassName();
} elseif ($typeName === 'parent') {
if (!$this->classDef) {
$this->fatalError($type, 'Cannot use "parent" type declaration outside a class');
}
$class = $this->classDef->extends;
} elseif ($typeName === 'static') {
// static 类无法在编译期获取
@ -2645,7 +2648,14 @@ class CompilerBase extends \PhpAot\Core\Translator
}
if (!isset($args[$k])) {
if ($argInfo->defaultValue === null) {
$this->fatalError($callArgs[$i], 'Named argument `' . $argInfo->name . '` is missing default value');
$errorNode = null;
foreach ($callArgs as $a) {
if ($a instanceof Node\Arg && $a->name) {
$errorNode = $a;
break;
}
}
$this->fatalError($errorNode ?? reset($callArgs), 'Named argument `' . $argInfo->name . '` is missing default value');
}
$args[$k] = new Node\Arg($argInfo->defaultValue);
}
@ -3214,6 +3224,9 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($className === 'self') {
$className = $this->getFullClassName();
} elseif ($className === 'parent') {
if (!$this->classDef) {
$this->fatalError($expr, 'Cannot use "parent" outside a class');
}
$className = $this->classDef->extends;
} else {
$className = $this->getNamespacedClassName($className);

Loading…
Cancel
Save