Completed
Push — master ( f67f4f...77dcfd )
by Curtis
40s queued 12s
created

Methods::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 10
1
<?php
2
3
namespace App\Traits\enso\DynamicMethods;
4
5
use BadMethodCallException;
6
use Closure;
7
8
trait Methods
9
{
10
    protected static array $dynamicMethods = [];
11
12
    public function __call($method, $args)
13
    {
14
        if (isset(static::$dynamicMethods[$method])) {
15
            $closure = Closure::bind(
16
                static::$dynamicMethods[$method], $this, static::class
17
            );
18
19
            return $closure(...$args);
20
        }
21
22
        if (method_exists(parent::class, '__call')) {
23
            return parent::__call($method, $args);
24
        }
25
26
        throw new BadMethodCallException(
27
            'Method '.static::class.'::'.$method.'() not found'
28
        );
29
    }
30
31
    public static function addDynamicMethod($name, Closure $method)
32
    {
33
        static::$dynamicMethods[$name] = $method;
34
    }
35
}
36