From 04610c5897f751d0fe36176b6462dd9b2adc3763 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 29 Jun 2026 14:05:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reflection):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=E6=8F=90=E5=8F=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81=E8=81=94=E5=90=88=E5=92=8C?= =?UTF-8?q?=E4=BA=A4=E9=9B=86=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取公共方法 extractNamedReturnType 处理返回类型 - 移除对 ReflectionUnionType 和 ReflectionIntersectionType 的直接检查 - 简化函数和方法返回类型的获取逻辑 - 添加对联合类型返回值的测试用例 - 添加对交集类型返回值的测试用例 - 统一返回类型处理方式提高代码可维护性 --- phpunit/src/ReflectionTest.php | 46 ++++++++++++++++++++++++++++++++++ src/Php/Reflection.php | 21 ++++++++-------- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/phpunit/src/ReflectionTest.php b/phpunit/src/ReflectionTest.php index 1158c2cf..effac68c 100644 --- a/phpunit/src/ReflectionTest.php +++ b/phpunit/src/ReflectionTest.php @@ -7,6 +7,25 @@ use PhpAot\Php\Reflection; class ReflectionTest extends TestCase { + public function testGetFunctionReturnTypeUnionReturnsNull(): void + { + eval('function php_aot_reflection_union_return(): int|string { return 1; }'); + $this->assertNull(Reflection::getFunctionReturnType('php_aot_reflection_union_return')); + } + + public function testGetFunctionReturnTypeIntersectionReturnsNull(): void + { + eval(' + interface PhpAotReflectionI1 {} + interface PhpAotReflectionI2 {} + final class PhpAotReflectionBoth implements PhpAotReflectionI1, PhpAotReflectionI2 {} + function php_aot_reflection_intersection_return(): PhpAotReflectionI1&PhpAotReflectionI2 { + return new PhpAotReflectionBoth(); + } + '); + $this->assertNull(Reflection::getFunctionReturnType('php_aot_reflection_intersection_return')); + } + public function testIsInternalClass(): void { // Standard PHP internal classes @@ -125,6 +144,33 @@ class ReflectionTest extends TestCase $this->assertEquals('string', $type); } + public function testGetMethodReturnTypeUnionReturnsNull(): void + { + eval(' + class PhpAotReflectionUnionMethodReturn { + public function value(): int|string { + return 1; + } + } + '); + $this->assertNull(Reflection::getMethodReturnType('PhpAotReflectionUnionMethodReturn', 'value')); + } + + public function testGetMethodReturnTypeIntersectionReturnsNull(): void + { + eval(' + interface PhpAotReflectionMethodI1 {} + interface PhpAotReflectionMethodI2 {} + final class PhpAotReflectionMethodBoth implements PhpAotReflectionMethodI1, PhpAotReflectionMethodI2 {} + class PhpAotReflectionIntersectionMethodReturn { + public function value(): PhpAotReflectionMethodI1&PhpAotReflectionMethodI2 { + return new PhpAotReflectionMethodBoth(); + } + } + '); + $this->assertNull(Reflection::getMethodReturnType('PhpAotReflectionIntersectionMethodReturn', 'value')); + } + public function testGetMethodReturnTypeNonexistent(): void { $type = Reflection::getMethodReturnType('NonExistent_' . uniqid(), 'test'); diff --git a/src/Php/Reflection.php b/src/Php/Reflection.php index 9dc89764..5c380d3f 100644 --- a/src/Php/Reflection.php +++ b/src/Php/Reflection.php @@ -93,15 +93,7 @@ class Reflection if (!$func) { return null; } - $returnType = $func->getReturnType(); - if (!$returnType) { - return null; - } - if ($returnType instanceof \ReflectionUnionType) { - return null; - } - - return $returnType->getName(); + return self::extractNamedReturnType($func->getReturnType()); } public static function getFunctionParameter(string $fn, int $index): ?\ReflectionParameter @@ -172,7 +164,7 @@ class Reflection return null; } $methodDef = $classRef->getMethod($method); - return $methodDef->getReturnType() ? $methodDef->getReturnType()->getName() : null; + return self::extractNamedReturnType($methodDef->getReturnType()); } public static function isAbstractClass(string $name): bool @@ -215,4 +207,13 @@ class Reflection $lastParam = end($params); return $lastParam->isVariadic() ? $lastParam : null; } + + private static function extractNamedReturnType(?\ReflectionType $returnType): ?string + { + if (!$returnType instanceof \ReflectionNamedType) { + return null; + } + + return $returnType->getName(); + } }