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 SingleEliminationTreeGen 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 SingleEliminationTreeGen, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class SingleEliminationTreeGen extends TreeGen |
||
12 | { |
||
13 | /** |
||
14 | * Calculate the Byes needed to fill the Championship Tree. |
||
15 | * |
||
16 | * @param $fighters |
||
17 | * |
||
18 | * @return Collection |
||
19 | */ |
||
20 | View Code Duplication | protected function getByeGroup($fighters) |
|
29 | |||
30 | /** |
||
31 | * Save Groups with their parent info. |
||
32 | * |
||
33 | * @param int $numRounds |
||
34 | * @param int $numFighters |
||
35 | */ |
||
36 | protected function pushGroups($numRounds, $numFighters) |
||
58 | |||
59 | /** |
||
60 | * Create empty groups after round 1. |
||
61 | * |
||
62 | * @param $numFighters |
||
63 | */ |
||
64 | protected function pushEmptyGroupsToTree($numFighters) |
||
79 | |||
80 | /** |
||
81 | * Chunk Fighters into groups for fighting, and optionnaly shuffle. |
||
82 | * |
||
83 | * @param $fightersByEntity |
||
84 | * |
||
85 | * @return Collection|null |
||
86 | */ |
||
87 | protected function chunk(Collection $fightersByEntity) |
||
96 | |||
97 | /** |
||
98 | * Generate First Round Fights. |
||
99 | */ |
||
100 | protected function generateFights() |
||
124 | |||
125 | /** |
||
126 | * Return number of rounds for the tree based on fighter count. |
||
127 | * |
||
128 | * @param $numFighters |
||
129 | * |
||
130 | * @return int |
||
131 | */ |
||
132 | protected function getNumRounds($numFighters) |
||
136 | |||
137 | /** |
||
138 | * Get the group size for the first row |
||
139 | * |
||
140 | * @return int - return 2 if not preliminary, 3,4,5 otherwise |
||
141 | */ |
||
142 | private function firstRoundGroupSize() |
||
148 | |||
149 | /** |
||
150 | * Generate all the groups, and assign figthers to group |
||
151 | * @throws TreeGenerationException |
||
152 | */ |
||
153 | protected function generateAllTrees() |
||
162 | |||
163 | /** |
||
164 | * @param Collection $usersByArea |
||
165 | * @param $round |
||
166 | */ |
||
167 | public function generateGroupsForRound(Collection $usersByArea, $round) |
||
182 | |||
183 | /** |
||
184 | * Check if there is enough fighters, throw exception otherwise. |
||
185 | * |
||
186 | * @throws TreeGenerationException |
||
187 | */ |
||
188 | private function minFightersCheck() |
||
212 | |||
213 | /** |
||
214 | * Insert byes group alternated with full groups. |
||
215 | * |
||
216 | * @param Collection $fighters - List of fighters |
||
217 | * @param integer $numByeTotal - Quantity of byes to insert |
||
218 | * @return Collection - Full list of fighters including byes |
||
219 | */ |
||
220 | private function insertByes(Collection $fighters, $numByeTotal) |
||
250 | |||
251 | /** |
||
252 | * @param $frequency |
||
253 | * @param $groupSize |
||
254 | * @param $count |
||
255 | * @param $byeCount |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | private |
||
264 | |||
265 | |||
266 | /** |
||
267 | * Method that fills fighters with Bye Groups at the end |
||
268 | * @param $fighters |
||
269 | * @param Collection $fighterGroups |
||
270 | * |
||
271 | * @return Collection |
||
272 | */ |
||
273 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
292 | |||
293 | /** |
||
294 | * Get the biggest entity group. |
||
295 | * |
||
296 | * @param $userGroups |
||
297 | * |
||
298 | * @return int |
||
299 | */ |
||
300 | private |
||
310 | |||
311 | /** |
||
312 | * Repart BYE in the tree,. |
||
313 | * |
||
314 | * @param $fighterGroups |
||
315 | * @param int $max |
||
316 | * |
||
317 | * @return Collection |
||
318 | */ |
||
319 | private |
||
333 | } |
||
334 |
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.