From 156fb0a8bfdf938361361549abcf89606bb17da2 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 8 Apr 2026 16:54:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=9F=A5=E5=92=8C=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=87=8D=E5=86=99=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整了对象参数类型验证的条件判断结构 - 在反射类中添加了获取类方法修饰符的辅助方法 - 优化了父类方法重写的检查逻辑,支持内置类的私有方法检测 - 添加了标签跳转来处理私有方法重写的错误情况 --- src/Php/CompilerBase.php | 12 +++++++----- src/Php/Reflection.php | 15 +++++++++++++++ src/Php/Translator.php | 8 ++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index cd16c9fc..492bbb0b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3270,11 +3270,13 @@ class CompilerBase extends \PhpAot\Core\Translator } if ($argInfo->type === self::TYPE_OBJECT) { - $object = $this->parseVariable($arg->value); - if ($this->isVarExpr($arg->value) and $this->isTypedObject($object)) { - $class = $this->getObjectType($object); - if ($argInfo->class and $class != $argInfo->class) { - $this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given"); + if ($this->isVarExpr($arg->value)) { + $object = $this->parseVariable($arg->value); + if ($this->isTypedObject($object)) { + $class = $this->getObjectType($object); + if ($argInfo->class and $class != $argInfo->class) { + $this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given"); + } } } return $this->convertObjectExpr($expr); diff --git a/src/Php/Reflection.php b/src/Php/Reflection.php index 2158e8ac..833c3213 100644 --- a/src/Php/Reflection.php +++ b/src/Php/Reflection.php @@ -95,6 +95,21 @@ class Reflection return $args[$index]; } + public static function getClassMethodModifiers(string $className, string $fn): ?int + { + $classRef = self::getClass($className); + if (!$classRef) { + return null; + } + + try { + $method = $classRef->getMethod($fn); + return $method->getModifiers(); + } catch (\ReflectionException $e) { + return null; + } + } + public static function getClassMethodParameter(string $className, string $fn, int $index): ?\ReflectionParameter { $classRef = self::getClass($className); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index f6995ac3..e0ec0874 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1200,10 +1200,18 @@ class Translator extends Preprocessor if (!$extends) { break; } + // 父类是内置类 + if ($classDef->inheritedFromInternalClass) { + if (Reflection::getClassMethodModifiers($extends, $name) & \ReflectionMethod::IS_PRIVATE) { + goto _error; + } + break; + } $classDef = $this->getClass($extends); if ($classDef->hasMethod($this->method)) { $methodDef = $classDef->getMethod($this->method); if ($methodDef->flags & Modifiers::PRIVATE) { + _error: $this->fatalError($v, 'Cannot override private method `' . $classDef->getNamespacedName(false) . '::' . $this->method . '()`');