-
Notifications
You must be signed in to change notification settings - Fork 7
/
Reflection.php
56 lines (49 loc) · 1.49 KB
/
Reflection.php
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
<?php
namespace mindplay\unbox;
use Closure;
use ReflectionFunction;
use ReflectionFunctionAbstract;
use ReflectionNamedType;
use ReflectionParameter;
use TypeError;
/**
* Pseudo-namespace for some common reflection helper-functions.
*/
abstract class Reflection
{
/**
* Create a Reflection of the function referenced by any type of callable
*
* @param callable $callback
*
* @return ReflectionFunctionAbstract
*
* @throws InvalidArgumentException
*/
public static function createFromCallable($callback): ReflectionFunctionAbstract
{
try {
return new ReflectionFunction(Closure::fromCallable($callback));
} catch (TypeError $error) {
throw new InvalidArgumentException("unexpected value: " . var_export($callback, true) . " - expected callable");
}
}
/**
* Obtain the type-hint of a `ReflectionParameter`, ignoring scalar types and PHP 8 union types.
*
* @param ReflectionParameter $param
*
* @return string|null fully-qualified type-name (or NULL, if no type-hint was available)
*/
public static function getParameterType(ReflectionParameter $param): ?string
{
$type = $param->getType();
if ($type instanceof ReflectionNamedType) {
if ($type->isBuiltin()) {
return null; // ignore scalar type-hints
}
return $type->getName();
}
return null; // no acceptable type-hint available
}
}