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 PricePNRWithBookingClass12 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 PricePNRWithBookingClass12, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 56 | class PricePNRWithBookingClass12 extends BaseWsMessage |
||
| 57 | { |
||
| 58 | public $pnrLocatorData; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var PricePnr12\PaxSegReference |
||
| 62 | */ |
||
| 63 | public $paxSegReference; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var PricePnr12\OverrideInformation |
||
| 67 | */ |
||
| 68 | public $overrideInformation; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var PricePnr12\DateOverride |
||
| 72 | */ |
||
| 73 | public $dateOverride; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var PricePnr12\ValidatingCarrier |
||
| 77 | */ |
||
| 78 | public $validatingCarrier; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var PricePnr12\CityOverride |
||
| 82 | */ |
||
| 83 | public $cityOverride; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var PricePnr12\CurrencyOverride |
||
| 87 | */ |
||
| 88 | public $currencyOverride; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var PricePnr12\TaxDetails[] |
||
| 92 | */ |
||
| 93 | public $taxDetails = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var PricePnr12\DiscountInformation[] |
||
| 97 | */ |
||
| 98 | public $discountInformation = []; |
||
| 99 | /** |
||
| 100 | * @var PricePnr12\PricingFareBase[] |
||
| 101 | */ |
||
| 102 | public $pricingFareBase = []; |
||
| 103 | |||
| 104 | public $flightInformation; |
||
| 105 | |||
| 106 | public $openSegmentsInformation; |
||
| 107 | |||
| 108 | public $bookingClassSelection; |
||
| 109 | |||
| 110 | public $fopInformation; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var CarrierAgreements |
||
| 114 | */ |
||
| 115 | public $carrierAgreements; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var FrequentFlyerInformation |
||
| 119 | */ |
||
| 120 | public $frequentFlyerInformation; |
||
| 121 | |||
| 122 | public $instantPricingOption; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * PricePNRWithBookingClass12 constructor. |
||
| 126 | * |
||
| 127 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt|null $options |
||
| 128 | */ |
||
| 129 | public function __construct($options) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 165 | * @throws OptionNotSupportedException |
||
| 166 | */ |
||
| 167 | protected function checkUnsupportedOptions($options) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 180 | */ |
||
| 181 | protected function loadOverrideOptions($options) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Convert new codes (Price PNR version 13+) to old format |
||
| 202 | * |
||
| 203 | * @param string $optionsTicketType |
||
| 204 | * @return string|null |
||
| 205 | */ |
||
| 206 | protected function convertTicketType($optionsTicketType) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 225 | */ |
||
| 226 | protected function loadCorporateFares($options) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $corporateNegoFare |
||
| 243 | */ |
||
| 244 | protected function loadCorporateNegoFare($corporateNegoFare) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string[] $corporateUniFares |
||
| 254 | */ |
||
| 255 | protected function loadCorporateUniFares($corporateUniFares) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param AwardPricing $awardPricing |
||
| 267 | */ |
||
| 268 | protected function loadAwardPricing($awardPricing) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 276 | */ |
||
| 277 | protected function loadOverrideLocations($options) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 299 | */ |
||
| 300 | protected function loadValidatingCarrier($options) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 309 | * |
||
| 310 | */ |
||
| 311 | protected function loadFareBasis($options) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt $options |
||
| 323 | */ |
||
| 324 | protected function loadTaxOptions($options) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param Tax[] $taxes |
||
| 337 | */ |
||
| 338 | protected function loadTaxes($taxes) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param ExemptTax[] $exemptTaxes |
||
| 347 | */ |
||
| 348 | protected function loadExemptTaxes($exemptTaxes) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string[] $paxDiscountCodes |
||
| 357 | * @param PaxSegRef[] $refs |
||
| 358 | */ |
||
| 359 | protected function loadPaxDiscount($paxDiscountCodes, $refs) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param PaxSegRef[] $references |
||
| 384 | */ |
||
| 385 | protected function loadReferences($references) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param \DateTime|null $pastDate |
||
| 401 | */ |
||
| 402 | protected function loadPastDate($pastDate) |
||
| 408 | } |
||
| 409 |
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.