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 Protocol091 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 Protocol091, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Protocol091 |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @return array |
||
15 | */ |
||
16 | public function connectionStart($version_major = 0, $version_minor = 9, $server_properties, $mechanisms = 'PLAIN', $locales = 'en_US') |
||
26 | |||
27 | |||
28 | |||
29 | /** |
||
30 | * @param AMQPReader $args |
||
31 | * @return array |
||
32 | */ |
||
33 | View Code Duplication | public static function connectionStartOk($args) |
|
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * @return array |
||
47 | */ |
||
48 | public function connectionSecure($challenge) |
||
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * @param AMQPReader $args |
||
59 | * @return array |
||
60 | */ |
||
61 | public static function connectionSecureOk($args) |
||
67 | |||
68 | |||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | View Code Duplication | public function connectionTune($channel_max = 0, $frame_max = 0, $heartbeat = 0) |
|
81 | |||
82 | |||
83 | |||
84 | /** |
||
85 | * @param AMQPReader $args |
||
86 | * @return array |
||
87 | */ |
||
88 | View Code Duplication | public static function connectionTuneOk($args) |
|
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | */ |
||
102 | View Code Duplication | public function connectionOpen($virtual_host = '/', $capabilities = '', $insist = false) |
|
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * @param AMQPReader $args |
||
115 | * @return array |
||
116 | */ |
||
117 | public static function connectionOpenOk($args) |
||
123 | |||
124 | |||
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | 60 | View Code Duplication | public function connectionClose($reply_code, $reply_text = '', $class_id, $method_id) |
138 | |||
139 | |||
140 | |||
141 | /** |
||
142 | * @param AMQPReader $args |
||
143 | * @return array |
||
144 | */ |
||
145 | public static function connectionCloseOk($args) |
||
150 | |||
151 | |||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | 5 | public function connectionBlocked($reason = '') |
|
162 | |||
163 | |||
164 | |||
165 | /** |
||
166 | * @param AMQPReader $args |
||
167 | * @return array |
||
168 | */ |
||
169 | public static function connectionUnblocked($args) |
||
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * @return array |
||
179 | */ |
||
180 | 65 | public function channelOpen($out_of_band = '') |
|
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * @param AMQPReader $args |
||
191 | * @return array |
||
192 | */ |
||
193 | public static function channelOpenOk($args) |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | 5 | public function channelFlow($active) |
|
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * @param AMQPReader $args |
||
216 | * @return array |
||
217 | */ |
||
218 | public static function channelFlowOk($args) |
||
224 | |||
225 | |||
226 | |||
227 | /** |
||
228 | * @return array |
||
229 | */ |
||
230 | 65 | View Code Duplication | public function channelClose($reply_code, $reply_text = '', $class_id, $method_id) |
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * @param AMQPReader $args |
||
244 | * @return array |
||
245 | */ |
||
246 | public static function channelCloseOk($args) |
||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * @return array |
||
256 | */ |
||
257 | 5 | View Code Duplication | public function accessRequest($realm = '/data', $exclusive = false, $passive = true, $active = true, $write = true, $read = true) |
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * @param AMQPReader $args |
||
269 | * @return array |
||
270 | */ |
||
271 | public static function accessRequestOk($args) |
||
277 | |||
278 | |||
279 | |||
280 | /** |
||
281 | * @return array |
||
282 | */ |
||
283 | 60 | View Code Duplication | public function exchangeDeclare($ticket = 0, $exchange, $type = 'direct', $passive = false, $durable = false, $auto_delete = false, $internal = false, $nowait = false, $arguments = array()) |
293 | |||
294 | |||
295 | |||
296 | /** |
||
297 | * @param AMQPReader $args |
||
298 | * @return array |
||
299 | */ |
||
300 | public static function exchangeDeclareOk($args) |
||
305 | |||
306 | |||
307 | |||
308 | /** |
||
309 | * @return array |
||
310 | */ |
||
311 | 60 | public function exchangeDelete($ticket = 0, $exchange, $if_unused = false, $nowait = false) |
|
319 | |||
320 | |||
321 | |||
322 | /** |
||
323 | * @param AMQPReader $args |
||
324 | * @return array |
||
325 | */ |
||
326 | public static function exchangeDeleteOk($args) |
||
331 | |||
332 | |||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | 5 | View Code Duplication | public function exchangeBind($ticket = 0, $destination, $source, $routing_key = '', $nowait = false, $arguments = array()) |
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * @param AMQPReader $args |
||
353 | * @return array |
||
354 | */ |
||
355 | public static function exchangeBindOk($args) |
||
360 | |||
361 | |||
362 | |||
363 | /** |
||
364 | * @return array |
||
365 | */ |
||
366 | 5 | View Code Duplication | public function exchangeUnbind($ticket = 0, $destination, $source, $routing_key = '', $nowait = false, $arguments = array()) |
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * @param AMQPReader $args |
||
382 | * @return array |
||
383 | */ |
||
384 | public static function exchangeUnbindOk($args) |
||
389 | |||
390 | |||
391 | |||
392 | /** |
||
393 | * @return array |
||
394 | */ |
||
395 | 65 | View Code Duplication | public function queueDeclare($ticket = 0, $queue = '', $passive = false, $durable = false, $exclusive = false, $auto_delete = false, $nowait = false, $arguments = array()) |
404 | |||
405 | |||
406 | |||
407 | /** |
||
408 | * @param AMQPReader $args |
||
409 | * @return array |
||
410 | */ |
||
411 | View Code Duplication | public static function queueDeclareOk($args) |
|
419 | |||
420 | |||
421 | |||
422 | /** |
||
423 | * @return array |
||
424 | */ |
||
425 | 60 | View Code Duplication | public function queueBind($ticket = 0, $queue = '', $exchange, $routing_key = '', $nowait = false, $arguments = array()) |
436 | |||
437 | |||
438 | |||
439 | /** |
||
440 | * @param AMQPReader $args |
||
441 | * @return array |
||
442 | */ |
||
443 | public static function queueBindOk($args) |
||
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * @return array |
||
453 | */ |
||
454 | 5 | View Code Duplication | public function queuePurge($ticket = 0, $queue = '', $nowait = false) |
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * @param AMQPReader $args |
||
467 | * @return array |
||
468 | */ |
||
469 | public static function queuePurgeOk($args) |
||
475 | |||
476 | |||
477 | |||
478 | /** |
||
479 | * @return array |
||
480 | */ |
||
481 | 25 | public function queueDelete($ticket = 0, $queue = '', $if_unused = false, $if_empty = false, $nowait = false) |
|
489 | |||
490 | |||
491 | |||
492 | /** |
||
493 | * @param AMQPReader $args |
||
494 | * @return array |
||
495 | */ |
||
496 | public static function queueDeleteOk($args) |
||
502 | |||
503 | |||
504 | |||
505 | /** |
||
506 | * @return array |
||
507 | */ |
||
508 | 5 | View Code Duplication | public function queueUnbind($ticket = 0, $queue = '', $exchange, $routing_key = '', $arguments = array()) |
518 | |||
519 | |||
520 | |||
521 | /** |
||
522 | * @param AMQPReader $args |
||
523 | * @return array |
||
524 | */ |
||
525 | public static function queueUnbindOk($args) |
||
530 | |||
531 | |||
532 | |||
533 | /** |
||
534 | * @return array |
||
535 | */ |
||
536 | 5 | View Code Duplication | public function basicQos($prefetch_size = 0, $prefetch_count = 0, $global = false) |
544 | |||
545 | |||
546 | |||
547 | /** |
||
548 | * @param AMQPReader $args |
||
549 | * @return array |
||
550 | */ |
||
551 | public static function basicQosOk($args) |
||
556 | |||
557 | |||
558 | |||
559 | /** |
||
560 | * @return array |
||
561 | */ |
||
562 | 30 | View Code Duplication | public function basicConsume($ticket = 0, $queue = '', $consumer_tag = '', $no_local = false, $no_ack = false, $exclusive = false, $nowait = false, $arguments = array()) |
572 | |||
573 | |||
574 | |||
575 | /** |
||
576 | * @param AMQPReader $args |
||
577 | * @return array |
||
578 | */ |
||
579 | public static function basicConsumeOk($args) |
||
585 | |||
586 | |||
587 | |||
588 | /** |
||
589 | * @return array |
||
590 | */ |
||
591 | 30 | View Code Duplication | public function basicCancel($consumer_tag, $nowait = false) |
598 | |||
599 | |||
600 | |||
601 | /** |
||
602 | * @param AMQPReader $args |
||
603 | * @return array |
||
604 | */ |
||
605 | public static function basicCancelOk($args) |
||
611 | |||
612 | |||
613 | |||
614 | /** |
||
615 | * @return array |
||
616 | */ |
||
617 | 60 | View Code Duplication | public function basicPublish($ticket = 0, $exchange = '', $routing_key = '', $mandatory = false, $immediate = false) |
626 | |||
627 | |||
628 | |||
629 | /** |
||
630 | * @return array |
||
631 | */ |
||
632 | View Code Duplication | public function basicReturn($reply_code, $reply_text = '', $exchange, $routing_key) |
|
641 | |||
642 | |||
643 | |||
644 | /** |
||
645 | * @return array |
||
646 | */ |
||
647 | View Code Duplication | public function basicDeliver($consumer_tag, $delivery_tag, $redelivered = false, $exchange, $routing_key) |
|
657 | |||
658 | |||
659 | |||
660 | /** |
||
661 | * @return array |
||
662 | */ |
||
663 | 25 | public function basicGet($ticket = 0, $queue = '', $no_ack = false) |
|
671 | |||
672 | |||
673 | |||
674 | /** |
||
675 | * @param AMQPReader $args |
||
676 | * @return array |
||
677 | */ |
||
678 | View Code Duplication | public static function basicGetOk($args) |
|
688 | |||
689 | |||
690 | |||
691 | /** |
||
692 | * @param AMQPReader $args |
||
693 | * @return array |
||
694 | */ |
||
695 | public static function basicGetEmpty($args) |
||
701 | |||
702 | |||
703 | |||
704 | /** |
||
705 | * @return array |
||
706 | */ |
||
707 | 20 | View Code Duplication | public function basicAck($delivery_tag = 0, $multiple = false) |
714 | |||
715 | |||
716 | |||
717 | /** |
||
718 | * @return array |
||
719 | */ |
||
720 | 5 | View Code Duplication | public function basicReject($delivery_tag, $requeue = true) |
727 | |||
728 | |||
729 | |||
730 | /** |
||
731 | * @return array |
||
732 | */ |
||
733 | public function basicRecoverAsync($requeue = false) |
||
739 | |||
740 | |||
741 | |||
742 | /** |
||
743 | * @return array |
||
744 | */ |
||
745 | 5 | public function basicRecover($requeue = false) |
|
751 | |||
752 | |||
753 | |||
754 | /** |
||
755 | * @param AMQPReader $args |
||
756 | * @return array |
||
757 | */ |
||
758 | public static function basicRecoverOk($args) |
||
763 | |||
764 | |||
765 | |||
766 | /** |
||
767 | * @return array |
||
768 | */ |
||
769 | View Code Duplication | public function basicNack($delivery_tag = 0, $multiple = false, $requeue = true) |
|
776 | |||
777 | |||
778 | |||
779 | /** |
||
780 | * @return array |
||
781 | */ |
||
782 | public function txSelect() |
||
787 | |||
788 | |||
789 | |||
790 | /** |
||
791 | * @param AMQPReader $args |
||
792 | * @return array |
||
793 | */ |
||
794 | public static function txSelectOk($args) |
||
799 | |||
800 | |||
801 | |||
802 | /** |
||
803 | * @return array |
||
804 | */ |
||
805 | public function txCommit() |
||
810 | |||
811 | |||
812 | |||
813 | /** |
||
814 | * @param AMQPReader $args |
||
815 | * @return array |
||
816 | */ |
||
817 | public static function txCommitOk($args) |
||
822 | |||
823 | |||
824 | |||
825 | /** |
||
826 | * @return array |
||
827 | */ |
||
828 | public function txRollback() |
||
833 | |||
834 | |||
835 | |||
836 | /** |
||
837 | * @param AMQPReader $args |
||
838 | * @return array |
||
839 | */ |
||
840 | public static function txRollbackOk($args) |
||
845 | |||
846 | |||
847 | |||
848 | /** |
||
849 | * @return array |
||
850 | */ |
||
851 | public function confirmSelect($nowait = false) |
||
857 | |||
858 | |||
859 | |||
860 | /** |
||
861 | * @param AMQPReader $args |
||
862 | * @return array |
||
863 | */ |
||
864 | public static function confirmSelectOk($args) |
||
869 | |||
870 | } |
||
871 |
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.