refactor(reflection): 重构返回类型提取逻辑以支持联合和交集类型

- 提取公共方法 extractNamedReturnType 处理返回类型
- 移除对 ReflectionUnionType 和 ReflectionIntersectionType 的直接检查
- 简化函数和方法返回类型的获取逻辑
- 添加对联合类型返回值的测试用例
- 添加对交集类型返回值的测试用例
- 统一返回类型处理方式提高代码可维护性
pull/5/head
韩天峰 2 months ago
parent a01548ae96
commit 04610c5897
  1. 46
      phpunit/src/ReflectionTest.php
  2. 21
      src/Php/Reflection.php

@ -7,6 +7,25 @@ use PhpAot\Php\Reflection;
class ReflectionTest extends TestCase 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 public function testIsInternalClass(): void
{ {
// Standard PHP internal classes // Standard PHP internal classes
@ -125,6 +144,33 @@ class ReflectionTest extends TestCase
$this->assertEquals('string', $type); $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 public function testGetMethodReturnTypeNonexistent(): void
{ {
$type = Reflection::getMethodReturnType('NonExistent_' . uniqid(), 'test'); $type = Reflection::getMethodReturnType('NonExistent_' . uniqid(), 'test');

@ -93,15 +93,7 @@ class Reflection
if (!$func) { if (!$func) {
return null; return null;
} }
$returnType = $func->getReturnType(); return self::extractNamedReturnType($func->getReturnType());
if (!$returnType) {
return null;
}
if ($returnType instanceof \ReflectionUnionType) {
return null;
}
return $returnType->getName();
} }
public static function getFunctionParameter(string $fn, int $index): ?\ReflectionParameter public static function getFunctionParameter(string $fn, int $index): ?\ReflectionParameter
@ -172,7 +164,7 @@ class Reflection
return null; return null;
} }
$methodDef = $classRef->getMethod($method); $methodDef = $classRef->getMethod($method);
return $methodDef->getReturnType() ? $methodDef->getReturnType()->getName() : null; return self::extractNamedReturnType($methodDef->getReturnType());
} }
public static function isAbstractClass(string $name): bool public static function isAbstractClass(string $name): bool
@ -215,4 +207,13 @@ class Reflection
$lastParam = end($params); $lastParam = end($params);
return $lastParam->isVariadic() ? $lastParam : null; return $lastParam->isVariadic() ? $lastParam : null;
} }
private static function extractNamedReturnType(?\ReflectionType $returnType): ?string
{
if (!$returnType instanceof \ReflectionNamedType) {
return null;
}
return $returnType->getName();
}
} }

Loading…
Cancel
Save