Module   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 28
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 4
loc 28
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 2 7 4
A __callStatic() 2 6 4
A extend() 0 8 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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