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 |
||
| 29 | class Device extends AbstractAPI |
||
| 30 | { |
||
| 31 | const API_DEVICE_APPLYID = 'https://api.weixin.qq.com/shakearound/device/applyid'; |
||
| 32 | const API_DEVICE_APPLYSTATUS = 'https://api.weixin.qq.com/shakearound/device/applystatus'; |
||
| 33 | const API_DEVICE_UPDATE = 'https://api.weixin.qq.com/shakearound/device/update'; |
||
| 34 | const API_DEVICE_BINDLOCATION = 'https://api.weixin.qq.com/shakearound/device/bindlocation'; |
||
| 35 | const API_DEVICE_SEARCH = 'https://api.weixin.qq.com/shakearound/device/search'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Apply device ids. |
||
| 39 | * |
||
| 40 | * @param int $quantity |
||
| 41 | * @param string $reason |
||
| 42 | * @param string $comment |
||
| 43 | * @param int $poi_id |
||
| 44 | * |
||
| 45 | * @return \EasyWeChat\Support\Collection |
||
| 46 | */ |
||
| 47 | public function apply($quantity, $reason, $comment = '', $poi_id = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get audit status. |
||
| 67 | * |
||
| 68 | * @param int $apply_id |
||
| 69 | * |
||
| 70 | * @return \EasyWeChat\Support\Collection |
||
| 71 | */ |
||
| 72 | public function getStatus($apply_id) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Update a device comment. |
||
| 83 | * |
||
| 84 | * @param array $device_identifier |
||
| 85 | * @param string $comment |
||
| 86 | * |
||
| 87 | * @return \EasyWeChat\Support\Collection |
||
| 88 | */ |
||
| 89 | public function update(array $device_identifier, $comment) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Bind location for device. |
||
| 101 | * |
||
| 102 | * @param array $device_identifier |
||
| 103 | * @param int $poi_id |
||
| 104 | * @param int $type |
||
| 105 | * @param string $poi_appid |
||
| 106 | * |
||
| 107 | * @return \EasyWeChat\Support\Collection |
||
| 108 | * |
||
| 109 | * @throws InvalidArgumentException |
||
| 110 | */ |
||
| 111 | public function bindLocation(array $device_identifier, $poi_id, $type = 1, $poi_appid = null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Fetch batch of devices by device_ids. |
||
| 130 | * |
||
| 131 | * @param array $device_identifiers |
||
| 132 | * |
||
| 133 | * @return \EasyWeChat\Support\Collection |
||
| 134 | */ |
||
| 135 | public function fetchByIds(array $device_identifiers) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Pagination to fetch batch of devices |
||
| 147 | * |
||
| 148 | * @param int $last_seen |
||
| 149 | * @param int $count |
||
| 150 | * |
||
| 151 | * @return \EasyWeChat\Support\Collection |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function pagination($last_seen, $count) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Fetch batch of devices by apply_id. |
||
| 166 | * |
||
| 167 | * @param int $apply_id |
||
| 168 | * @param int $last_seen |
||
| 169 | * @param int $count |
||
| 170 | * |
||
| 171 | * @return \EasyWeChat\Support\Collection |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function fetchByApplyId($apply_id, $last_seen, $count) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Fetch batch of devices. |
||
| 187 | * |
||
| 188 | * @param array $params |
||
| 189 | * |
||
| 190 | * @return \EasyWeChat\Support\Collection |
||
| 191 | */ |
||
| 192 | private function fetch($params) |
||
| 196 | } |
||
| 197 |