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 PricePNRWithBookingClass13 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 PricePNRWithBookingClass13, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | class PricePNRWithBookingClass13 extends BasePricingMessage |
||
61 | { |
||
62 | /** |
||
63 | * @var PricePnr13\PricingOptionGroup[] |
||
64 | */ |
||
65 | public $pricingOptionGroup = []; |
||
66 | |||
67 | /** |
||
68 | * PricePNRWithBookingClass13 constructor. |
||
69 | * |
||
70 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt|InformativePriceOpt|null $options |
||
71 | 120 | * @throws MessageVersionUnsupportedException |
|
72 | */ |
||
73 | 120 | public function __construct($options) |
|
79 | |||
80 | /** |
||
81 | * Load an array of PricingOptionGroup objects from the Pricing request options. |
||
82 | * |
||
83 | * Extracted because this method is also used in the InformativePricingWithoutPnr messages. |
||
84 | * |
||
85 | * @param FarePricePnrWithBookingClassOptions|LowerFareOpt|LowestFareOpt|InformativePriceOpt $options |
||
86 | 155 | * @return PricingOptionGroup[] |
|
87 | */ |
||
88 | 155 | public static function loadPricingOptionsFromRequestOptions($options) |
|
198 | |||
199 | 155 | /** |
|
200 | * @param string[] $overrideOptions |
||
201 | 155 | * @param PricingOptionGroup[] $priceOptions |
|
202 | 40 | * @return PricingOptionGroup[] |
|
203 | 40 | */ |
|
204 | 16 | View Code Duplication | protected static function makeOverrideOptions($overrideOptions, $priceOptions) |
216 | |||
217 | 155 | /** |
|
218 | * @param string[] $overrideOptionsWithCriteria |
||
219 | 155 | * @param PricingOptionGroup[] $priceOptions |
|
220 | 5 | * @return PricingOptionGroup[] |
|
221 | 5 | */ |
|
222 | 2 | protected static function makeOverrideOptionsWithCriteria($overrideOptionsWithCriteria, $priceOptions) |
|
234 | 155 | ||
235 | /** |
||
236 | 155 | * @param string|null $validatingCarrier |
|
237 | 40 | * @return PricePnr13\PricingOptionGroup[] |
|
238 | */ |
||
239 | 40 | View Code Duplication | protected static function makePricingOptionForValidatingCarrier($validatingCarrier) |
253 | 155 | ||
254 | /** |
||
255 | 155 | * @param string|null $currency |
|
256 | 40 | * @return PricePnr13\PricingOptionGroup[] |
|
257 | */ |
||
258 | 40 | View Code Duplication | protected static function makePricingOptionForCurrencyOverride($currency) |
272 | |||
273 | 155 | ||
274 | /** |
||
275 | 155 | * @param FareBasis[] $pricingsFareBasis |
|
276 | 155 | * @return PricePnr13\PricingOptionGroup[] |
|
277 | 20 | */ |
|
278 | View Code Duplication | protected static function makePricingOptionFareBasisOverride($pricingsFareBasis) |
|
303 | 155 | ||
304 | /** |
||
305 | 155 | * Load fare-family pricing option and return it. |
|
306 | * |
||
307 | 155 | * @param string $fareFamily input fare-family, e.g. "CLASSIC" |
|
308 | 5 | * @return PricePnr13\PricingOptionGroup[] |
|
309 | 5 | */ |
|
310 | protected static function makePricingOptionFareFamilyOverride($fareFamily) |
||
336 | |||
337 | /** |
||
338 | * Load corporate negofare |
||
339 | * |
||
340 | * @param string|null $corporateNegoFare |
||
341 | * @return PricingOptionGroup[] |
||
342 | */ |
||
343 | View Code Duplication | protected static function loadCorpNegoFare($corporateNegoFare) |
|
344 | { |
||
345 | 155 | $opt = []; |
|
346 | |||
347 | 155 | if ($corporateNegoFare !== null) { |
|
348 | $po = new PricingOptionGroup(PricingOptionKey::OPTION_CORPORATE_NEGOTIATED_FARES); |
||
349 | 155 | ||
350 | 10 | $po->optionDetail = new OptionDetail($corporateNegoFare); |
|
351 | 10 | ||
352 | 10 | $opt[] = $po; |
|
353 | } |
||
354 | 10 | ||
355 | 5 | return $opt; |
|
356 | 2 | } |
|
357 | 4 | ||
358 | /** |
||
359 | 155 | * Load corporate unifares |
|
360 | * |
||
361 | * @param string[] $corporateUniFares |
||
362 | * @param AwardPricing|null $awardPricing |
||
363 | * @return PricingOptionGroup[] |
||
364 | */ |
||
365 | protected static function loadCorpUniFares($corporateUniFares, $awardPricing) |
||
381 | |||
382 | /** |
||
383 | * @param AwardPricing $awardPricing |
||
384 | * @return PricingOptionGroup |
||
385 | */ |
||
386 | View Code Duplication | protected static function loadAwardPricing($awardPricing) |
|
399 | 5 | ||
400 | 5 | /** |
|
401 | 2 | * Load OB Fees |
|
402 | * |
||
403 | 5 | * @param ObFee[] $obFees |
|
404 | 2 | * @param PaxSegRef[] $obFeeRefs |
|
405 | * @return PricingOptionGroup[] |
||
406 | 155 | */ |
|
407 | View Code Duplication | protected static function loadObFees($obFees, $obFeeRefs) |
|
428 | 8 | ||
429 | /** |
||
430 | 20 | * @param string[] $paxDiscount |
|
431 | 8 | * @param PaxSegRef[] $paxDiscountCodeRefs |
|
432 | * @return PricingOptionGroup[] |
||
433 | 155 | */ |
|
434 | View Code Duplication | protected static function loadPaxDiscount($paxDiscount, $paxDiscountCodeRefs) |
|
455 | |||
456 | 155 | /** |
|
457 | 5 | * @param string|null $posOverride |
|
458 | * @param string|null $potOverride |
||
459 | 5 | * @return PricingOptionGroup[] |
|
460 | 5 | */ |
|
461 | 2 | View Code Duplication | protected static function loadPointOverrides($posOverride, $potOverride) |
489 | |||
490 | /** |
||
491 | 155 | * @param string|null $pricingLogic |
|
492 | * @return PricingOptionGroup[] |
||
493 | 155 | */ |
|
494 | View Code Duplication | protected static function loadPricingLogic($pricingLogic) |
|
506 | |||
507 | /** |
||
508 | * @param string|null $ticketType |
||
509 | * @return PricingOptionGroup[] |
||
510 | 155 | */ |
|
511 | View Code Duplication | protected static function loadTicketType($ticketType) |
|
525 | 2 | ||
526 | 2 | /** |
|
527 | 2 | * @param Tax[] $taxes |
|
528 | 5 | * @return PricingOptionGroup[] |
|
529 | 2 | */ |
|
530 | View Code Duplication | protected static function loadTaxes($taxes) |
|
553 | 2 | ||
554 | /** |
||
555 | 155 | * @param ExemptTax[] $exemptTaxes |
|
556 | * @return PricingOptionGroup[] |
||
557 | */ |
||
558 | View Code Duplication | protected static function loadExemptTaxes($exemptTaxes) |
|
577 | 155 | ||
578 | /** |
||
579 | * @param \DateTime|null $pastDate |
||
580 | * @return PricingOptionGroup[] |
||
581 | */ |
||
582 | View Code Duplication | protected static function loadPastDate($pastDate) |
|
599 | |||
600 | |||
601 | /** |
||
602 | * @param FormOfPayment[] $formOfPayment |
||
603 | * @return PricingOptionGroup[] |
||
604 | 155 | */ |
|
605 | View Code Duplication | protected static function loadFormOfPayment($formOfPayment) |
|
606 | 155 | { |
|
607 | $opt = []; |
||
608 | 155 | ||
609 | 5 | if (!empty($formOfPayment)) { |
|
610 | $po = new PricingOptionGroup(PricingOptionKey::OPTION_FORM_OF_PAYMENT); |
||
611 | 5 | ||
612 | $po->formOfPaymentInformation = new FormOfPaymentInformation($formOfPayment); |
||
613 | 5 | ||
614 | 2 | $opt[] = $po; |
|
615 | } |
||
616 | 155 | ||
617 | return $opt; |
||
618 | } |
||
619 | |||
620 | /** |
||
621 | * @param PaxSegRef[] $references |
||
622 | * @return PricingOptionGroup[] |
||
623 | */ |
||
624 | View Code Duplication | protected static function loadReferences($references) |
|
625 | { |
||
626 | $opt = []; |
||
627 | |||
628 | if (!empty($references)) { |
||
629 | $po = new PricingOptionGroup(PricingOptionKey::OPTION_PAX_SEGMENT_TST_SELECTION); |
||
630 | |||
631 | $po->paxSegTstReference = new PaxSegTstReference($references); |
||
632 | |||
633 | $opt[] = $po; |
||
634 | } |
||
635 | |||
636 | return $opt; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Load ZAP-Off |
||
641 | * |
||
642 | * @param ZapOff[] $zapOffs |
||
643 | * @return PricingOptionGroup[] |
||
644 | */ |
||
645 | protected static function loadZapOffs($zapOffs) |
||
667 | } |
||
668 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.