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 | * @throws \ReflectionException |
||
| 82 | */ |
||
| 83 | public function __construct(PnrAddMultiElementsBase $params = null) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * PNR_AddMultiElements call which only adds requested data to the message |
||
| 96 | * |
||
| 97 | * For doing specific actions like ignoring or saving PNR. |
||
| 98 | * |
||
| 99 | * @param PnrAddMultiElementsOptions $params |
||
| 100 | * @throws \ReflectionException |
||
| 101 | */ |
||
| 102 | protected function loadBare(PnrAddMultiElementsOptions $params) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input. |
||
| 144 | * |
||
| 145 | * @throws InvalidArgumentException When invalid input is provided |
||
| 146 | * @param PnrCreatePnrOptions $params |
||
| 147 | * @throws \ReflectionException |
||
| 148 | */ |
||
| 149 | protected function loadCreatePnr(PnrCreatePnrOptions $params) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Load Segment itinerary |
||
| 176 | * |
||
| 177 | * @param Itinerary[] $itineraries |
||
| 178 | * @param Segment[] $legacySegments |
||
| 179 | * @param int $tattooCounter (BYREF) |
||
| 180 | * @throws \ReflectionException |
||
| 181 | */ |
||
| 182 | protected function addItineraries($itineraries, $legacySegments, &$tattooCounter) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param Segment[] $segments |
||
| 200 | * @param int $tattooCounter |
||
| 201 | * @param string|null $origin |
||
| 202 | * @param string|null $destination |
||
| 203 | * @throws \ReflectionException |
||
| 204 | */ |
||
| 205 | protected function addSegments($segments, &$tattooCounter, $origin = null, $destination = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param Segment $segment |
||
| 222 | * @param int $tattooCounter (BYREF) |
||
| 223 | * @return ItineraryInfo |
||
| 224 | * @throws \ReflectionException |
||
| 225 | */ |
||
| 226 | protected function createSegment($segment, &$tattooCounter) |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Traveller[] $travellers |
||
| 272 | */ |
||
| 273 | protected function addTravellers($travellers) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Traveller $traveller |
||
| 282 | * @return TravellerInfo |
||
| 283 | */ |
||
| 284 | protected function createTraveller($traveller) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param TravellerGroup $group |
||
| 291 | */ |
||
| 292 | protected function addTravellerGroup($group) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Element[] $elements |
||
| 301 | * @param int $tattooCounter (BYREF) |
||
| 302 | * @param bool $autoAddRf |
||
| 303 | * @param string|null $defaultRf |
||
| 304 | * @param string|null $explicitRf |
||
| 305 | * @throws \ReflectionException |
||
| 306 | */ |
||
| 307 | protected function addElements($elements, &$tattooCounter, $autoAddRf, $defaultRf, $explicitRf = null) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param Element $element |
||
| 341 | * @param int $tattooCounter (BYREF) |
||
| 342 | * @throws InvalidArgumentException |
||
| 343 | * @return DataElementsIndiv |
||
| 344 | * @throws \ReflectionException |
||
| 345 | */ |
||
| 346 | protected function createElement($element, &$tattooCounter) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Add Received From field - if needed. |
||
| 363 | * |
||
| 364 | * @param string|null $explicitRf Explicitly provided RF string on request. |
||
| 365 | * @param bool $doAutoAdd Wether to automatically add an RF field. |
||
| 366 | * @param string|null $defaultRf The default RF string set in the client. |
||
| 367 | * @param int $tattooCounter (BYREF) |
||
| 368 | * @throws \ReflectionException |
||
| 369 | */ |
||
| 370 | protected function addReceivedFrom($explicitRf, $doAutoAdd, $defaultRf, &$tattooCounter) |
||
| 389 | } |
||
| 390 |
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.