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 AddMultiElements 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 AddMultiElements, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class AddMultiElements extends BaseWsMessage |
||
55 | { |
||
56 | /** |
||
57 | * @var AddMultiElements\ReservationInfo |
||
58 | */ |
||
59 | public $reservationInfo; |
||
60 | /** |
||
61 | * @var AddMultiElements\PnrActions |
||
62 | */ |
||
63 | public $pnrActions; |
||
64 | /** |
||
65 | * @var AddMultiElements\TravellerInfo[] |
||
66 | */ |
||
67 | public $travellerInfo = []; |
||
68 | /** |
||
69 | * @var AddMultiElements\OriginDestinationDetails[] |
||
70 | */ |
||
71 | public $originDestinationDetails = []; |
||
72 | /** |
||
73 | * @var AddMultiElements\DataElementsMaster |
||
74 | */ |
||
75 | public $dataElementsMaster; |
||
76 | |||
77 | /** |
||
78 | * Create PNR_AddMultiElements object |
||
79 | * |
||
80 | * @param PnrAddMultiElementsBase|null $params |
||
81 | */ |
||
82 | 40 | public function __construct(PnrAddMultiElementsBase $params = null) |
|
92 | |||
93 | /** |
||
94 | * PNR_AddMultiElements call which only adds requested data to the message |
||
95 | * |
||
96 | * For doing specific actions like ignoring or saving PNR. |
||
97 | * |
||
98 | * @param PnrAddMultiElementsOptions $params |
||
99 | */ |
||
100 | 11 | protected function loadBare(PnrAddMultiElementsOptions $params) |
|
101 | { |
||
102 | 11 | $tattooCounter = 0; |
|
103 | |||
104 | 11 | if (!is_null($params->actionCode)) { |
|
105 | 11 | $this->pnrActions = new AddMultiElements\PnrActions( |
|
106 | 11 | $params->actionCode |
|
|
|||
107 | 11 | ); |
|
108 | 11 | } |
|
109 | |||
110 | 11 | if (!is_null($params->recordLocator)) { |
|
111 | 3 | $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator); |
|
112 | 3 | } |
|
113 | |||
114 | 11 | if ($params->travellerGroup !== null) { |
|
115 | 1 | $this->addTravellerGroup($params->travellerGroup); |
|
116 | 1 | } else { |
|
117 | 10 | $this->addTravellers($params->travellers); |
|
118 | } |
||
119 | |||
120 | 11 | $this->addItineraries($params->itineraries, $params->tripSegments, $tattooCounter); |
|
121 | |||
122 | 11 | if (!empty($params->elements)) { |
|
123 | 5 | $this->addElements( |
|
124 | 5 | $params->elements, |
|
125 | 5 | $tattooCounter, |
|
126 | 5 | $params->autoAddReceivedFrom, |
|
127 | 5 | $params->defaultReceivedFrom, |
|
128 | 5 | $params->receivedFrom |
|
129 | 5 | ); |
|
130 | 5 | } else { |
|
131 | 6 | $this->addReceivedFrom( |
|
132 | 6 | $params->receivedFrom, |
|
133 | 6 | $params->autoAddReceivedFrom, |
|
134 | 6 | $params->defaultReceivedFrom, |
|
135 | $tattooCounter |
||
136 | 6 | ); |
|
137 | } |
||
138 | 11 | } |
|
139 | |||
140 | /** |
||
141 | * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input. |
||
142 | * |
||
143 | * @throws InvalidArgumentException When invalid input is provided |
||
144 | * @param PnrCreatePnrOptions $params |
||
145 | */ |
||
146 | 29 | protected function loadCreatePnr(PnrCreatePnrOptions $params) |
|
147 | { |
||
148 | 29 | $this->pnrActions = new AddMultiElements\PnrActions( |
|
149 | 29 | $params->actionCode |
|
150 | 29 | ); |
|
151 | |||
152 | 29 | $tattooCounter = 0; |
|
153 | |||
154 | 29 | if ($params->travellerGroup !== null) { |
|
155 | 1 | $this->addTravellerGroup($params->travellerGroup); |
|
156 | 1 | } else { |
|
157 | 28 | $this->addTravellers($params->travellers); |
|
158 | } |
||
159 | |||
160 | 29 | $this->addItineraries($params->itineraries, $params->tripSegments, $tattooCounter); |
|
161 | |||
162 | 27 | $this->addElements( |
|
163 | 27 | $params->elements, |
|
164 | 27 | $tattooCounter, |
|
165 | 27 | $params->autoAddReceivedFrom, |
|
166 | 27 | $params->defaultReceivedFrom, |
|
167 | 27 | $params->receivedFrom |
|
168 | 27 | ); |
|
169 | 25 | } |
|
170 | |||
171 | /** |
||
172 | * Load Segment itinerary |
||
173 | * |
||
174 | * @param Itinerary[] $itineraries |
||
175 | * @param Segment[] $legacySegments |
||
176 | * @param int $tattooCounter (BYREF) |
||
177 | */ |
||
178 | 40 | protected function addItineraries($itineraries, $legacySegments, &$tattooCounter) |
|
193 | |||
194 | /** |
||
195 | * @param Segment[] $segments |
||
196 | * @param int $tattooCounter |
||
197 | * @param string|null $origin |
||
198 | * @param string|null $destination |
||
199 | * |
||
200 | */ |
||
201 | 31 | protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null) |
|
215 | |||
216 | /** |
||
217 | * @param Segment $segment |
||
218 | * @param int $tattooCounter (BYREF) |
||
219 | * @return ItineraryInfo |
||
220 | */ |
||
221 | 31 | protected function createSegment($segment, &$tattooCounter) |
|
263 | |||
264 | |||
265 | /** |
||
266 | * @param Traveller[] $travellers |
||
267 | */ |
||
268 | 40 | protected function addTravellers($travellers) |
|
274 | |||
275 | /** |
||
276 | * @param Traveller $traveller |
||
277 | * @return TravellerInfo |
||
278 | */ |
||
279 | 35 | protected function createTraveller($traveller) |
|
283 | |||
284 | /** |
||
285 | * @param TravellerGroup $group |
||
286 | */ |
||
287 | 2 | protected function addTravellerGroup($group) |
|
293 | |||
294 | /** |
||
295 | * @param Element[] $elements |
||
296 | * @param int $tattooCounter (BYREF) |
||
297 | * @param bool $autoAddRf |
||
298 | * @param string|null $defaultRf |
||
299 | * @param string|null $explicitRf |
||
300 | */ |
||
301 | 32 | protected function addElements($elements, &$tattooCounter, $autoAddRf, $defaultRf, $explicitRf = null) |
|
332 | |||
333 | /** |
||
334 | * @param Element $element |
||
335 | * @param int $tattooCounter (BYREF) |
||
336 | * @throws InvalidArgumentException |
||
337 | * @return DataElementsIndiv |
||
338 | */ |
||
339 | 37 | protected function createElement($element, &$tattooCounter) |
|
353 | |||
354 | /** |
||
355 | * Add Received From field - if needed. |
||
356 | * |
||
357 | * @param string|null $explicitRf Explicitly provided RF string on request. |
||
358 | * @param bool $doAutoAdd Wether to automatically add an RF field. |
||
359 | * @param string|null $defaultRf The default RF string set in the client. |
||
360 | * @param int $tattooCounter (BYREF) |
||
361 | */ |
||
362 | 35 | protected function addReceivedFrom($explicitRf, $doAutoAdd, $defaultRf, &$tattooCounter) |
|
381 | } |
||
382 |
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.