fix(php): 修复类型节点解析和返回类型检查问题

- 修正 resolveTypeNode 方法的返回类型为 NodeAbstract
- 在返回类型检查中添加对原生类的判断条件
- 仅对存在的类进行静态检查以避免动态类的编译期验证错误
- 保持对抽象类和接口的原有检查逻辑
pull/3/head
韩天峰 2 months ago
parent cd37d26e64
commit d49743a0fe
  1. 7
      src/Php/CompilerBase.php
  2. 2
      src/Php/Generator/AnonClassGenerator.php

@ -1655,7 +1655,12 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($v, 'The return type is `' . $returnClass . '`, cannot return an instance of `' . $objectClass . '`');
}
// 把子类当做父类返回时,父类必须是抽象类或者接口
if ($objectClass and $objectClass !== $returnClass and !$this->isAbstractClass($returnClass) and !$this->hasInterface($returnClass) and !$this->isInternalInterface($returnClass)) {
// 仅原生类进行静态检查,若类不存在,说明该类是动态类,无法进行编译期验证
if ($objectClass and $objectClass !== $returnClass
and $this->hasClass($returnClass)
and !$this->isAbstractClass($returnClass)
and !$this->hasInterface($returnClass)
and !$this->isInternalInterface($returnClass)) {
$this->fatalError($v, "When returning a subclass `$objectClass` instance as parent type, the parent class `$returnClass` must be abstract/interface");
}
}

@ -61,7 +61,7 @@ trait AnonClassGenerator
/**
* Resolve a single type node, converting relative Name to FullyQualified.
*/
protected function resolveTypeNode(Node $type): Node
protected function resolveTypeNode(Node $type): NodeAbstract
{
if ($type instanceof Name) {
if ($type->isFullyQualified()) {

Loading…
Cancel
Save