Macroable::mixin()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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