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 GcmMessage 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 GcmMessage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class GcmMessage extends Message |
||
23 | { |
||
24 | // Default values |
||
25 | const DEFAULT_PRIORITY = 'normal'; |
||
26 | |||
27 | // Max number of recipients for a single message |
||
28 | const MULTICAST_MAX_TOKENS = 1000; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $collapseKey; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $priority; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $contentAvailable; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | private $delayWhileIdle; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $timeToLive; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $restrictedPackageName; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $dryRun; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $data; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $notificationTitle; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | private $notificationBody; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $notificationIcon; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | private $notificationSound; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | private $notificationBadge; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | private $notificationTag; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | private $notificationColor; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | private $notificationClickAction; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | private $notificationTitleLocKey; |
||
114 | |||
115 | /** |
||
116 | * @var array |
||
117 | */ |
||
118 | private $notificationTitleLocArgs; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | private $notificationBodyLocKey; |
||
124 | |||
125 | /** |
||
126 | * @var array |
||
127 | */ |
||
128 | private $notificationBodyLocArgs; |
||
129 | |||
130 | /** |
||
131 | * Constructor |
||
132 | */ |
||
133 | 58 | public function __construct() |
|
142 | |||
143 | /** |
||
144 | * Get full message payload. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 4 | public function getPayload() |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc } |
||
230 | * |
||
231 | * @throws \RuntimeException |
||
232 | */ |
||
233 | 3 | public function setTokens(array $tokens) |
|
241 | |||
242 | /** |
||
243 | * {@inheritdoc } |
||
244 | * |
||
245 | * @throws \RuntimeException |
||
246 | */ |
||
247 | 5 | public function addToken($token) |
|
255 | |||
256 | /** |
||
257 | * Get the value of Collapse Key. |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 5 | public function getCollapseKey() |
|
265 | |||
266 | /** |
||
267 | * Set the value of Collapse Key. |
||
268 | * |
||
269 | * @param string $collapseKey |
||
270 | * |
||
271 | * @return self |
||
272 | */ |
||
273 | 2 | public function setCollapseKey($collapseKey) |
|
279 | |||
280 | /** |
||
281 | * Get the value of Priority. |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | 6 | public function getPriority() |
|
289 | |||
290 | /** |
||
291 | * Set the value of Priority. |
||
292 | * |
||
293 | * @param string $priority 'normal'|'hight' |
||
294 | * |
||
295 | * @return self |
||
296 | */ |
||
297 | 4 | public function setPriority($priority) |
|
307 | |||
308 | /** |
||
309 | * Get the value of Content Available. |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 5 | public function getContentAvailable() |
|
317 | |||
318 | /** |
||
319 | * Set the value of Content Available. |
||
320 | * |
||
321 | * @param bool $contentAvailable |
||
322 | * |
||
323 | * @return self |
||
324 | */ |
||
325 | 2 | public function setContentAvailable($contentAvailable) |
|
331 | |||
332 | /** |
||
333 | * Get the value of Delay While Idle. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | 5 | public function getDelayWhileIdle() |
|
341 | |||
342 | /** |
||
343 | * Set the value of Delay While Idle. |
||
344 | * |
||
345 | * @param bool $delayWhileIdle |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | 2 | public function setDelayWhileIdle($delayWhileIdle) |
|
355 | |||
356 | /** |
||
357 | * Get the value of Time To Live. |
||
358 | * |
||
359 | * @return int |
||
360 | */ |
||
361 | 5 | public function getTimeToLive() |
|
365 | |||
366 | /** |
||
367 | * Set the value of Time To Live. |
||
368 | * |
||
369 | * @param int $timeToLive |
||
370 | * |
||
371 | * @return self |
||
372 | */ |
||
373 | 2 | public function setTimeToLive($timeToLive) |
|
379 | |||
380 | /** |
||
381 | * Get the value of Restricted Package Name. |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | 5 | public function getRestrictedPackageName() |
|
389 | |||
390 | /** |
||
391 | * Set the value of Restricted Package Name. |
||
392 | * |
||
393 | * @param string $restrictedPackageName |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | 2 | public function setRestrictedPackageName($restrictedPackageName) |
|
403 | |||
404 | /** |
||
405 | * Get the value of Dry Run. |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | 5 | public function getDryRun() |
|
413 | |||
414 | /** |
||
415 | * Set the value of Dry Run. |
||
416 | * |
||
417 | * @param bool $dryRun |
||
418 | * |
||
419 | * @return self |
||
420 | */ |
||
421 | 2 | public function setDryRun($dryRun) |
|
427 | |||
428 | /** |
||
429 | * Get the value of Notification Title. |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | 5 | public function getNotificationTitle() |
|
437 | |||
438 | /** |
||
439 | * Set the value of Notification Title. |
||
440 | * |
||
441 | * @param string $notificationTitle |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | 2 | public function setNotificationTitle($notificationTitle) |
|
451 | |||
452 | /** |
||
453 | * Get the value of Notification Body. |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 5 | public function getNotificationBody() |
|
461 | |||
462 | /** |
||
463 | * Set the value of Notification Body. |
||
464 | * |
||
465 | * @param string $notificationBody |
||
466 | * |
||
467 | * @return self |
||
468 | */ |
||
469 | 2 | public function setNotificationBody($notificationBody) |
|
475 | |||
476 | /** |
||
477 | * Get the value of Notification Icon. |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | 5 | public function getNotificationIcon() |
|
485 | |||
486 | /** |
||
487 | * Set the value of Notification Icon. |
||
488 | * |
||
489 | * @param string $notificationIcon |
||
490 | * |
||
491 | * @return self |
||
492 | */ |
||
493 | 2 | public function setNotificationIcon($notificationIcon) |
|
499 | |||
500 | /** |
||
501 | * Get the value of Notification Sound. |
||
502 | * |
||
503 | * @return string |
||
504 | */ |
||
505 | 5 | public function getNotificationSound() |
|
509 | |||
510 | /** |
||
511 | * Set the value of Notification Sound. |
||
512 | * |
||
513 | * @param string $notificationSound |
||
514 | * |
||
515 | * @return self |
||
516 | */ |
||
517 | 2 | public function setNotificationSound($notificationSound) |
|
523 | |||
524 | /** |
||
525 | * Get the value of Notification Badge. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 5 | public function getNotificationBadge() |
|
533 | |||
534 | /** |
||
535 | * Set the value of Notification Badge. |
||
536 | * |
||
537 | * @param string $notificationBadge |
||
538 | * |
||
539 | * @return self |
||
540 | */ |
||
541 | 2 | public function setNotificationBadge($notificationBadge) |
|
547 | |||
548 | /** |
||
549 | * Get the value of Notification Tag. |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | 5 | public function getNotificationTag() |
|
557 | |||
558 | /** |
||
559 | * Set the value of Notification Tag. |
||
560 | * |
||
561 | * @param string $notificationTag |
||
562 | * |
||
563 | * @return self |
||
564 | */ |
||
565 | 2 | public function setNotificationTag($notificationTag) |
|
571 | |||
572 | /** |
||
573 | * Get the value of Notification Color. |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | 5 | public function getNotificationColor() |
|
581 | |||
582 | /** |
||
583 | * Set the value of Notification Color. |
||
584 | * |
||
585 | * @param string $notificationColor |
||
586 | * |
||
587 | * @return self |
||
588 | */ |
||
589 | 2 | public function setNotificationColor($notificationColor) |
|
595 | |||
596 | /** |
||
597 | * Get the value of Notification Click Action. |
||
598 | * |
||
599 | * @return string |
||
600 | */ |
||
601 | 5 | public function getNotificationClickAction() |
|
605 | |||
606 | /** |
||
607 | * Set the value of Notification Click Action. |
||
608 | * |
||
609 | * @param string $notificationClickAction |
||
610 | * |
||
611 | * @return self |
||
612 | */ |
||
613 | 2 | public function setNotificationClickAction($notificationClickAction) |
|
619 | |||
620 | /** |
||
621 | * Get the value of Notification Body Loc Key. |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | 5 | public function getNotificationBodyLocKey() |
|
629 | |||
630 | /** |
||
631 | * Set the value of Notification Body Loc Key. |
||
632 | * |
||
633 | * @param string $notificationBodyLocKey |
||
634 | * |
||
635 | * @return self |
||
636 | */ |
||
637 | 2 | public function setNotificationBodyLocKey($notificationBodyLocKey) |
|
643 | |||
644 | /** |
||
645 | * Get the value of Notification Body Loc Args. |
||
646 | * |
||
647 | * @return array |
||
648 | */ |
||
649 | 5 | public function getNotificationBodyLocArgs() |
|
653 | |||
654 | /** |
||
655 | * Set the value of Notification Body Loc Args. |
||
656 | * |
||
657 | * @param array $notificationBodyLocArgs |
||
658 | * |
||
659 | * @return self |
||
660 | */ |
||
661 | 2 | public function setNotificationBodyLocArgs(array $notificationBodyLocArgs) |
|
667 | |||
668 | /** |
||
669 | * Get the value of Notification Title Loc Key. |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | 5 | public function getNotificationTitleLocKey() |
|
677 | |||
678 | /** |
||
679 | * Set the value of Notification Title Loc Key. |
||
680 | * |
||
681 | * @param string $notificationTitleLocKey |
||
682 | * |
||
683 | * @return self |
||
684 | */ |
||
685 | 2 | public function setNotificationTitleLocKey($notificationTitleLocKey) |
|
691 | |||
692 | /** |
||
693 | * Get the value of Notification Title Loc Args. |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | 5 | public function getNotificationTitleLocArgs() |
|
701 | |||
702 | /** |
||
703 | * Set the value of Notification Title Loc Args. |
||
704 | * |
||
705 | * @param array $notificationTitleLocArgs |
||
706 | * |
||
707 | * @return self |
||
708 | */ |
||
709 | 2 | public function setNotificationTitleLocArgs(array $notificationTitleLocArgs) |
|
715 | |||
716 | /** |
||
717 | * Get the value of Data. |
||
718 | * |
||
719 | * @return array |
||
720 | */ |
||
721 | 6 | public function getData() |
|
725 | |||
726 | /** |
||
727 | * Set the value of Data. |
||
728 | * |
||
729 | * @param array $data |
||
730 | * |
||
731 | * @return self |
||
732 | */ |
||
733 | 7 | public function setData(array $data) |
|
772 | |||
773 | /** |
||
774 | * Set a key/value pair in the data array. |
||
775 | * |
||
776 | * @param string $key |
||
777 | * @param mixed $value |
||
778 | * |
||
779 | * @return self |
||
780 | */ |
||
781 | 2 | View Code Duplication | public function addData($key, $value) |
793 | } |
||
794 |
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.