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 Base 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 Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Base extends BaseUtils |
||
41 | { |
||
42 | /** |
||
43 | * Analyze the response from the server and throw an exception when an error has been detected. |
||
44 | * |
||
45 | * @param SendResult $sendResult The Send Result from the Session Handler |
||
46 | * @param string $messageName The message that was called |
||
47 | * |
||
48 | * @throws Exception |
||
49 | * @throws \RuntimeException |
||
50 | * @return Result |
||
51 | */ |
||
52 | public function analyzeResponse($sendResult, $messageName) |
||
66 | |||
67 | /** |
||
68 | * Analysing a Security_Authenticate |
||
69 | * |
||
70 | * @param SendResult $response Security_Authenticate result |
||
71 | * @return Result |
||
72 | */ |
||
73 | protected function analyzeSecurityAuthenticateResponse($response) |
||
77 | |||
78 | /** |
||
79 | * Analysing a Security_Authenticate |
||
80 | * |
||
81 | * @param SendResult $response Security_Authenticate result |
||
82 | * @return Result |
||
83 | */ |
||
84 | protected function analyzeSecuritySignOutResponse($response) |
||
88 | |||
89 | /** |
||
90 | * Unknown response for Command_Cryptic because you need to analyse the cryptic response yourself |
||
91 | * |
||
92 | * @param SendResult $response |
||
93 | * @return Result |
||
94 | */ |
||
95 | protected function analyzeCommandCrypticResponse($response) |
||
105 | |||
106 | View Code Duplication | protected function analyzeAirMultiAvailabilityResponse($response) |
|
132 | |||
133 | protected function analyzeAirSellFromRecommendationResponse($response) |
||
161 | |||
162 | /** |
||
163 | * @param SendResult $response |
||
164 | * @return Result |
||
165 | */ |
||
166 | protected function analyzeAirFlightInfoResponse($response) |
||
196 | |||
197 | /** |
||
198 | * @param SendResult $response |
||
199 | * @return Result |
||
200 | */ |
||
201 | protected function analyzeAirRetrieveSeatMapResponse($response) |
||
261 | |||
262 | /** |
||
263 | * Analysing a PNR_Retrieve response |
||
264 | * |
||
265 | * @param SendResult $response PNR_Retrieve result |
||
266 | * @return Result |
||
267 | */ |
||
268 | protected function analyzePnrRetrieveResponse($response) |
||
272 | |||
273 | /** |
||
274 | * @param SendResult $response PNR_AddMultiElements result |
||
275 | * @return Result |
||
276 | */ |
||
277 | protected function analyzePnrAddMultiElementsResponse($response) |
||
281 | |||
282 | /** |
||
283 | * @param SendResult $response PNR_Cancel result |
||
284 | * @return Result |
||
285 | */ |
||
286 | protected function analyzePnrCancelResponse($response) |
||
290 | |||
291 | /** |
||
292 | * @param SendResult $response Pnr_RetrieveAndDisplay response |
||
293 | * @return Result |
||
294 | * @throws Exception |
||
295 | */ |
||
296 | protected function analyzePnrRetrieveAndDisplayResponse($response) |
||
300 | |||
301 | /** |
||
302 | * Analysing a PNR_TransferOwnershipReply |
||
303 | * |
||
304 | * @param SendResult $response PNR_TransferOwnership response |
||
305 | * @return Result |
||
306 | * @throws Exception |
||
307 | */ |
||
308 | protected function analyzePNRTransferOwnershipResponse($response) |
||
312 | |||
313 | /** |
||
314 | * Analysing a PNR_DisplayHistoryReply |
||
315 | * |
||
316 | * @param SendResult $response PNR_DisplayHistoryReply result |
||
317 | * @return Result |
||
318 | * @throws Exception |
||
319 | */ |
||
320 | protected function analyzePnrDisplayHistoryResponse($response) |
||
343 | |||
344 | /** |
||
345 | * @param SendResult $response Queue_RemoveItem response |
||
346 | * @return Result |
||
347 | * @throws Exception |
||
348 | */ |
||
349 | protected function analyzeQueueRemoveItemResponse($response) |
||
353 | |||
354 | /** |
||
355 | * @param SendResult $response Queue_MoveItem response |
||
356 | * @return Result |
||
357 | * @throws Exception |
||
358 | */ |
||
359 | protected function analyzeQueueMoveItemResponse($response) |
||
363 | |||
364 | /** |
||
365 | * @param SendResult $response Queue_PlacePNR response |
||
366 | * @return Result |
||
367 | * @throws Exception |
||
368 | */ |
||
369 | protected function analyzeQueuePlacePNRResponse($response) |
||
373 | |||
374 | /** |
||
375 | * @param SendResult $response Queue_List result |
||
376 | * @return Result |
||
377 | * @throws Exception |
||
378 | */ |
||
379 | protected function analyzeQueueListResponse($response) |
||
383 | |||
384 | /** |
||
385 | * Analyze a generic Queue response |
||
386 | * |
||
387 | * @param SendResult $response Queue_*Reply result |
||
388 | * @return Result |
||
389 | * @throws Exception |
||
390 | */ |
||
391 | protected function analyzeGenericQueueResponse($response) |
||
410 | |||
411 | |||
412 | /** |
||
413 | * @param SendResult $response Fare_PricePNRWithBookingClass result |
||
414 | * @return Result |
||
415 | * @throws Exception |
||
416 | */ |
||
417 | View Code Duplication | protected function analyzeFarePricePNRWithBookingClassResponse($response) |
|
447 | |||
448 | /** |
||
449 | * @param SendResult $response Fare_PricePNRWithLowerFares result |
||
450 | * @return Result |
||
451 | * @throws Exception |
||
452 | */ |
||
453 | protected function analyzeFarePricePNRWithLowerFaresResponse($response) |
||
457 | |||
458 | /** |
||
459 | * @param SendResult $response Fare_PricePNRWithLowestFare result |
||
460 | * @return Result |
||
461 | * @throws Exception |
||
462 | */ |
||
463 | protected function analyzeFarePricePNRWithLowestFareResponse($response) |
||
467 | |||
468 | /** |
||
469 | * @param SendResult $response |
||
470 | * @return Result |
||
471 | */ |
||
472 | protected function analyzeFareMasterPricerCalendarResponse($response) |
||
476 | |||
477 | /** |
||
478 | * @param SendResult $response |
||
479 | * @return Result |
||
480 | */ |
||
481 | protected function analyzeFareMasterPricerTravelBoardSearchResponse($response) |
||
509 | |||
510 | /** |
||
511 | * @param SendResult $response |
||
512 | * @return Result |
||
513 | */ |
||
514 | protected function analyzeFareConvertCurrencyResponse($response) |
||
518 | |||
519 | /** |
||
520 | * @param SendResult $response |
||
521 | * @return Result |
||
522 | */ |
||
523 | protected function analyzeFareCheckRulesResponse($response) |
||
527 | |||
528 | /** |
||
529 | * @param SendResult $response |
||
530 | * @return Result |
||
531 | */ |
||
532 | protected function analyzeFareInformativePricingWithoutPNRResponse($response) |
||
536 | |||
537 | /** |
||
538 | * @param SendResult $response |
||
539 | * @return Result |
||
540 | */ |
||
541 | protected function analyzeFareInformativeBestPricingWithoutPNRResponse($response) |
||
545 | |||
546 | /** |
||
547 | * @param SendResult $response |
||
548 | * @return Result |
||
549 | */ |
||
550 | protected function analyzeDocIssuanceIssueTicketResponse($response) |
||
554 | |||
555 | /** |
||
556 | * @param SendResult $response Ticket_DeleteTST result |
||
557 | * @return Result |
||
558 | */ |
||
559 | protected function analyzeTicketDisplayTSTResponse($response) |
||
563 | |||
564 | /** |
||
565 | * @param SendResult $response Ticket_DeleteTST result |
||
566 | * @return Result |
||
567 | */ |
||
568 | protected function analyzeTicketDeleteTSTResponse($response) |
||
572 | |||
573 | /** |
||
574 | * @param SendResult $response Ticket_CreateTSTFromPricing result |
||
575 | * @return Result |
||
576 | */ |
||
577 | View Code Duplication | protected function analyzeTicketCreateTSTFromPricingResponse($response) |
|
604 | |||
605 | /** |
||
606 | * @param SendResult $response Ticket_CreateTSMFromPricing result |
||
607 | * @return Result |
||
608 | */ |
||
609 | protected function analyzeTicketCreateTSMFromPricingResponse($response) |
||
613 | |||
614 | /** |
||
615 | * @param SendResult $response |
||
616 | * @return Result |
||
617 | */ |
||
618 | protected function analyzeOfferConfirmCarOfferResponse($response) |
||
622 | |||
623 | /** |
||
624 | * @param SendResult $response |
||
625 | * @return Result |
||
626 | */ |
||
627 | protected function analyzeOfferConfirmHotelOfferResponse($response) |
||
652 | |||
653 | /** |
||
654 | * Offer_ConfirmAirOffer |
||
655 | * |
||
656 | * @param SendResult $response |
||
657 | * @return Result |
||
658 | */ |
||
659 | protected function analyzeOfferConfirmAirOfferResponse($response) |
||
663 | |||
664 | /** |
||
665 | * Offer_VerifyOffer |
||
666 | * |
||
667 | * @param SendResult $response |
||
668 | * @return Result |
||
669 | */ |
||
670 | protected function analyzeOfferVerifyOfferResponse($response) |
||
674 | |||
675 | /** |
||
676 | * Offer_CreateOffer |
||
677 | * |
||
678 | * @param SendResult $response |
||
679 | * @return Result |
||
680 | */ |
||
681 | protected function analyzeOfferCreateOfferResponse($response) |
||
685 | |||
686 | /** |
||
687 | * @param SendResult $response |
||
688 | * @return Result |
||
689 | */ |
||
690 | protected function analyzeMiniRuleGetFromPricingRecResponse($response) |
||
720 | |||
721 | /** |
||
722 | * @param SendResult $response |
||
723 | * @return Result |
||
724 | */ |
||
725 | protected function analyzeMiniRuleGetFromPricingResponse($response) |
||
729 | |||
730 | /** |
||
731 | * @param SendResult $response |
||
732 | * @return Result |
||
733 | */ |
||
734 | protected function analyzeInfoEncodeDecodeCityResponse($response) |
||
738 | |||
739 | /** |
||
740 | * @param SendResult $response |
||
741 | * @return Result |
||
742 | */ |
||
743 | protected function analyzePriceXplorerExtremeSearchResponse($response) |
||
747 | |||
748 | /** |
||
749 | * @param SendResult $response |
||
750 | * @return Result |
||
751 | */ |
||
752 | protected function analyzeSalesReportsDisplayQueryReportResponse($response) |
||
756 | |||
757 | /** |
||
758 | * @param SendResult $response |
||
759 | * @return Result |
||
760 | */ |
||
761 | protected function analyzeServiceIntegratedPricingResponse($response) |
||
765 | } |
||
766 |
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.