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 |
||
9 | class ItinerarySorter |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $destinationHashmap = []; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $originHashmap = []; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $pointersHashmap = []; |
||
25 | |||
26 | /** |
||
27 | * @var null|BoardingCard |
||
28 | */ |
||
29 | private $backwardPointer; |
||
30 | |||
31 | /** |
||
32 | * @var null|BoardingCard |
||
33 | */ |
||
34 | private $forwardPointer; |
||
35 | |||
36 | /** |
||
37 | * @var BoardingCardCollection |
||
38 | */ |
||
39 | private $sortedBoardingCards; |
||
40 | |||
41 | /** |
||
42 | * Given a unsorted collection of boarding cards |
||
43 | * Returns a sorted collection of boarding cards |
||
44 | * |
||
45 | * @param BoardingCardCollection $boardingCards |
||
46 | * @return BoardingCardCollection |
||
47 | * @throws ItineraryNotFoundException |
||
48 | * @throws EmptyItineraryException |
||
49 | */ |
||
50 | public function sort(BoardingCardCollection $boardingCards) |
||
74 | |||
75 | /** |
||
76 | * @param BoardingCardCollection $boardingCards |
||
77 | */ |
||
78 | private function generateHashmaps(BoardingCardCollection $boardingCards) |
||
87 | |||
88 | private function clearHashmaps() |
||
94 | |||
95 | private function moveBackward() |
||
114 | |||
115 | private function moveForward() |
||
132 | |||
133 | /** |
||
134 | * @param BoardingCardCollection $boardingCards |
||
135 | * @return \ItinerarySorter\Model\BoardingCard |
||
136 | * @throws EmptyItineraryException |
||
137 | */ |
||
138 | public function initializePointers(BoardingCardCollection $boardingCards) |
||
147 | |||
148 | /** |
||
149 | * @param array $hashmap |
||
150 | * @param string $location |
||
151 | * @return bool |
||
152 | */ |
||
153 | private function isThereNextLeg($hashmap, $location) |
||
157 | } |
||
158 |
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.