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 Device extends AbstractAPI |
||
| 28 | { |
||
| 29 | |||
| 30 | protected $deviceType; |
||
| 31 | protected $productId; |
||
| 32 | protected $config; |
||
| 33 | |||
| 34 | const API_TRANS_MSG = 'https://api.weixin.qq.com/device/transmsg'; |
||
| 35 | const API_CREATE = 'https://api.weixin.qq.com/device/create_qrcode'; |
||
| 36 | const API_DEV_STAT = 'https://api.weixin.qq.com/device/get_stat'; |
||
| 37 | const API_DEV_AUTH = 'https://api.weixin.qq.com/device/authorize_device'; |
||
| 38 | const API_DEV_GET_QRCODE = 'https://api.weixin.qq.com/device/getqrcode'; |
||
| 39 | const API_DEV_VERIFY_QRCODE = 'https://api.weixin.qq.com/device/verify_qrcode'; |
||
| 40 | const API_DEV_BIND = 'https://api.weixin.qq.com/device/bind'; |
||
| 41 | const API_DEV_UNBIND = 'https://api.weixin.qq.com/device/unbind'; |
||
| 42 | const API_DEV_COMPEL_BIND = 'https://api.weixin.qq.com/device/compel_bind'; |
||
| 43 | const API_DEV_COMPEL_UNBIND = 'https://api.weixin.qq.com/device/compel_unbind'; |
||
| 44 | const API_DEV_GET_OPENID = 'https://api.weixin.qq.com/device/get_openid'; |
||
| 45 | const API_USER_DEV_BIND = 'https://api.weixin.qq.com/device/get_bind_device'; |
||
| 46 | |||
| 47 | public function __construct(AccessToken $accessToken, $config) |
||
| 54 | |||
| 55 | public function setProductId($productId) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Send message to device |
||
| 63 | * |
||
| 64 | * @param int $sceneValue |
||
| 65 | * |
||
| 66 | * @return \EasyWeChat\Support\Collection |
||
| 67 | */ |
||
| 68 | public function sendToDevice($deviceId, $openId, $content) |
||
| 79 | |||
| 80 | public function getDeviceQrcode(array $deviceIds) |
||
| 89 | |||
| 90 | public function authorizeDevice(array $deviceInfos, $opType = 0) |
||
| 101 | |||
| 102 | protected function getDeviceList($deviceInfos) |
||
| 127 | |||
| 128 | public function createDeviceId() |
||
| 136 | |||
| 137 | public function bind($openId, $deviceId, $ticket) |
||
| 147 | |||
| 148 | public function unbind($openId, $deviceId, $ticket) |
||
| 158 | |||
| 159 | View Code Duplication | public function compelBind($openId, $deviceId) |
|
| 168 | |||
| 169 | View Code Duplication | public function compelUnbind($openId, $deviceId) |
|
| 178 | |||
| 179 | View Code Duplication | public function getDeviceStatus($deviceId) |
|
| 187 | |||
| 188 | View Code Duplication | public function verifyQrcode($ticket) |
|
| 196 | |||
| 197 | View Code Duplication | public function getOpenid($deviceId) |
|
| 206 | |||
| 207 | View Code Duplication | public function getDeviceidByOpenid($openid) |
|
| 215 | } |
||
| 216 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.