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:
| 1 | <?php |
||
| 34 | class TravellerInfo |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var ElementManagementPassenger |
||
| 38 | */ |
||
| 39 | public $elementManagementPassenger; |
||
| 40 | /** |
||
| 41 | * Up to 2 PassengerData elements |
||
| 42 | * |
||
| 43 | * @var PassengerData[] |
||
| 44 | */ |
||
| 45 | public $passengerData = []; |
||
| 46 | /** |
||
| 47 | * @todo expand this structure |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | public $enhancedPassengerData = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * TravellerInfo constructor. |
||
| 54 | * |
||
| 55 | * @param TravellerOptions|null $traveller |
||
| 56 | * @param TravellerGroupOptions|null $travellerGroup |
||
| 57 | */ |
||
| 58 | 156 | public function __construct($traveller = null, $travellerGroup = null) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param TravellerGroupOptions $group |
||
| 69 | */ |
||
| 70 | 8 | protected function loadTravellerGroup($group) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param TravellerOptions $traveller |
||
| 84 | */ |
||
| 85 | 152 | protected function loadTraveller(TravellerOptions $traveller) |
|
| 86 | { |
||
| 87 | 152 | $this->elementManagementPassenger = new ElementManagementPassenger( |
|
| 88 | 76 | ElementManagementPassenger::SEG_NAME |
|
| 89 | 76 | ); |
|
| 90 | |||
| 91 | 152 | $this->passengerData[] = new PassengerData($traveller->lastName); |
|
| 92 | |||
| 93 | 152 | if (!is_null($traveller->number)) { |
|
| 94 | 148 | $this->elementManagementPassenger->reference = new Reference( |
|
| 95 | 148 | Reference::QUAL_PASSENGER, |
|
| 96 | 148 | $traveller->number |
|
| 97 | 74 | ); |
|
| 98 | 74 | } |
|
| 99 | |||
| 100 | 152 | if ($traveller->firstName !== null || $traveller->travellerType !== null) { |
|
| 101 | 148 | $this->passengerData[0]->travellerInformation->passenger[] = new Passenger( |
|
| 102 | 148 | $traveller->firstName, |
|
| 103 | 148 | $traveller->travellerType |
|
| 104 | 74 | ); |
|
| 105 | 74 | } |
|
| 106 | |||
| 107 | 152 | if ($traveller->withInfant === true || $traveller->infant !== null) { |
|
| 108 | 16 | $this->addInfant($traveller); |
|
| 109 | 8 | } |
|
| 110 | |||
| 111 | 152 | View Code Duplication | if ($traveller->dateOfBirth instanceof \DateTime) { |
|
|
|||
| 112 | 8 | $this->passengerData[0]->dateOfBirth = new DateOfBirth( |
|
| 113 | 8 | $this->formatDateOfBirth($traveller->dateOfBirth) |
|
| 114 | 4 | ); |
|
| 115 | 4 | } |
|
| 116 | 152 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Add infant |
||
| 120 | * |
||
| 121 | * 3 scenario's: |
||
| 122 | * - infant without additional information |
||
| 123 | * - infant with only first name provided |
||
| 124 | * - infant with first name, last name & date of birth provided. |
||
| 125 | * |
||
| 126 | * @param TravellerOptions $traveller |
||
| 127 | */ |
||
| 128 | 16 | protected function addInfant($traveller) |
|
| 129 | { |
||
| 130 | 16 | $this->passengerData[0]->travellerInformation->traveller->quantity = 2; |
|
| 131 | |||
| 132 | 16 | if ($traveller->withInfant && is_null($traveller->infant)) { |
|
| 133 | 4 | $this->makePassengerIfNeeded(); |
|
| 134 | 4 | $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_NOINFO; |
|
| 135 | 14 | } elseif ($traveller->infant instanceof TravellerOptions) { |
|
| 136 | 12 | if (empty($traveller->infant->lastName)) { |
|
| 137 | 8 | $this->makePassengerIfNeeded(); |
|
| 138 | 8 | $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_GIVEN; |
|
| 139 | |||
| 140 | 8 | $tmpInfantPassenger = new Passenger( |
|
| 141 | 8 | $traveller->infant->firstName, |
|
| 142 | 4 | Passenger::PASST_INFANT |
|
| 143 | 4 | ); |
|
| 144 | |||
| 145 | 8 | $this->passengerData[0]->travellerInformation->passenger[] = $tmpInfantPassenger; |
|
| 146 | 4 | } else { |
|
| 147 | 4 | $this->makePassengerIfNeeded(); |
|
| 148 | 4 | $this->passengerData[0]->travellerInformation->passenger[0]->infantIndicator = Passenger::INF_FULL; |
|
| 149 | |||
| 150 | 4 | $tmpInfant = new PassengerData($traveller->infant->lastName); |
|
| 151 | 4 | $tmpInfant->travellerInformation->passenger[] = new Passenger( |
|
| 152 | 4 | $traveller->infant->firstName, |
|
| 153 | 2 | Passenger::PASST_INFANT |
|
| 154 | 2 | ); |
|
| 155 | |||
| 156 | 4 | View Code Duplication | if ($traveller->infant->dateOfBirth instanceof \DateTime) { |
| 157 | 4 | $tmpInfant->dateOfBirth = new DateOfBirth( |
|
| 158 | 4 | $this->formatDateOfBirth($traveller->infant->dateOfBirth) |
|
| 159 | 2 | ); |
|
| 160 | 2 | } |
|
| 161 | |||
| 162 | 4 | $this->passengerData[] = $tmpInfant; |
|
| 163 | } |
||
| 164 | 6 | } |
|
| 165 | 16 | } |
|
| 166 | |||
| 167 | /** |
||
| 168 | * If there is no passenger node at |
||
| 169 | * $travellerInfo->passengerData[0]->travellerInformation->passenger[0] |
||
| 170 | * create one |
||
| 171 | */ |
||
| 172 | 16 | protected function makePassengerIfNeeded() |
|
| 178 | |||
| 179 | 16 | protected function formatDateOfBirth(\DateTime $dateOfBirth) |
|
| 180 | { |
||
| 190 | } |
||
| 191 |
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.