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:
Complex classes like XgPusher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XgPusher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class XgPusher |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The XingeApp instance. |
||
| 15 | * |
||
| 16 | * @var \ElfSundae\XgPush\XingeApp |
||
| 17 | */ |
||
| 18 | protected $xinge; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The pusher environment. |
||
| 22 | * |
||
| 23 | * 向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0. |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $environment = XingeApp::IOSENV_DEV; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The key of custom payload. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $customKey = 'custom'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Xinge account prefix. |
||
| 38 | * |
||
| 39 | * @warning 信鸽不允许使用简单的账号,例如纯数字的id。 |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $accountPrefix = 'user'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new instance. |
||
| 47 | * |
||
| 48 | * @param string $appKey |
||
| 49 | * @param string $appSecret |
||
| 50 | */ |
||
| 51 | public function __construct($appKey, $appSecret) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get the XingeApp instance. |
||
| 58 | * |
||
| 59 | * @return \ElfSundae\XgPush\XingeApp |
||
| 60 | */ |
||
| 61 | public function getXinge() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the app key. |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getAppKey() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the app secret. |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function getAppSecret() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get the pusher environment. |
||
| 88 | * |
||
| 89 | * @return int |
||
| 90 | */ |
||
| 91 | public function getEnvironment() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set the pusher environment. |
||
| 98 | * |
||
| 99 | * @param mixed $env |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function setEnvironment($env) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the key of custom payload. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getCustomKey() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set the key of custom payload. |
||
| 127 | * |
||
| 128 | * @param string $key |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setCustomKey($key) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the account prefix. |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function getAccountPrefix() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set the account prefix. |
||
| 152 | * |
||
| 153 | * @param string $prefix |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function setAccountPrefix($prefix) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Determine if the Xinge response is success. |
||
| 165 | * |
||
| 166 | * @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 |
||
| 167 | * |
||
| 168 | * @param mixed $response |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function succeed($response) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the code of Xinge response. |
||
| 178 | * |
||
| 179 | * @param mixed $response |
||
| 180 | * @return int |
||
| 181 | */ |
||
| 182 | public function code($response) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the error message of Xinge response. |
||
| 189 | * |
||
| 190 | * @param mixed $response |
||
| 191 | * @return string|null |
||
| 192 | */ |
||
| 193 | public function message($response) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the result data of Xinge response. |
||
| 202 | * |
||
| 203 | * @param mixed $response |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function result($response, $key = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Encode the custom data. |
||
| 215 | * |
||
| 216 | * @param mixed $data |
||
| 217 | * @return array|null |
||
| 218 | */ |
||
| 219 | public function encodeCustomData($data) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get Xinge account for the given user. |
||
| 228 | * |
||
| 229 | * @param mixed $user |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function accountForUser($user) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Creates a new MessageIOS instance. |
||
| 249 | * |
||
| 250 | * @param string $alert |
||
| 251 | * @param mixed $custom |
||
| 252 | * @param int $badge |
||
| 253 | * @param string $sound |
||
| 254 | * @return \ElfSundae\XgPush\MessageIOS |
||
| 255 | */ |
||
| 256 | public function createIOSMessage($alert = '', $custom = null, $badge = 1, $sound = 'default') |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Create a new Message instance. |
||
| 275 | * |
||
| 276 | * @param string $content |
||
| 277 | * @param mixed $custom |
||
| 278 | * @param string $title |
||
| 279 | * @param int $type |
||
| 280 | * @return \ElfSundae\XgPush\Message |
||
| 281 | */ |
||
| 282 | public function createAndroidMessage($content = '', $custom = null, $title = null, $type = Message::TYPE_NOTIFICATION) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Push message to a device. |
||
| 301 | * |
||
| 302 | * @param string $deviceToken |
||
| 303 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function toDevice($deviceToken, $message) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Push message to all devices. |
||
| 313 | * |
||
| 314 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function toAllDevices($message) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Push message to an user. |
||
| 324 | * |
||
| 325 | * @param mixed $user |
||
| 326 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function toUser($user, $message) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Push message to multi users. |
||
| 336 | * |
||
| 337 | * @warning 用户数限制 100 个。 |
||
| 338 | * |
||
| 339 | * @param string[] $users |
||
| 340 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function toUsersList($users, $message) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Push message to tagged devices. |
||
| 352 | * |
||
| 353 | * @param string|string[] $tags |
||
| 354 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function toTags($tags, $message, $tagsOperation = 'OR') |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Create a batch push. |
||
| 364 | * |
||
| 365 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
| 366 | * @return string|null |
||
| 367 | */ |
||
| 368 | public function createBatch($message) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Batch pushing to a list of users. |
||
| 375 | * |
||
| 376 | * @warning 用户数限制 1000 个。 |
||
| 377 | * |
||
| 378 | * @param int|string $pushId |
||
| 379 | * @param string|string[] $users |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function batchToUsers($pushId, $users) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Batch pushing to a list of devices. |
||
| 391 | * |
||
| 392 | * @param int|string $pushId |
||
| 393 | * @param string|string[] $devices |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | public function batchToDevices($pushId, $devices) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Query all device tokens for the given user. |
||
| 403 | * |
||
| 404 | * @param mixed $user |
||
| 405 | * @return string[]|null |
||
| 406 | */ |
||
| 407 | public function queryDeviceTokensForUser($user) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Query all tags for the given device token. |
||
| 414 | * |
||
| 415 | * @param string $deviceToken |
||
| 416 | * @return string[]|null |
||
| 417 | */ |
||
| 418 | public function queryTagsForDeviceToken($deviceToken) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Query all tags for the given user. |
||
| 425 | * |
||
| 426 | * @param mixed $user |
||
| 427 | * @param array &$deviceTokens |
||
| 428 | * @return array|null |
||
| 429 | */ |
||
| 430 | public function queryTagsForUser($user, &$deviceTokens = null) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Dynamically handle calls to the XingeApp instance. |
||
| 446 | * |
||
| 447 | * @param string $method |
||
| 448 | * @param array $parameters |
||
| 449 | * @return mixed |
||
| 450 | */ |
||
| 451 | public function __call($method, $parameters) |
||
| 455 | } |
||
| 456 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.