MenuItem   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 22 6
A buttons() 0 14 3
A button() 0 8 1
1
<?php
2
/**
3
 * MenuItem.php
4
 *
5
 * @author Tian.
6
 */
7
8
namespace Wechat\Utils\Menu;
9
10
use Closure;
11
use Wechat\Utils\Menu\MagicAttributes;
12
13
/**
14
 * 菜单项
15
 *
16
 * @property array $sub_button
17
 */
18
class MenuItem extends MagicAttributes
19
{
20
21
    /**
22
     * 实例化菜单
23
     *
24
     * @param string $name
25
     * @param string $type
26
     * @param string $property
27
     */
28
    public function __construct($name, $type = null, $property = null)
29
    {
30
        $this->with('name', $name);
31
32
        $type !== null && $this->with('type', $type);
33
34
        if ($property !== null) {
35
            switch ($type) {
36
                case 'view':
37
                    $key = 'url';
38
                    break;
39
                case 'media_id':
40
                    // no break
41
                case 'view_limited':
42
                    $key = 'media_id';
43
                    break;
44
                default:
45
                    $key = 'key';
46
            }
47
            $this->with($key, $property);
48
        }
49
    }
50
51
    /**
52
     * 设置子菜单
53
     *
54
     * @param array $buttons
55
     *
56
     * @return MenuItem
57
     */
58
    public function buttons($buttons)
59
    {
60
        if ($buttons instanceof Closure) {
61
            $buttons = $buttons($this);
62
        }
63
64
        if (!is_array($buttons)) {
65
            exit('子菜单必须是数组或者匿名函数返回数组');
0 ignored issues
show
Coding Style Compatibility introduced by
The method buttons() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
66
        }
67
68
        $this->with('sub_button', $buttons);
69
70
        return $this;
71
    }
72
73
    /**
74
     * 添加子菜单
75
     *
76
     * @param MenuItem $button
77
     */
78
    public function button(MenuItem $button)
79
    {
80
        $subButtons = $this->sub_button;
81
82
        $subButtons[] = $button;
83
84
        $this->with('sub_button', $subButtons);
85
    }
86
}
87