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 | abstract class PlayOffTreeGen extends TreeGen |
||
10 | { |
||
11 | /** |
||
12 | * Calculate the Byes need to fill the Championship Tree. |
||
13 | * |
||
14 | * @param $fighters |
||
15 | * |
||
16 | * @return Collection |
||
17 | */ |
||
18 | View Code Duplication | protected function getByeGroup($fighters) |
|
26 | |||
27 | /** |
||
28 | * Chunk Fighters into groups for fighting, and optionnaly shuffle. |
||
29 | * |
||
30 | * @param $fightersByEntity |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | protected function chunk(Collection $fightersByEntity) |
||
43 | |||
44 | /** |
||
45 | * Generate First Round Fights. |
||
46 | */ |
||
47 | public function generateFights() |
||
52 | |||
53 | /** |
||
54 | * Save Groups with their parent info. |
||
55 | * |
||
56 | * @param int $numRounds |
||
57 | * @param $numFightersElim |
||
58 | */ |
||
59 | protected function pushGroups($numRounds, $numFightersElim) |
||
71 | |||
72 | /** |
||
73 | * Return number of rounds for the tree based on fighter count. |
||
74 | * |
||
75 | * @param $numFighters |
||
76 | * |
||
77 | * @return int |
||
78 | */ |
||
79 | protected function getNumRounds($numFighters) |
||
83 | |||
84 | /** |
||
85 | * @throws TreeGenerationException |
||
86 | */ |
||
87 | protected function generateAllTrees() |
||
105 | |||
106 | protected function generateGroupsForRound(Collection $fightersByArea, $round) |
||
112 | } |
||
113 |
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.