Completed
Push — master ( a7bdd4...c7897e )
by Antonio Carlos
03:18
created

Macroable::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Collection\Support\Traits;
4
5
use Closure;
6
use ReflectionClass;
7
use ReflectionMethod;
8
use BadMethodCallException;
9
10
trait Macroable
11
{
12
    /**
13
     * The registered string macros.
14
     *
15
     * @var array
16
     */
17
    protected static $macros = [];
18
19
    /**
20
     * Register a custom macro.
21
     *
22
     * @param  string $name
23
     * @param  object|callable  $macro
24
     *
25
     * @return void
26
     */
27 7
    public static function macro($name, $macro)
28
    {
29 7
        static::$macros[$name] = $macro;
30 7
    }
31
32
    /**
33
     * Mix another object into the class.
34
     *
35
     * @param  object  $mixin
36
     * @return void
37
     *
38
     * @throws \ReflectionException
39
     */
40
    public static function mixin($mixin)
41 2
    {
42
        $methods = (new ReflectionClass($mixin))->getMethods(
43 2
            ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
44 2
        );
45
46
        foreach ($methods as $method) {
47 2
            $method->setAccessible(true);
48 2
49 2
            static::macro($method->name, $method->invoke($mixin));
50 2
        }
51
    }
52
53 2
    /**
54
     * Checks if macro is registered.
55
     *
56
     * @param  string  $name
57
     * @return bool
58
     */
59
    public static function hasMacro($name)
60
    {
61 7
        return isset(static::$macros[$name]);
62
    }
63 7
64
    /**
65
     * Dynamically handle calls to the class.
66
     *
67
     * @param  string  $method
68
     * @param  array   $parameters
69
     * @return mixed
70
     *
71
     * @throws \BadMethodCallException
72
     */
73
    public static function __callStatic($method, $parameters)
74
    {
75 2
        if (! static::hasMacro($method)) {
76
            throw new BadMethodCallException(sprintf(
77 2
                'Method %s::%s does not exist.', static::class, $method
78
            ));
79
        }
80
81
        if (static::$macros[$method] instanceof Closure) {
82
            return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
83 2
        }
84 2
85
        return call_user_func_array(static::$macros[$method], $parameters);
86
    }
87
88
    /**
89
     * Dynamically handle calls to the class.
90
     *
91
     * @param  string  $method
92
     * @param  array   $parameters
93
     * @return mixed
94
     *
95
     * @throws \BadMethodCallException
96
     */
97
    public function __call($method, $parameters)
98
    {
99 6
        if (! static::hasMacro($method)) {
100
            throw new BadMethodCallException(sprintf(
101 6
                'Method %s::%s does not exist.', static::class, $method
102
            ));
103
        }
104
105
        $macro = static::$macros[$method];
106
107 6
        if ($macro instanceof Closure) {
108
            return call_user_func_array($macro->bindTo($this, static::class), $parameters);
109 6
        }
110 6
111
        return call_user_func_array($macro, $parameters);
112
    }
113
}
114