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 |
||
27 | class Shelf extends Base implements ShelfInterface |
||
28 | { |
||
29 | const API_ADD = 'https://api.weixin.qq.com/merchant/shelf/add'; |
||
30 | const API_DELETE = 'https://api.weixin.qq.com/merchant/shelf/del'; |
||
31 | const API_UPDATE = 'https://api.weixin.qq.com/merchant/shelf/mod'; |
||
32 | const API_LISTS = 'https://api.weixin.qq.com/merchant/shelf/getall'; |
||
33 | const API_GET_BY_ID = 'https://api.weixin.qq.com/merchant/shelf/getbyid'; |
||
34 | |||
35 | /** |
||
36 | * 添加货架 |
||
37 | * |
||
38 | * @param $shelfData |
||
39 | * @param $shelfBanner |
||
40 | * @param $shelfName |
||
41 | * |
||
42 | * @return int |
||
43 | * |
||
44 | * @throws ShopsException |
||
45 | */ |
||
46 | View Code Duplication | public function add($shelfData, $shelfBanner, $shelfName) |
|
69 | |||
70 | /** |
||
71 | * 删除货架 |
||
72 | * |
||
73 | * @param int $shelfId |
||
74 | * |
||
75 | * @return bool |
||
76 | * |
||
77 | * @throws ShopsException |
||
78 | */ |
||
79 | public function delete($shelfId) |
||
85 | |||
86 | /** |
||
87 | * 修改货架 |
||
88 | * |
||
89 | * @param array|callable $shelfData |
||
90 | * @param $shelfId |
||
91 | * @param $shelfBanner |
||
92 | * @param $shelfName |
||
93 | * |
||
94 | * @return bool |
||
95 | * |
||
96 | * @throws ShopsException |
||
97 | */ |
||
98 | View Code Duplication | public function update($shelfData, $shelfId, $shelfBanner, $shelfName) |
|
122 | |||
123 | /** |
||
124 | * 获取所有货架 |
||
125 | * |
||
126 | * @return array |
||
127 | * |
||
128 | * @throws ShopsException |
||
129 | */ |
||
130 | public function lists() |
||
136 | |||
137 | /** |
||
138 | * 根据货架ID获取货架信息 |
||
139 | * |
||
140 | * @param $shelfId |
||
141 | * |
||
142 | * @return array |
||
143 | * |
||
144 | * @throws ShopsException |
||
145 | */ |
||
146 | public function getById($shelfId) |
||
152 | } |
||
153 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.