Completed
Push — master ( afcc87...cf7539 )
by n
01:55
created

ReflectionCallable::getParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace N1215\CakeCandle\Reflection;
4
5
use ReflectionException;
6
use ReflectionFunction;
7
use ReflectionFunctionAbstract;
8
use ReflectionMethod;
9
use ReflectionParameter;
10
11
/**
12
 * Class ReflectionCallable
13
 * @package N1215\CakeCandle\Reflection
14
 */
15
final class ReflectionCallable
16
{
17
    /**
18
     * @var callable
19
     */
20
    private $callable;
21
22
    /**
23
     * @var ReflectionFunctionAbstract
24
     */
25
    private $reflectionFunctionAbstract;
26
27
    /**
28
     * @param callable $callable
29
     * @throws ReflectionException
30
     */
31 18
    public function __construct(callable $callable)
32
    {
33 18
        $this->callable = $callable;
34 18
        $this->reflectionFunctionAbstract = $this->createReflectionFunctionAbstract($callable);
35 16
    }
36
37
    /**
38
     * @param callable $callable
39
     * @return ReflectionFunctionAbstract
40
     * @throws ReflectionException
41
     */
42 18
    private function createReflectionFunctionAbstract(callable $callable)
43
    {
44
        // array
45 18
        if (\is_array($callable)) {
46 7
            list($class, $method) = $callable;
47 7
            return new ReflectionMethod($class, $method);
48
        }
49
50
        // closure
51 11
        if ($callable instanceof \Closure) {
52 5
            return new ReflectionFunction($callable);
53
        }
54
55
        // callable object
56 6
        if (\is_object($callable) && \method_exists($callable, '__invoke')) {
57 2
            return new ReflectionMethod($callable, '__invoke');
58
        }
59
60
        // standard function
61 4
        if (\function_exists($callable)) {
0 ignored issues
show
Bug introduced by
$callable of type callable is incompatible with the type string expected by parameter $function_name of function_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        if (\function_exists(/** @scrutinizer ignore-type */ $callable)) {
Loading history...
62 2
            return new ReflectionFunction($callable);
63
        }
64
65
        // static method
66 2
        $parts = explode('::', $callable);
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

66
        $parts = explode('::', /** @scrutinizer ignore-type */ $callable);
Loading history...
67 2
        return new ReflectionMethod($parts[0], $parts[1]);
68
    }
69
70
    /**
71
     * @return ReflectionParameter[]
72
     */
73 10
    public function getParameters()
74
    {
75 10
        return $this->reflectionFunctionAbstract->getParameters();
76
    }
77
78
    /**
79
     * @param array $args
80
     * @return mixed
81
     */
82 8
    public function invoke(array $args = [])
83
    {
84 8
        $callable = $this->callable;
85 8
        return call_user_func_array($callable, $args);
86
    }
87
}
88