Skip to content

Commit e4121fd

Browse files
committed
feat(Functions): Added request data utils
1 parent eedcf46 commit e4121fd

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

Reflection.php

+52-38
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,70 @@ class Reflection {
2020
/**
2121
* Get a reflector for the target.
2222
*
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
2527
*
2628
* @throws \InvalidArgumentException If the target is invalid.
2729
*/
28-
public static function get_reflector( mixed $target ): Reflector {
30+
public static function get_reflector( callable|array|string|object $target ): ReflectionClass|ReflectionFunction|ReflectionMethod {
2931
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 ),
3438
default => throw new \InvalidArgumentException( 'Invalid target' ),
3539
};
3640
}
3741

3842
/**
3943
* Is the target callable.
4044
*
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.
4248
* @return bool
4349
*/
44-
public static function is_callable( mixed $target ): bool {
50+
public static function is_callable( callable|array|string|object $target ): bool {
4551
return static::is_valid_method( $target ) || static::is_valid_function( $target );
4652
}
4753

4854
/**
4955
* Is the target a valid class.
5056
*
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.
5260
* @return bool
5361
*/
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 ) );
5664
}
5765

5866
/**
5967
* Is the target a valid method.
6068
*
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.
6272
* @return bool
6373
*/
64-
public static function is_valid_method( mixed $target ): bool {
74+
public static function is_valid_method( callable|array|string|object $target ): bool {
6575
return \is_array( $target ) && \is_callable( $target );
6676
}
6777

6878
/**
6979
* Is the target a valid function.
7080
*
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.
7284
* @return bool
7385
*/
74-
public static function is_valid_function( mixed $target ): bool {
86+
public static function is_valid_function( callable|array|string|object $target ): bool {
7587
return \is_string( $target ) && ( \function_exists( $target ) || \is_callable( $target ) );
7688
}
7789

@@ -92,14 +104,14 @@ public static function class_implements( string|object $thing, string $iname, bo
92104
/**
93105
* Get decorators for a target
94106
*
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>[]
100112
*/
101113
public static function get_attributes(
102-
mixed $target,
114+
callable|array|string|object $target,
103115
string $decorator,
104116
?int $flags = ReflectionAttribute::IS_INSTANCEOF,
105117
): array {
@@ -110,14 +122,14 @@ public static function get_attributes(
110122
/**
111123
* Get decorators for a target
112124
*
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.
117129
* @return array<T>
118130
*/
119131
public static function get_decorators(
120-
mixed $target,
132+
callable|array|string|object $target,
121133
string $decorator,
122134
?int $flags = ReflectionAttribute::IS_INSTANCEOF,
123135
): array {
@@ -130,14 +142,14 @@ public static function get_decorators(
130142
/**
131143
* Get decorators for a target class, and its parent classes.
132144
*
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.
137149
* @return array<T>
138150
*/
139151
public static function get_decorators_deep(
140-
mixed $target,
152+
callable|array|string|object $target,
141153
string $decorator,
142154
?int $flags = ReflectionAttribute::IS_INSTANCEOF,
143155
): array {
@@ -160,12 +172,14 @@ public static function get_decorators_deep(
160172
/**
161173
* Get a **SINGLE** attribute for a target
162174
*
163-
* @template T
175+
* @template T of object
176+
* @template K of int
177+
*
164178
* @param Reflector|mixed $target The target to get decorators for.
165179
* @param class-string<T> $decorator The decorator to get.
166180
* @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
169183
*/
170184
public static function get_attribute(
171185
mixed $target,
@@ -179,7 +193,7 @@ public static function get_attribute(
179193
/**
180194
* Get a **SINGLE** decorator for a target
181195
*
182-
* @template T
196+
* @template T of object
183197
* @param Reflector|mixed $target The target to get decorators for.
184198
* @param class-string<T> $decorator The decorator to get.
185199
* @param int|null $flags Flags to pass to getAttributes.
@@ -202,7 +216,7 @@ public static function get_decorator(
202216
*
203217
* @param string|object $target Class or object to get the traits for.
204218
* @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.
206220
*/
207221
public static function class_uses_deep( string|object $target, bool $autoload = true ) {
208222
$traits = array();
@@ -222,7 +236,7 @@ public static function class_uses_deep( string|object $target, bool $autoload =
222236
/**
223237
* Get the inheritance chain for a class.
224238
*
225-
* @template T
239+
* @template T of object
226240
*
227241
* @param class-string<T>|T $target The class to get the inheritance chain for.
228242
* @param bool $inclusive Whether to include the target class in the chain.

0 commit comments

Comments
 (0)