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 |
||
| 35 | View Code Duplication | class FlightInfo |
|
|
|
|||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var flightDetails |
||
| 39 | */ |
||
| 40 | public $flightDetails; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var AdditionalSegmentDetails |
||
| 44 | */ |
||
| 45 | public $additionnalSegmentDetails; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Inventory |
||
| 49 | */ |
||
| 50 | public $inventory; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * SegmentGroup constructor. |
||
| 54 | * |
||
| 55 | * @param Segment $options |
||
| 56 | */ |
||
| 57 | public function __construct($options) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Load non-required options if available |
||
| 76 | * |
||
| 77 | * @param Segment $options |
||
| 78 | */ |
||
| 79 | protected function loadOptionalSegmentInformation($options) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string|null $airplaneCode |
||
| 98 | * @param int|null $nrOfStops |
||
| 99 | */ |
||
| 100 | protected function loadAdditionalSegmentDetails($airplaneCode, $nrOfStops) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Load inventory information |
||
| 109 | * |
||
| 110 | * @param array $inventory |
||
| 111 | */ |
||
| 112 | protected function loadInventory($inventory) |
||
| 125 | } |
||
| 126 |
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.