From d49743a0fe8af28f1fae33ba4388fd27eac88a96 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 23 Jun 2026 20:12:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=8A=82=E7=82=B9=E8=A7=A3=E6=9E=90=E5=92=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修正 resolveTypeNode 方法的返回类型为 NodeAbstract - 在返回类型检查中添加对原生类的判断条件 - 仅对存在的类进行静态检查以避免动态类的编译期验证错误 - 保持对抽象类和接口的原有检查逻辑 --- src/Php/CompilerBase.php | 7 ++++++- src/Php/Generator/AnonClassGenerator.php | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 915b723d..ff9e57d3 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.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"); } } diff --git a/src/Php/Generator/AnonClassGenerator.php b/src/Php/Generator/AnonClassGenerator.php index 43ebcfea..d65509fd 100644 --- a/src/Php/Generator/AnonClassGenerator.php +++ b/src/Php/Generator/AnonClassGenerator.php @@ -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()) {