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

Shelf   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 39.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 46
loc 117
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 24 24 3
A delete() 0 6 1
A update() 22 22 3
A lists() 0 6 1
A getById() 0 6 1

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.
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;
16
17
use Overtrue\Wechat\Shop\Foundation\Base;
18
use Overtrue\Wechat\Shop\Foundation\Shelf as ShelfInterface;
19
use Overtrue\Wechat\Shop\Data\Shelf as ShelfData;
20
use Overtrue\Wechat\Shop\Foundation\ShopsException;
21
22
/**
23
 * 货架系统
24
 *
25
 * Class Shelf
26
 * @package Shop
27
 */
28
class Shelf extends Base implements ShelfInterface
29
{
30
31
    const API_ADD = 'https://api.weixin.qq.com/merchant/shelf/add';
32
    const API_DELETE = 'https://api.weixin.qq.com/merchant/shelf/del';
33
    const API_UPDATE = 'https://api.weixin.qq.com/merchant/shelf/mod';
34
    const API_LISTS = 'https://api.weixin.qq.com/merchant/shelf/getall';
35
    const API_GET_BY_ID = 'https://api.weixin.qq.com/merchant/shelf/getbyid';
36
37
    /**
38
     * 添加货架
39
     *
40
     * @param $shelfData
41
     * @param $shelfBanner
42
     * @param $shelfName
43
     * @return int
44
     * @throws ShopsException
45
     */
46 View Code Duplication
    public function add($shelfData,$shelfBanner,$shelfName)
47
    {
48
49
        if (is_callable($shelfData)) {
50
            $shelf = call_user_func($shelfData, new ShelfData());
51
            if (!($shelf instanceof ShelfData)) throw new ShopsException('必须返回 Shop\Data\Shelf class');
52
            $shelfData = $shelf->toArray();
53
        }
54
55
        //todo 判断出bug
56
        //if (!is_array($shelfData)) throw new ShopsException('$shelfData 必须是数组');
57
58
        $this->response = $this->http->jsonPost(self::API_ADD,array(
59
            'shelf_data' => array(
60
                'module_infos'=>$shelfData,
61
            ),
62
            'shelf_banner'=>$shelfBanner,
63
            'shelf_name'=>$shelfName
64
        ));
65
66
67
        return $this->getResponse();
68
69
    }
70
71
    /**
72
     * 删除货架
73
     *
74
     * @param int $shelfId
75
     * @return bool
76
     * @throws ShopsException
77
     */
78
    public function delete($shelfId)
79
    {
80
        $this->response = $this->http->jsonPost(self::API_DELETE,array('shelf_id' => $shelfId));
81
82
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 82 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Shelf::delete of type boolean.
Loading history...
83
    }
84
85
    /**
86
     * 修改货架
87
     *
88
     * @param array|callable $shelfData
89
     * @param $shelfId
90
     * @param $shelfBanner
91
     * @param $shelfName
92
     * @return bool
93
     * @throws ShopsException
94
     */
95 View Code Duplication
    public function update($shelfData,$shelfId,$shelfBanner,$shelfName)
96
    {
97
        if (is_callable($shelfData)) {
98
            $shelf = call_user_func($shelfData, new ShelfData());
99
            if (!($shelf instanceof ShelfData)) throw new ShopsException('必须返回 Shop\Data\Shelf class');
100
            $shelfData = $shelf->toArray();
101
        }
102
103
        //todo 判断出bug
104
        //if (!is_array($shelfData)) throw new ShopsException('$shelfData 必须是数组');
105
106
        $this->response = $this->http->jsonPost(self::API_UPDATE,array(
107
            'shelf_id' => $shelfId,
108
            'shelf_data' => array(
109
                'module_infos'=>$shelfData,
110
            ),
111
            'shelf_banner'=>$shelfBanner,
112
            'shelf_name'=>$shelfName
113
        ));
114
115
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type array to the return on line 115 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Shelf::update of type boolean.
Loading history...
116
    }
117
118
    /**
119
     * 获取所有货架
120
     *
121
     * @return array
122
     * @throws ShopsException
123
     */
124
    public function lists()
125
    {
126
        $this->response = $this->http->get(self::API_LISTS);
127
128
        return $this->getResponse();
129
    }
130
131
    /**
132
     * 根据货架ID获取货架信息
133
     *
134
     * @param $shelfId
135
     * @return array
136
     * @throws ShopsException
137
     */
138
    public function getById($shelfId)
139
    {
140
        $this->response = $this->http->jsonPost(self::API_GET_BY_ID,array('shelf_id' => $shelfId));
141
142
        return $this->getResponse();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getResponse(); of type boolean|array adds the type boolean to the return on line 142 which is incompatible with the return type declared by the interface Overtrue\Wechat\Shop\Foundation\Shelf::getById of type array.
Loading history...
143
    }
144
}