fix(php): 修复子类作为父类返回时的类型检查问题

- 添加了内部接口类型的检查条件
- 实现了接口继承链的递归验证逻辑
- 支持传递性接口继承关系的检查
- 修复了Iterator等扩展Traversable的接口验证问题
pull/1/head
韩天峰 2 months ago
parent 8fa49e078f
commit ceb5e5963f
  1. 15
      src/Php/CompilerBase.php

@ -1573,7 +1573,7 @@ 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)) {
if ($objectClass and $objectClass !== $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");
}
}
@ -3564,6 +3564,19 @@ class CompilerBase extends \PhpAot\Core\Translator
if ($classDef->implements and in_array($expected, $classDef->implements)) {
return true;
}
// Check transitive interface inheritance (e.g., Iterator extends Traversable)
foreach ($classDef->implements as $iface) {
$check = $iface;
while ($check && $this->hasInterface($check)) {
if (strcasecmp($check, $expected) === 0) {
return true;
}
$check = $this->getInterface($check)->extends;
}
if (is_subclass_of($iface, $expected)) {
return true;
}
}
} else {
if (strcasecmp($class, $expected) === 0) {
return true;

Loading…
Cancel
Save