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 |
||
26 | class Device extends AbstractAPI |
||
27 | { |
||
28 | protected $deviceType; |
||
29 | protected $productId; |
||
30 | protected $config; |
||
31 | |||
32 | const API_TRANS_MSG = 'https://api.weixin.qq.com/device/transmsg'; |
||
33 | const API_CREATE = 'https://api.weixin.qq.com/device/create_qrcode'; |
||
34 | const API_DEV_STAT = 'https://api.weixin.qq.com/device/get_stat'; |
||
35 | const API_DEV_AUTH = 'https://api.weixin.qq.com/device/authorize_device'; |
||
36 | const API_DEV_GET_QRCODE = 'https://api.weixin.qq.com/device/getqrcode'; |
||
37 | const API_DEV_VERIFY_QRCODE = 'https://api.weixin.qq.com/device/verify_qrcode'; |
||
38 | const API_DEV_BIND = 'https://api.weixin.qq.com/device/bind'; |
||
39 | const API_DEV_UNBIND = 'https://api.weixin.qq.com/device/unbind'; |
||
40 | const API_DEV_COMPEL_BIND = 'https://api.weixin.qq.com/device/compel_bind'; |
||
41 | const API_DEV_COMPEL_UNBIND = 'https://api.weixin.qq.com/device/compel_unbind'; |
||
42 | const API_DEV_GET_OPENID = 'https://api.weixin.qq.com/device/get_openid'; |
||
43 | const API_USER_DEV_BIND = 'https://api.weixin.qq.com/device/get_bind_device'; |
||
44 | |||
45 | public function __construct(AccessToken $accessToken, $config) |
||
52 | |||
53 | public function setProductId($productId) |
||
58 | |||
59 | /** |
||
60 | * |
||
61 | * Send message to device. |
||
62 | * @param int $sceneValue |
||
63 | * |
||
64 | * @return \EasyWeChat\Support\Collection |
||
65 | */ |
||
66 | public function sendToDevice($deviceId, $openId, $content) |
||
77 | |||
78 | public function getDeviceQrcode(array $deviceIds) |
||
87 | |||
88 | public function authorizeDevice(array $deviceInfos, $opType = 0) |
||
99 | |||
100 | protected function getDeviceList($deviceInfos) |
||
124 | |||
125 | public function createDeviceId() |
||
133 | |||
134 | public function bind($openId, $deviceId, $ticket) |
||
144 | |||
145 | public function unbind($openId, $deviceId, $ticket) |
||
155 | |||
156 | View Code Duplication | public function compelBind($openId, $deviceId) |
|
165 | |||
166 | View Code Duplication | public function compelUnbind($openId, $deviceId) |
|
175 | |||
176 | View Code Duplication | public function getDeviceStatus($deviceId) |
|
184 | |||
185 | View Code Duplication | public function verifyQrcode($ticket) |
|
193 | |||
194 | View Code Duplication | public function getOpenid($deviceId) |
|
203 | |||
204 | View Code Duplication | public function getDeviceidByOpenid($openid) |
|
212 | } |
||
213 |
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 theSon
calls the wrong method in the parent class.