Completed
Push — master ( a3c7a7...bd72ff )
by Carlos
02:47
created

Shelf   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 16.77 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 2
dl 26
loc 155
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A controlOne() 14 14 1
A controlTwo() 0 21 3
A controlThree() 12 12 1
C controlFour() 0 47 8
A controlFive() 0 19 2

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
 * Shelf.php
4
 *
5
 * Part of Overtrue\Wechat\Shop\Data.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    a939638621 <[email protected]>
11
 * @copyright 2015 a939638621 <[email protected]>
12
 * @link      https://github.com/a939638621
13
 */
14
15
namespace Overtrue\Wechat\Shop\Data;
16
17
18
use Overtrue\Wechat\Utils\MagicAttributes;
19
use Overtrue\Wechat\Shop\Foundation\ShopsException;
20
21
class Shelf extends MagicAttributes
22
{
23
    /**
24
     * 控件1
25
     *
26
     * @param int $count
27
     * @param int $groupId
28
     * @return $this
29
     */
30 View Code Duplication
    public function controlOne($count, $groupId)
31
    {
32
        $this->attributes[] = array(
33
            'group_info' => array(
34
                'filter' => array(
35
                    'count' => $count
36
                ),
37
                'group_id' => $groupId
38
            ),
39
            'eid' => count($this->attributes)+1
40
        );
41
42
        return $this;
43
    }
44
45
    /**
46
     * 控件2
47
     *
48
     * @param array $groupId
49
     * @return Shelf $this
50
     * @throws ShopsException
51
     */
52
    public function controlTwo(array $groupId)
53
    {
54
        if (count($groupId) > 4) {
55
            throw new ShopsException('错误个数');
56
        }
57
58
        $groupsData = array();
59
60
        foreach ($groupId as $v) {
61
            $groupsData[] = array( 'group_id' => $v);
62
        }
63
64
        $this->attributes[] = array(
65
            'group_infos' => array(
66
                'groups' => $groupsData
67
            ),
68
            'eid' => count($this->attributes)+1
69
        );
70
71
        return $this;
72
    }
73
74
    /**
75
     * 控件3
76
     *
77
     * @param string $groupId
78
     * @param string $img
79
     * @return Shelf $this
80
     */
81 View Code Duplication
    public function controlThree($groupId, $img)
82
    {
83
        $this->attributes[] = array(
84
            'group_info' => array(
85
                'group_id' => $groupId,
86
                'img' => $img
87
            ),
88
            'eid' => count($this->attributes)+1
89
        );
90
91
        return $this;
92
    }
93
94
    /**
95
     * 控件4
96
     *
97
     * @param array $groups
98
     * @return $this
99
     * @throws ShopsException
100
     */
101
    public function controlFour(array $groups)
102
    {
103
        if (count($groups) > 3) throw new ShopsException('个数错误');
104
105
        $groupsData = array();
106
107
        foreach ($groups as $k =>$v) {
108
            if (count($v) != 2) throw new ShopsException('个数错误');
109
110
            $keys = array_keys($v);
111
112
//            [
113
//                ['1','images'],
114
//                ['1','images'],
115
//                ['1','images']
116
//            ];
117
118
            if (is_numeric($keys[0]) && is_numeric($keys[1])) {
119
120
                $values = array_values($v);
121
122
                $groupsData[] = array(
123
                    'group_id' => $values[0],
124
                    'img' => $values[1]
125
                );
126
            }
127
128
//            [
129
//                [ 'group_id'=>'1','img'=>'images' ],
130
//                [ 'group_id'=>'1','img'=>'images' ],
131
//                [ 'group_id'=>'1','img'=>'images' ]
132
//            ];
133
134
            if ($keys[0] == 'group_id' && $keys[1] == 'img') {
135
                $groupsData = $groups;
136
            }
137
        }
138
139
        $this->attributes[] = array(
140
            'group_infos' => array(
141
                'groups'=>$groupsData
142
            ),
143
            'eid' => count($this->attributes)+1
144
        );
145
146
        return $this;
147
    }
148
149
    /**
150
     * 控件5
151
     *
152
     * @param array $groups
153
     * @param string $imgBackground
154
     * @return $this
155
     */
156
    public function controlFive(array $groups, $imgBackground)
157
    {
158
159
        $groupsData = array();
160
161
        foreach ($groups as $v) {
162
            $groupsData[] = array('group_id'=>$v);
163
        }
164
165
        $this->attributes[] = array(
166
            'group_infos' => array(
167
                'groups' => $groupsData,
168
                'img_background'=>$imgBackground
169
            ),
170
            'eid'=>count($this->attributes)+1
171
        );
172
173
        return $this;
174
    }
175
}