@@ -20,58 +20,70 @@ class Reflection {
20
20
/**
21
21
* Get a reflector for the target.
22
22
*
23
- * @param mixed $target The target to get a reflector for.
24
- * @return ReflectionClass|ReflectionMethod|ReflectionFunction
23
+ * @template T of object
24
+ *
25
+ * @param Reflector|class-string<T>|T|callable|\Closure|array{T, string} $target The target to get a reflector for.
26
+ * @return ReflectionClass<T>|ReflectionMethod|ReflectionFunction
25
27
*
26
28
* @throws \InvalidArgumentException If the target is invalid.
27
29
*/
28
- public static function get_reflector ( mixed $ target ): Reflector {
30
+ public static function get_reflector ( callable | array | string | object $ target ): ReflectionClass | ReflectionFunction | ReflectionMethod {
29
31
return match ( true ) {
30
- $ target instanceof Reflector => $ target ,
31
- static ::is_valid_class ( $ target ) => new ReflectionClass ( $ target ),
32
- static ::is_valid_method ( $ target ) => new ReflectionMethod ( ...$ target ),
33
- static ::is_valid_function ( $ target ) => new ReflectionFunction ( $ target ),
32
+ $ target instanceof ReflectionClass,
33
+ $ target instanceof ReflectionMethod,
34
+ $ target instanceof ReflectionFunction => $ target ,
35
+ static ::is_valid_class ( $ target ) => new ReflectionClass ( $ target ),
36
+ static ::is_valid_method ( $ target ) => new ReflectionMethod ( ...$ target ),
37
+ static ::is_valid_function ( $ target ) => new ReflectionFunction ( $ target ),
34
38
default => throw new \InvalidArgumentException ( 'Invalid target ' ),
35
39
};
36
40
}
37
41
38
42
/**
39
43
* Is the target callable.
40
44
*
41
- * @param mixed $target The target to check.
45
+ * @template T of object
46
+ *
47
+ * @param class-string<T>|T|callable|\Closure|array{object, string} $target The target to get a reflector for.
42
48
* @return bool
43
49
*/
44
- public static function is_callable ( mixed $ target ): bool {
50
+ public static function is_callable ( callable | array | string | object $ target ): bool {
45
51
return static ::is_valid_method ( $ target ) || static ::is_valid_function ( $ target );
46
52
}
47
53
48
54
/**
49
55
* Is the target a valid class.
50
56
*
51
- * @param mixed $target The target to check.
57
+ * @template T of object
58
+ *
59
+ * @param class-string<T>|T|callable|\Closure|array{object, string}|object $target The target to get a reflector for.
52
60
* @return bool
53
61
*/
54
- public static function is_valid_class ( mixed $ target ): bool {
55
- return \is_object ( $ target ) || \ class_exists ( $ target );
62
+ public static function is_valid_class ( callable | array | string | object $ target ): bool {
63
+ return \is_object ( $ target ) || ( \is_string ( $ target ) && \ class_exists ( $ target ) );
56
64
}
57
65
58
66
/**
59
67
* Is the target a valid method.
60
68
*
61
- * @param mixed $target The target to check.
69
+ * @template T of object
70
+ *
71
+ * @param class-string<T>|T|callable|\Closure|array{object, string} $target The target to get a reflector for.
62
72
* @return bool
63
73
*/
64
- public static function is_valid_method ( mixed $ target ): bool {
74
+ public static function is_valid_method ( callable | array | string | object $ target ): bool {
65
75
return \is_array ( $ target ) && \is_callable ( $ target );
66
76
}
67
77
68
78
/**
69
79
* Is the target a valid function.
70
80
*
71
- * @param mixed $target The target to check.
81
+ * @template T of object
82
+ *
83
+ * @param class-string<T>|T|callable|\Closure|array{object, string} $target The target to get a reflector for.
72
84
* @return bool
73
85
*/
74
- public static function is_valid_function ( mixed $ target ): bool {
86
+ public static function is_valid_function ( callable | array | string | object $ target ): bool {
75
87
return \is_string ( $ target ) && ( \function_exists ( $ target ) || \is_callable ( $ target ) );
76
88
}
77
89
@@ -92,14 +104,14 @@ public static function class_implements( string|object $thing, string $iname, bo
92
104
/**
93
105
* Get decorators for a target
94
106
*
95
- * @template T
96
- * @param Reflector|mixed $target The target to get decorators for.
97
- * @param class-string<T> $decorator The decorator to get.
98
- * @param int|null $flags Flags to pass to getAttributes.
99
- * @return array <T>
107
+ * @template T of object
108
+ * @param Reflector|class-string<T>|T|callable|\Closure|array{T, string} $target The target to get decorators for.
109
+ * @param class-string<T> $decorator The decorator to get.
110
+ * @param int|null $flags Flags to pass to getAttributes.
111
+ * @return ReflectionAttribute <T>[]
100
112
*/
101
113
public static function get_attributes (
102
- mixed $ target ,
114
+ callable | array | string | object $ target ,
103
115
string $ decorator ,
104
116
?int $ flags = ReflectionAttribute::IS_INSTANCEOF ,
105
117
): array {
@@ -110,14 +122,14 @@ public static function get_attributes(
110
122
/**
111
123
* Get decorators for a target
112
124
*
113
- * @template T
114
- * @param Reflector|mixed $target The target to get decorators for.
115
- * @param class-string<T> $decorator The decorator to get.
116
- * @param int|null $flags Flags to pass to getAttributes.
125
+ * @template T of object
126
+ * @param Reflector|class-string<T>|T|callable|\Closure|array{T, string} $target The target to get decorators for.
127
+ * @param class-string<T> $decorator The decorator to get.
128
+ * @param int|null $flags Flags to pass to getAttributes.
117
129
* @return array<T>
118
130
*/
119
131
public static function get_decorators (
120
- mixed $ target ,
132
+ callable | array | string | object $ target ,
121
133
string $ decorator ,
122
134
?int $ flags = ReflectionAttribute::IS_INSTANCEOF ,
123
135
): array {
@@ -130,14 +142,14 @@ public static function get_decorators(
130
142
/**
131
143
* Get decorators for a target class, and its parent classes.
132
144
*
133
- * @template T
134
- * @param Reflector|mixed $target The target to get decorators for.
135
- * @param class-string<T> $decorator The decorator to get.
136
- * @param int|null $flags Flags to pass to getAttributes.
145
+ * @template T of object
146
+ * @param Reflector|class-string<T>|T|callable|\Closure|array{T, string} $target The target to get decorators for.
147
+ * @param class-string<T> $decorator The decorator to get.
148
+ * @param int|null $flags Flags to pass to getAttributes.
137
149
* @return array<T>
138
150
*/
139
151
public static function get_decorators_deep (
140
- mixed $ target ,
152
+ callable | array | string | object $ target ,
141
153
string $ decorator ,
142
154
?int $ flags = ReflectionAttribute::IS_INSTANCEOF ,
143
155
): array {
@@ -160,12 +172,14 @@ public static function get_decorators_deep(
160
172
/**
161
173
* Get a **SINGLE** attribute for a target
162
174
*
163
- * @template T
175
+ * @template T of object
176
+ * @template K of int
177
+ *
164
178
* @param Reflector|mixed $target The target to get decorators for.
165
179
* @param class-string<T> $decorator The decorator to get.
166
180
* @param int|null $flags Flags to pass to getAttributes.
167
- * @param int $index The index of the decorator to get.
168
- * @return T |null
181
+ * @param K $index The index of the decorator to get.
182
+ * @return ReflectionAttribute<T> |null
169
183
*/
170
184
public static function get_attribute (
171
185
mixed $ target ,
@@ -179,7 +193,7 @@ public static function get_attribute(
179
193
/**
180
194
* Get a **SINGLE** decorator for a target
181
195
*
182
- * @template T
196
+ * @template T of object
183
197
* @param Reflector|mixed $target The target to get decorators for.
184
198
* @param class-string<T> $decorator The decorator to get.
185
199
* @param int|null $flags Flags to pass to getAttributes.
@@ -202,7 +216,7 @@ public static function get_decorator(
202
216
*
203
217
* @param string|object $target Class or object to get the traits for.
204
218
* @param bool $autoload Whether to allow this function to load the class automatically through the __autoload() magic method.
205
- * @return array Array of traits.
219
+ * @return array<class-string> Array of traits.
206
220
*/
207
221
public static function class_uses_deep ( string |object $ target , bool $ autoload = true ) {
208
222
$ traits = array ();
@@ -222,7 +236,7 @@ public static function class_uses_deep( string|object $target, bool $autoload =
222
236
/**
223
237
* Get the inheritance chain for a class.
224
238
*
225
- * @template T
239
+ * @template T of object
226
240
*
227
241
* @param class-string<T>|T $target The class to get the inheritance chain for.
228
242
* @param bool $inclusive Whether to include the target class in the chain.
0 commit comments