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 |
||
12 | class XgPusher |
||
13 | { |
||
14 | /** |
||
15 | * The XingeApp instance. |
||
16 | * |
||
17 | * @var \ElfSundae\XgPush\XingeApp |
||
18 | */ |
||
19 | protected $xinge; |
||
20 | |||
21 | /** |
||
22 | * The pusher environment. |
||
23 | * |
||
24 | * 向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $environment = XingeApp::IOSENV_DEV; |
||
29 | |||
30 | /** |
||
31 | * The key of custom payload. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $customKey; |
||
36 | |||
37 | /** |
||
38 | * Xinge account prefix. |
||
39 | * |
||
40 | * @warning 信鸽不允许使用简单的账号,例如纯数字的id。 |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $accountPrefix; |
||
45 | |||
46 | /** |
||
47 | * Create a new instance. |
||
48 | * |
||
49 | * @param string $appKey |
||
50 | * @param string $appSecret |
||
51 | */ |
||
52 | public function __construct($appKey, $appSecret) |
||
56 | |||
57 | /** |
||
58 | * Get the XingeApp instance. |
||
59 | * |
||
60 | * @return \ElfSundae\XgPush\XingeApp |
||
61 | */ |
||
62 | public function xinge() |
||
66 | |||
67 | /** |
||
68 | * Get the app key. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getAppKey() |
||
76 | |||
77 | /** |
||
78 | * Get the app secret. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getAppSecret() |
||
86 | |||
87 | /** |
||
88 | * Get the pusher environment. |
||
89 | * |
||
90 | * @return int |
||
91 | */ |
||
92 | public function getEnvironment() |
||
96 | |||
97 | /** |
||
98 | * Set the pusher environment. |
||
99 | * |
||
100 | * @param mixed $env |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setEnvironment($env) |
||
115 | |||
116 | /** |
||
117 | * Get the key of custom payload. |
||
118 | * |
||
119 | * @return string|null |
||
120 | */ |
||
121 | public function getCustomKey() |
||
125 | |||
126 | /** |
||
127 | * Set the key of custom payload. |
||
128 | * |
||
129 | * @param string|null $key |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setCustomKey($key) |
||
138 | |||
139 | /** |
||
140 | * Get the account prefix. |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getAccountPrefix() |
||
148 | |||
149 | /** |
||
150 | * Set the account prefix. |
||
151 | * |
||
152 | * @param string $prefix |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setAccountPrefix($prefix) |
||
161 | |||
162 | /** |
||
163 | * Determine if the Xinge response is success. |
||
164 | * |
||
165 | * @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 |
||
166 | * |
||
167 | * @param mixed $response |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function succeed($response) |
||
174 | |||
175 | /** |
||
176 | * Get the code of Xinge response. |
||
177 | * |
||
178 | * @param mixed $response |
||
179 | * @return int|null |
||
180 | */ |
||
181 | public function code($response) |
||
187 | |||
188 | /** |
||
189 | * Get the error message of Xinge response. |
||
190 | * |
||
191 | * @param mixed $response |
||
192 | * @return string|null |
||
193 | */ |
||
194 | public function message($response) |
||
200 | |||
201 | /** |
||
202 | * Get the result data of Xinge response. |
||
203 | * |
||
204 | * @param mixed $response |
||
205 | * @param string $key |
||
206 | * @param mixed $default |
||
207 | * @return mixed |
||
208 | */ |
||
209 | public function result($response, $key = null, $default = null) |
||
215 | |||
216 | /** |
||
217 | * Encode the custom data. |
||
218 | * |
||
219 | * @param mixed $data |
||
220 | * @return array |
||
221 | */ |
||
222 | public function encodeCustomData($data) |
||
230 | |||
231 | /** |
||
232 | * Get Xinge account for the given user. |
||
233 | * |
||
234 | * @param mixed $user |
||
235 | * @return string |
||
236 | */ |
||
237 | public function accountForUser($user) |
||
251 | |||
252 | /** |
||
253 | * Get Xinge accounts for users. |
||
254 | * |
||
255 | * @param array $users |
||
256 | * @return array |
||
257 | */ |
||
258 | public function accountsForUsers(array $users) |
||
262 | |||
263 | /** |
||
264 | * Creates a new MessageIOS instance. |
||
265 | * |
||
266 | * @param string $alert |
||
267 | * @param mixed $custom |
||
268 | * @param int $badge |
||
269 | * @param string $sound |
||
270 | * @return \ElfSundae\XgPush\MessageIOS |
||
271 | */ |
||
272 | public function createIOSMessage($alert = '', $custom = null, $badge = 1, $sound = 'default') |
||
286 | |||
287 | /** |
||
288 | * Create a new Message instance. |
||
289 | * |
||
290 | * @param string $title |
||
291 | * @param string $content |
||
292 | * @param mixed $custom |
||
293 | * @param int $type |
||
294 | * @return \ElfSundae\XgPush\Message |
||
295 | */ |
||
296 | public function createAndroidMessage($title = '', $content = '', $custom = null, $type = Message::TYPE_MESSAGE) |
||
306 | |||
307 | /** |
||
308 | * Create a new Message instance for notification. |
||
309 | * The default action is opening app. |
||
310 | * |
||
311 | * @param string $title |
||
312 | * @param string $content |
||
313 | * @param mixed $custom |
||
314 | * @return \ElfSundae\XgPush\Message |
||
315 | */ |
||
316 | public function createAndroidNotification($title = '', $content = '', $custom = null) |
||
327 | |||
328 | /** |
||
329 | * Push message to a device. |
||
330 | * |
||
331 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
332 | * @param string $deviceToken |
||
333 | * @return array |
||
334 | */ |
||
335 | public function toDevice($message, $deviceToken) |
||
339 | |||
340 | /** |
||
341 | * Push message to all devices. |
||
342 | * |
||
343 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
344 | * @return array |
||
345 | */ |
||
346 | public function toAllDevices($message) |
||
350 | |||
351 | /** |
||
352 | * Push message to users. |
||
353 | * |
||
354 | * @warning 用户数限制 100 个。 |
||
355 | * |
||
356 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
357 | * @param mixed $users |
||
358 | * @return array |
||
359 | */ |
||
360 | public function toUser($message, $users) |
||
371 | |||
372 | /** |
||
373 | * Push message to tagged devices. |
||
374 | * |
||
375 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
376 | * @param string|string[] $tags |
||
377 | * @param string $tagsOperation 'OR', 'AND' |
||
378 | * @return array |
||
379 | */ |
||
380 | public function toTags($message, $tags, $tagsOperation = 'OR') |
||
384 | |||
385 | /** |
||
386 | * Create a batch push. |
||
387 | * |
||
388 | * @param \ElfSundae\XgPush\Message|\ElfSundae\XgPush\MessageIOS $message |
||
389 | * @return string|null |
||
390 | */ |
||
391 | public function createBatch($message) |
||
395 | |||
396 | /** |
||
397 | * Batch pushing to a list of users. |
||
398 | * |
||
399 | * @warning 用户数限制 1000 个。 |
||
400 | * |
||
401 | * @param int|string $pushId |
||
402 | * @param mixed $users |
||
403 | * @return array |
||
404 | */ |
||
405 | public function batchToUsers($pushId, $users) |
||
412 | |||
413 | /** |
||
414 | * Batch pushing to a list of devices. |
||
415 | * |
||
416 | * @warning 设备数限制 1000 个。 |
||
417 | * |
||
418 | * @param int|string $pushId |
||
419 | * @param mixed $deviceTokens |
||
420 | * @return array |
||
421 | */ |
||
422 | public function batchToDevices($pushId, $deviceTokens) |
||
429 | |||
430 | /** |
||
431 | * Query group pushing status. |
||
432 | * |
||
433 | * @param mixed $pushIds |
||
434 | * @return array |
||
435 | */ |
||
436 | public function queryPushStatus($pushIds) |
||
444 | |||
445 | /** |
||
446 | * Cancel a timed pushing task that has not been pushed. |
||
447 | * |
||
448 | * @param string $pushId |
||
449 | * @return array |
||
450 | */ |
||
451 | public function cancelTimedPush($pushId) |
||
455 | |||
456 | /** |
||
457 | * Query all device tokens for the given user. |
||
458 | * |
||
459 | * @param mixed $user |
||
460 | * @return string[] |
||
461 | */ |
||
462 | public function queryDeviceTokensForUser($user) |
||
466 | |||
467 | /** |
||
468 | * Delete device tokens for the given user. |
||
469 | * |
||
470 | * @param mixed $user |
||
471 | * @param string|string[] $deviceTokens |
||
472 | * @return bool |
||
473 | */ |
||
474 | public function deleteDeviceTokensForUser($user, $deviceTokens = null) |
||
492 | |||
493 | /** |
||
494 | * Query count of registered devices. |
||
495 | * |
||
496 | * @return int |
||
497 | */ |
||
498 | public function queryCountOfDevices() |
||
502 | |||
503 | /** |
||
504 | * Query info for the given device token. |
||
505 | * |
||
506 | * @param string $deviceToken |
||
507 | * @return array |
||
508 | */ |
||
509 | public function queryDeviceTokenInfo($deviceToken) |
||
513 | |||
514 | /** |
||
515 | * Query count of registered tokens for the given tag. |
||
516 | * |
||
517 | * @param string $tag |
||
518 | * @return int |
||
519 | */ |
||
520 | public function queryCountOfDeviceTokensForTag($tag) |
||
524 | |||
525 | /** |
||
526 | * Query tags. |
||
527 | * |
||
528 | * @return array |
||
529 | */ |
||
530 | public function queryTags($start = 0, $limit = 100) |
||
534 | |||
535 | /** |
||
536 | * Query all tags for the given device token. |
||
537 | * |
||
538 | * @param string $deviceToken |
||
539 | * @return string[] |
||
540 | */ |
||
541 | public function queryTagsForDeviceToken($deviceToken) |
||
545 | |||
546 | /** |
||
547 | * Query all tags for the given user. |
||
548 | * |
||
549 | * @param mixed $user |
||
550 | * @param array &$deviceTokens |
||
551 | * @return array |
||
552 | */ |
||
553 | public function queryTagsForUser($user, &$deviceTokens = null) |
||
564 | |||
565 | /** |
||
566 | * Add tags for device tokens. |
||
567 | * |
||
568 | * @warning 每次最多设置 20 对。 |
||
569 | * |
||
570 | * @param \ElfSundae\XgPush\TagTokenPair[] $tagTokenPairs |
||
571 | * @return bool |
||
572 | */ |
||
573 | public function addTags($tagTokenPairs) |
||
577 | |||
578 | /** |
||
579 | * Add tags for the given device token. |
||
580 | * |
||
581 | * @param string $deviceToken |
||
582 | * @param mixed $tags |
||
583 | * @return bool |
||
584 | */ |
||
585 | public function addTagsForDeviceToken($deviceToken, $tags) |
||
591 | |||
592 | /** |
||
593 | * Add tags for the given user. |
||
594 | * |
||
595 | * @param mixed $user |
||
596 | * @param mixed $tags |
||
597 | * @return bool |
||
598 | */ |
||
599 | public function addTagsForUser($user, $tags) |
||
608 | |||
609 | /** |
||
610 | * Remove tags for device tokens. |
||
611 | * |
||
612 | * @warning 每次最多删除 20 对。 |
||
613 | * |
||
614 | * @param \ElfSundae\XgPush\TagTokenPair[] $tagTokenPairs |
||
615 | * @return bool |
||
616 | */ |
||
617 | public function removeTags($tagTokenPairs) |
||
621 | |||
622 | /** |
||
623 | * Remove tags for the given device token. |
||
624 | * |
||
625 | * @param string $deviceToken |
||
626 | * @param mixed $tags |
||
627 | * @return bool |
||
628 | */ |
||
629 | public function removeTagsForDeviceToken($deviceToken, $tags) |
||
635 | |||
636 | /** |
||
637 | * Remove tags for the given user. |
||
638 | * |
||
639 | * @param mixed $user |
||
640 | * @param mixed $tags |
||
641 | * @return bool |
||
642 | */ |
||
643 | public function removeTagsForUser($user, $tags) |
||
652 | |||
653 | /** |
||
654 | * Set tags for the given device token. |
||
655 | * |
||
656 | * @param string $deviceToken |
||
657 | * @param mixed $tags |
||
658 | * @return bool |
||
659 | */ |
||
660 | public function setTagsForDeviceToken($deviceToken, $tags) |
||
677 | |||
678 | /** |
||
679 | * Set tags for the given user. |
||
680 | * |
||
681 | * @param mixed $user |
||
682 | * @param mixed $tags |
||
683 | * @return bool |
||
684 | */ |
||
685 | public function setTagsForUser($user, $tags) |
||
715 | |||
716 | /** |
||
717 | * Get parameter as array. |
||
718 | * |
||
719 | * @param array $args |
||
720 | * @param int $offset |
||
721 | * @return array |
||
722 | */ |
||
723 | protected function getParameterAsArray(array $args, $offset = 0) |
||
727 | |||
728 | /** |
||
729 | * Create array of TagTokenPair. |
||
730 | * |
||
731 | * @warning $tags 和 $tokens 一个是数组,另一个是字符串 |
||
732 | * |
||
733 | * @param string|string[] $tags |
||
734 | * @param string|string[] $tokens |
||
735 | * @return \ElfSundae\XgPush\TagTokenPair[] |
||
736 | */ |
||
737 | protected function createTagTokenPairs($tags, $tokens) |
||
750 | |||
751 | /** |
||
752 | * Dynamically handle calls to the XingeApp instance. |
||
753 | * |
||
754 | * @param string $method |
||
755 | * @param array $parameters |
||
756 | * @return mixed |
||
757 | */ |
||
758 | public function __call($method, $parameters) |
||
762 | } |
||
763 |
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.