Completed
Push — master ( 0fcf2a...2c5181 )
by Ivannis Suárez
03:29
created

Delegate::fromStaticMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Delegate;
13
14
/**
15
 * Delegate class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class Delegate extends AbstractDelegate
20
{
21
    /**
22
     * @var \ReflectionFunction|\ReflectionMethod
23
     */
24
    private $reflection = null;
25
26
    /**
27
     * @param Closure $closure
28
     *
29
     * @return static
30
     */
31
    public static function fromClosure(\Closure $closure)
32
    {
33
        return new static($closure);
34
    }
35
36
    /**
37
     * @param object $object
38
     * @param string $method
39
     *
40
     * @return static
41
     */
42
    public static function fromMethod($object, $method)
43
    {
44
        return new static(array($object, $method));
45
    }
46
47
    /**
48
     * @param string $class
49
     * @param string $method
50
     *
51
     * @return static
52
     */
53
    public static function fromStaticMethod($class, $method)
54
    {
55
        return new static(array($class, $method));
56
    }
57
58
    /**
59
     * @param string $function
60
     *
61
     * @return static
62
     */
63
    public static function fromFunction($function)
64
    {
65
        return new static($function);
66
    }
67
68
    /**
69
     * @return \ReflectionFunction|\ReflectionMethod
70
     */
71
    public function reflection()
72
    {
73
        if ($this->reflection === null) {
74
            if (\is_array($this->callable)) {
75
                $this->reflection = new \ReflectionMethod($this->callable[0], $this->callable[1]);
76
            } elseif (\is_object($this->callable) && !$this->callable instanceof \Closure) {
77
                $this->reflection = new \ReflectionMethod($this->callable, '__invoke');
78
            } else {
79
                $this->reflection = new \ReflectionFunction($this->callable);
80
            }
81
        }
82
83
        return $this->reflection;
84
    }
85
}
86