Completed
Push — master ( 47a19f...e93966 )
by kill
06:04
created

Facade   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 8 2
A createFacade() 0 11 4
A getFacadeClass() 0 2 1
A instance() 0 4 1
A make() 0 4 1
A __callStatic() 0 4 1
1
<?php
2
/**
3
 * Created by rozbo at 2017/3/17 上午11:43
4
 */
5
6
namespace puck;
7
8
9
class Facade
10
{
11
12
    protected static $bind = [];
13
14
    /**
15
     * 绑定类的静态代理
16
     * @static
17
     * @access public
18
     * @param string    $name    代理名
19
     * @param string    $class   实际类名
20
     * @return object
21
     */
22
    public static function bind($name, $class = null)
23
    {
24
        if (is_array($name)) {
25
            self::$bind = array_merge(self::$bind, $name);
26
        } else {
27
            self::$bind[$name] = $class;
28
        }
29
    }
30
31
    /**
32
     * 创建Facade实例
33
     * @static
34
     * @access protected
35
     * @param string    $class    类名或标识
36
     * @param array     $args     变量
37
     * @return object
38
     */
39
    protected static function createFacade($class = '', $args = [])
40
    {
41
        $class       = $class ?: static::class;
42
        $facadeClass = static::getFacadeClass();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $facadeClass is correct as static::getFacadeClass() (which targets puck\Facade::getFacadeClass()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
        if ($facadeClass) {
44
            $class = $facadeClass;
45
        } elseif (isset(self::$bind[$class])) {
46
            $class = self::$bind[$class];
47
        }
48
        return Container::getInstance()->make($class, $args);
49
    }
50
51
    protected static function getFacadeClass()
52
    {}
53
    /**
54
     * 带参数实例化当前Facade类
55
     * @access public
56
     * @return object
57
     */
58
    public static function instance(...$args)
59
    {
60
        return self::createFacade('', $args);
61
    }
62
63
    /**
64
     * 指定某个Facade类进行实例化
65
     * @access public
66
     * @param string    $class    类名或者标识
67
     * @param array     $args     变量
68
     * @return object
69
     */
70
    public static function make($class, $args = [])
71
    {
72
        return self::createFacade($class, $args);
73
    }
74
75
    // 调用实际类的方法
76
    public static function __callStatic($method, $params)
77
    {
78
        return call_user_func_array([static::createFacade(), $method], $params);
79
    }
80
}