Completed
Push — master ( e3fc2f...a51ab2 )
by Antonio Carlos
04:36
created

Macroable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 102
ccs 21
cts 27
cp 0.7778
rs 10
wmc 10
lcom 1
cbo 0

5 Methods

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