Completed
Push — master ( 0625ac...1a51b8 )
by Antonio Carlos
02:05
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.4746

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 3.4746
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 6
    public static function macro($name, $macro)
28
    {
29 6
        static::$macros[$name] = $macro;
30 6
    }
31
32
    /**
33
     * Mix another object into the class.
34
     *
35
     * @param  object  $mixin
36
     * @return void
37
     * @throws \ReflectionException
38
     */
39
    public static function mixin($mixin)
40 1
    {
41
        $methods = (new ReflectionClass($mixin))->getMethods(
42 1
            ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
43 1
        );
44
45
        foreach ($methods as $method) {
46 1
            $method->setAccessible(true);
47 1
48
            static::macro($method->name, $method->invoke($mixin));
49 1
        }
50
    }
51 1
52
    /**
53
     * Checks if macro is registered.
54
     *
55
     * @param  string  $name
56
     * @return bool
57
     */
58
    public static function hasMacro($name)
59 6
    {
60
        return isset(static::$macros[$name]);
61 6
    }
62
63
    /**
64
     * Dynamically handle calls to the class.
65
     *
66
     * @param  string  $method
67
     * @param  array   $parameters
68
     * @return mixed
69
     *
70
     * @throws \BadMethodCallException
71
     */
72
    public static function __callStatic($method, $parameters)
73 2
    {
74
        if (! static::hasMacro($method)) {
75 2
            throw new BadMethodCallException(sprintf(
76
                'Method %s::%s does not exist.', static::class, $method
77
            ));
78
        }
79
80
        if (static::$macros[$method] instanceof Closure) {
81 2
            return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
82 2
        }
83
84
        return call_user_func_array(static::$macros[$method], $parameters);
85
    }
86
87
    /**
88
     * Dynamically handle calls to the class.
89
     *
90
     * @param  string  $method
91
     * @param  array   $parameters
92
     * @return mixed
93
     *
94
     * @throws \BadMethodCallException
95
     */
96
    public function __call($method, $parameters)
97 5
    {
98
        if (! static::hasMacro($method)) {
99 5
            throw new BadMethodCallException(sprintf(
100
                'Method %s::%s does not exist.', static::class, $method
101
            ));
102
        }
103
104
        $macro = static::$macros[$method];
105 5
106
        if ($macro instanceof Closure) {
107 5
            return call_user_func_array($macro->bindTo($this, static::class), $parameters);
108 5
        }
109
110
        return call_user_func_array($macro, $parameters);
111
    }
112
}
113