add reflection util

This commit is contained in:
sunxyw 2022-04-07 19:09:28 +08:00 committed by sunxyw
parent f5d34198e7
commit a43a4a429e
No known key found for this signature in database
GPG Key ID: CEA01A083E98C578

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace ZM\Utils;
use ReflectionNamedType;
use ReflectionParameter;
class ReflectionUtil
{
/**
* 获取参数的类名(如有)
*
* @param ReflectionParameter $parameter 参数
* @return null|string 类名,如果参数不是类,返回 null
*/
public static function getParameterClassName(ReflectionParameter $parameter): ?string
{
// 获取参数类型
$type = $parameter->getType();
// 没有声明类型或为基本类型
if (!$type instanceof ReflectionNamedType || $type->isBuiltin()) {
return null;
}
// 获取类名
$class_name = $type->getName();
// 如果存在父类
if (!is_null($class = $parameter->getDeclaringClass())) {
if ($class_name === 'self') {
return $class->getName();
}
if ($class_name === 'parent' && $parent = $class->getParentClass()) {
return $parent->getName();
}
}
return $class_name;
}
}