Module::__callStatic()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 6

Duplication

Lines 2
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 2
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Module trait
5
 *
6
 * Provides a way to extend static classes with new methods.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015/2016 - http://caffeina.com
11
 */
12
13
trait Module {
14
    static protected $__PROTOTYPE__ = array();
15
16
    final public function __call($name, $args){
17 View Code Duplication
      if (isset(static::$__PROTOTYPE__[$name]) && static::$__PROTOTYPE__[$name] instanceof \Closure)
18
        return call_user_func_array(static::$__PROTOTYPE__[$name]->bindTo($this, $this), $args);
19
      if (get_parent_class())
20
        return parent::__call($name, $args);
21
      else throw new \BadMethodCallException;
22
    }
23
24
    final public static function __callStatic($name, $args){
25 View Code Duplication
      if (isset(static::$__PROTOTYPE__[$name]) && static::$__PROTOTYPE__[$name] instanceof \Closure)
26
        return forward_static_call_array(static::$__PROTOTYPE__[$name], $args);
27
      if (get_parent_class()) return parent::__callStatic($name, $args);
28
      else throw new \BadMethodCallException;
29
    }
30
31
    public static function extend($method, $callback=null){
32
      $methods = ($callback === null && is_array($method)) ? $method : [$method=>$callback];
33
      foreach ($methods as $name => $meth) {
34
        if ($meth && $meth instanceof \Closure)
35
          static::$__PROTOTYPE__[$name] = $meth;
36
        else throw new \BadMethodCallException;
37
      }
38
    }
39
40
}
41