-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRef.php
More file actions
171 lines (159 loc) · 4.23 KB
/
Ref.php
File metadata and controls
171 lines (159 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* php 调用反射类获取类信息
*
* @author fdipzone
* @DateTime 2023-03-30 16:56:04
*
* Func
* public static setClass 设置反射类
* public static getBase 读取类基本信息
* public static getInterfaces 读取类接口
* public static getProperties 读取类属性
* public static getMethods 读取类方法
*/
class Ref{
/**
* 反射类对象
*
* @var ReflectionClass
*/
private static $refClass = null;
/**
* 设置反射类
*
* @author fdipzone
* @DateTime 2023-03-30 16:57:44
*
* @param string $class_name 类名称
* @return void
*/
public static function setClass(string $class_name):void{
self::$refClass = new ReflectionClass($class_name);
}
/**
* 读取类基本信息
*
* @author fdipzone
* @DateTime 2023-03-30 16:58:02
*
* @return array
*/
public static function getBase():array{
return array(
'className' => self::$refClass->getName(),
'classPath' => dirname(self::$refClass->getFileName()),
'classFileName' => basename(self::$refClass->getFileName()),
);
}
/**
* 读取类接口
*
* @author fdipzone
* @DateTime 2023-03-30 16:58:21
*
* @return array
*/
public static function getInterfaces():array{
$ret = [];
$interfaces = self::$refClass->getInterfaces();
if($interfaces){
foreach($interfaces as $interface){
$ret[] = $interface->getName();
}
}
return $ret;
}
/**
* 读取类属性
*
* @author fdipzone
* @DateTime 2023-03-30 16:58:33
*
* @return array
*/
public static function getProperties():array{
$ret = [];
$properties = self::$refClass->getProperties();
if($properties){
foreach($properties as $property){
$tmp = array(
'propertyName' => $property->getName(),
'propertyModifier' => self::getModifier($property),
'propertyComments' => self::formatComment($property->getDocComment()),
);
$ret[] = $tmp;
}
}
return $ret;
}
/**
* 读取类方法
*
* @author fdipzone
* @DateTime 2023-03-30 16:58:47
*
* @return array
*/
public static function getMethods():array{
$ret = [];
$methods = self::$refClass->getMethods();
if($methods){
foreach($methods as $method){
$tmp = array(
'methodName' => $method->getName(),
'methodModifier' => self::getModifier($method),
'methodParamsNum' => $method->getNumberOfParameters(),
'methodParams' => [],
'methodComments' => self::formatComment($method->getDocComment()),
);
$params = $method->getParameters();
if($params){
foreach($params as $param){
$tmp['methodParams'][] = $param->getName();
}
}
$ret[] = $tmp;
}
}
return $ret;
}
/**
* 获取修饰符 ReflectionMethod/ReflectionProperty Reflector
*
* @author fdipzone
* @DateTime 2023-03-30 16:59:05
*
* @param ReflectionMethod|ReflectionProperty|Reflector $o 反射类对象
* @return string
*/
private static function getModifier($o):string{
// public
if($o->isPublic()){
return 'public';
}
// protected
if($o->isProtected()){
return 'protected';
}
// private
if($o->isPrivate()){
return 'private';
}
return '';
}
/**
* 格式化注释内容
*
* @author fdipzone
* @DateTime 2023-03-30 17:00:33
*
* @param string $comment 注释内容
* @return string
*/
private static function formatComment(string $comment):string{
$doc = explode(PHP_EOL, $comment);
return isset($doc[1])? trim(str_replace('*','',$doc[1])) : '';
}
}
?>