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 |
||
| 7 | class XgPusher |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The XingeApp instance. |
||
| 11 | * |
||
| 12 | * @var \ElfSundae\XgPush\XingeApp |
||
| 13 | */ |
||
| 14 | protected $service; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Create a new instance. |
||
| 18 | */ |
||
| 19 | public function __construct() |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get the XingeApp instance. |
||
| 26 | * |
||
| 27 | * @return \ElfSundae\XgPush\XingeApp |
||
| 28 | */ |
||
| 29 | public function getService() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a XingeApp instance. |
||
| 36 | * |
||
| 37 | * @return \ElfSundae\XgPush\XingeApp |
||
| 38 | */ |
||
| 39 | public static function createService() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get the app key. |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | public static function appKey() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the app secret. |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public static function appSecret() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the custom key. |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public static function customKey() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the environment. |
||
| 76 | * |
||
| 77 | * environment: 向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0. |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | public static function environment() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 解析信鸽返回的结果。 |
||
| 90 | * |
||
| 91 | * @see http://developer.qq.com/wiki/xg/%E6%9C%8D%E5%8A%A1%E7%AB%AFAPI%E6%8E%A5%E5%85%A5/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97.html |
||
| 92 | * |
||
| 93 | * @param mixed $xgResult 信鸽的请求结果 |
||
| 94 | * @param int &$code 返回码,0 为成功 |
||
| 95 | * @param string &$message 请求出错时的错误信息 |
||
| 96 | * @param mixed &$result 请求正确时的额外数据 |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public static function parseResult($xgResult = null, &$code = null, &$message = null, &$result = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Encode the custom data. |
||
| 116 | * |
||
| 117 | * @param mixed $data |
||
| 118 | * @return array|null |
||
| 119 | */ |
||
| 120 | public static function encodeCustomData($data) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get Xinge account for the given user. |
||
| 127 | * |
||
| 128 | * @param mixed $user |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public static function accountForUser($user) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Creates a MessageIOS instance. |
||
| 150 | * |
||
| 151 | * @param string $alert |
||
| 152 | * @param mixed $custom |
||
| 153 | * @param int $badge |
||
| 154 | * @param string $sound |
||
| 155 | * @return MessageIOS |
||
| 156 | */ |
||
| 157 | public static function createIOSMessage($alert = '', $custom = null, $badge = 1, $sound = 'default') |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Create a Message instance. |
||
| 176 | * |
||
| 177 | * @param string $content |
||
| 178 | * @param mixed $custom |
||
| 179 | * @param string $title |
||
| 180 | * @param int $type |
||
| 181 | * @return Message |
||
| 182 | */ |
||
| 183 | public static function createAndroidMessage($content = '', $custom = null, $title = null, $type = Message::TYPE_NOTIFICATION) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Query all device tokens for the given user. |
||
| 203 | * |
||
| 204 | * @param mixed $user |
||
| 205 | * @return string[]|null |
||
| 206 | */ |
||
| 207 | View Code Duplication | public static function queryDeviceTokensForUser($user) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Query all tags for the given device token. |
||
| 218 | * |
||
| 219 | * @param string $deviceToken |
||
| 220 | * @return string[]|null |
||
| 221 | */ |
||
| 222 | View Code Duplication | public static function queryTagsForDeviceToken($deviceToken) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Query all tags for the given user. |
||
| 233 | * |
||
| 234 | * @param mixed $user |
||
| 235 | * @param array &$deviceTokens |
||
| 236 | * @return array|null |
||
| 237 | */ |
||
| 238 | public static function queryTagsForUser($user, &$deviceTokens = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Dynamically handle calls to the XingeApp class. |
||
| 254 | * |
||
| 255 | * @param string $method |
||
| 256 | * @param array $parameters |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | public static function __callStatic($method, $parameters) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Dynamically handle calls to the XingeApp instance. |
||
| 268 | * |
||
| 269 | * @param string $method |
||
| 270 | * @param array $parameters |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function __call($method, $parameters) |
||
| 277 | } |
||
| 278 |
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.