From ffdf4c6653e4ce5afe2edf6d145af28775f5d680 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 21 Apr 2026 20:28:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(traits):=20=E8=A7=A3=E5=86=B3trait=E4=B8=AD?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加trait注入class时使用完整类名的处理逻辑 - 重构参数类型解析逻辑,统一使用parseTypeDecl方法 - 修复void/never类型作为参数类型的错误检查 - 简化类型判断和类名获取流程 - 添加测试用例验证trait方法冲突处理 --- src/Php/CompilerBase.php | 26 ++++++++++---------------- tests/aot/trait/009.phpt | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 tests/aot/trait/009.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index bb511ef3..3d7fe2ad 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -801,6 +801,10 @@ class CompilerBase extends \PhpAot\Core\Translator } else { $class = $this->getNamespacedClassName($typeName); } + // Trait 在注入 class 需要使用完整类名 + if ($class and $this->classDef and $this->classDef->trait) { + $type->name = $class; + } return self::TYPE_OBJECT; } } @@ -1976,23 +1980,13 @@ class CompilerBase extends \PhpAot\Core\Translator if ($param->byRef) { return self::TYPE_REF; } - if ($param->type === null or $param->type instanceof NullableType or $param->type instanceof UnionType) { - return self::TYPE_VAR; - } - $type = $param->type; - $typeName = $type->name; - if ($typeName === 'void' or $typeName === 'never') { - $this->fatalError($param, 'Cannot use `void`/`never` as a parameter type.'); - } elseif ($typeName === 'self') { - $class = $this->classDef->getNamespacedName(false); - } elseif (isset($this->zendTypeMap[$typeName])) { - return $this->getTypeFromZendType($typeName); - } else { - $class = $this->getNamespacedClassName($typeName); + $class = ''; + $type = $this->parseTypeDecl($param->type, self::DECL_TYPE_OF_PARAM, $class); + if ($class) { + $this->addObject($var, $class); + $argInfo->class = $class; } - $this->addObject($var, $class); - $argInfo->class = $class; - return self::TYPE_OBJECT; + return $type; } protected function parseIncludes(): string diff --git a/tests/aot/trait/009.phpt b/tests/aot/trait/009.phpt new file mode 100644 index 00000000..48e0e684 --- /dev/null +++ b/tests/aot/trait/009.phpt @@ -0,0 +1,39 @@ +--TEST-- +Method conflict in traits +--FILE-- +hello($foo1); + } +} +?> +--EXPECT-- \ No newline at end of file