1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace N1215\CakeCandle\Reflection; |
||||
5 | |||||
6 | use Closure; |
||||
7 | use ReflectionException; |
||||
8 | use ReflectionFunction; |
||||
9 | use ReflectionFunctionAbstract; |
||||
10 | use ReflectionMethod; |
||||
11 | use ReflectionParameter; |
||||
12 | |||||
13 | /** |
||||
14 | * Class ReflectionCallable |
||||
15 | * @package N1215\CakeCandle\Reflection |
||||
16 | */ |
||||
17 | final class ReflectionCallable |
||||
18 | { |
||||
19 | /** |
||||
20 | * @var callable |
||||
21 | */ |
||||
22 | private $callable; |
||||
23 | |||||
24 | /** |
||||
25 | * @var ReflectionFunctionAbstract |
||||
26 | */ |
||||
27 | private $reflectionFunctionAbstract; |
||||
28 | |||||
29 | /** |
||||
30 | * @param callable $callable |
||||
31 | * @throws ReflectionException |
||||
32 | */ |
||||
33 | 19 | public function __construct(callable $callable) |
|||
34 | { |
||||
35 | 19 | $this->callable = $callable; |
|||
36 | 19 | $this->reflectionFunctionAbstract = $this->createReflectionFunctionAbstract($callable); |
|||
37 | 17 | } |
|||
38 | |||||
39 | /** |
||||
40 | * @param callable $callable |
||||
41 | * @return ReflectionFunctionAbstract |
||||
42 | * @throws ReflectionException |
||||
43 | */ |
||||
44 | 19 | private function createReflectionFunctionAbstract(callable $callable) |
|||
45 | { |
||||
46 | // array |
||||
47 | 19 | if (is_array($callable)) { |
|||
48 | 6 | list($class, $method) = $callable; |
|||
49 | 6 | return new ReflectionMethod($class, $method); |
|||
50 | } |
||||
51 | |||||
52 | // closure |
||||
53 | 13 | if ($callable instanceof Closure) { |
|||
54 | 7 | return new ReflectionFunction($callable); |
|||
55 | } |
||||
56 | |||||
57 | // callable object |
||||
58 | 6 | if (is_object($callable) && method_exists($callable, '__invoke')) { |
|||
59 | 2 | return new ReflectionMethod($callable, '__invoke'); |
|||
60 | } |
||||
61 | |||||
62 | // standard function |
||||
63 | 4 | if (function_exists($callable)) { |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
64 | 2 | return new ReflectionFunction($callable); |
|||
65 | } |
||||
66 | |||||
67 | // static method |
||||
68 | 2 | $parts = explode('::', $callable); |
|||
0 ignored issues
–
show
$callable of type callable is incompatible with the type string expected by parameter $string of explode() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | 2 | return new ReflectionMethod($parts[0], $parts[1]); |
|||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * @return ReflectionParameter[] |
||||
74 | */ |
||||
75 | 11 | public function getParameters() |
|||
76 | { |
||||
77 | 11 | return $this->reflectionFunctionAbstract->getParameters(); |
|||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * @param array $args |
||||
82 | * @return mixed |
||||
83 | */ |
||||
84 | 6 | public function invoke(array $args = []) |
|||
85 | { |
||||
86 | 6 | return ($this->callable)(...$args); |
|||
87 | } |
||||
88 | } |
||||
89 |