Passed
Pull Request — master (#1300)
by Curtis
05:45
created

StaticMethods::addDynamicStaticMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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