Completed
Push — master ( 09425d...ca910a )
by Evan
02:23
created

Callback::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silk;
4
5
class Callback
6
{
7
    protected $target;
8
9
    /**
10
     * Create a new Callback instance
11
     *
12
     * @param callable $target The callback to wrap
13
     */
14
    public function __construct(callable $target)
15
    {
16
        $this->target = static::normalizeSyntax($target);
17
    }
18
19
    /**
20
     * Normalize the callable syntax
21
     *
22
     * Converts string static class method to standard callable array.
23
     *
24
     * @param  mixed $callback  Target callback
25
     *
26
     * @return mixed Closure    Anonymous function
27
     *               array      Class method
28
     *               string     Function
29
     */
30
    public static function normalizeSyntax(callable $callback)
31
    {
32
        if (is_string($callback) && false !== strpos($callback, '::')) {
33
            $callback = explode('::', $callback);
34
        }
35
36
        return $callback;
37
    }
38
39
    /**
40
     * Call the target callable
41
     *
42
     * @return mixed  Returns the return value of the callback, or FALSE on error.
43
     */
44
    public function call()
45
    {
46
        return $this->callArray(func_get_args());
47
    }
48
49
    /**
50
     * Call the target callable, with an array of arguments
51
     *
52
     * @param  array $arguments  The parameters to be passed to the callback, as an indexed array.
53
     * @return mixed             Returns the return value of the callback, or FALSE on error.
54
     */
55
    public function callArray(array $arguments = [])
56
    {
57
        return call_user_func_array($this->target, $arguments);
58
    }
59
60
    /**
61
     * Get the target callable
62
     *
63
     * @return mixed  The normalized callable
64
     */
65
    public function get()
66
    {
67
        return $this->target;
68
    }
69
70
    /**
71
     * Get the number of parameters from the callback's signature
72
     *
73
     * @return int
74
     */
75
    public function parameterCount()
76
    {
77
        return $this->reflect()->getNumberOfParameters();
78
    }
79
80
    /**
81
     * Get the corresponding Reflection instance for the target callable
82
     *
83
     * @return \ReflectionFunctionAbstract
84
     */
85
    public function reflect()
86
    {
87
        if ($this->target instanceof \Closure
88
            || (is_string($this->target) && function_exists($this->target))
89
            ) {
90
            return new \ReflectionFunction($this->target);
91
        }
92
93
        list($class, $method) = $this->target;
94
95
        return new \ReflectionMethod($class, $method);
96
    }
97
}
98