Complex classes like TreeGen 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 TreeGen, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class TreeGen implements TreeGenerable |
||
15 | { |
||
16 | protected $groupBy; |
||
17 | protected $tree; |
||
18 | public $championship; |
||
19 | public $settings; |
||
20 | protected $numFighters; |
||
21 | |||
22 | abstract protected function pushEmptyGroupsToTree($numFighters); |
||
23 | |||
24 | abstract protected function generateFights(); |
||
25 | |||
26 | abstract protected function createByeFighter(); |
||
27 | |||
28 | abstract protected function chunkAndShuffle(Collection $fightersByEntity); |
||
29 | |||
30 | abstract protected function addFighterToGroup(FightersGroup $group, $fighter); |
||
31 | |||
32 | abstract protected function syncGroup(FightersGroup $group, $fighters); |
||
33 | |||
34 | abstract protected function getByeGroup($fighters); |
||
35 | |||
36 | abstract protected function getFighter($fighterId); |
||
37 | |||
38 | abstract protected function getFighters(); |
||
39 | |||
40 | abstract protected function getNumRounds($fightersCount); |
||
41 | |||
42 | abstract protected function generateGroupsForRound(Collection $usersByArea, $round); |
||
43 | |||
44 | abstract protected function generateAllTrees(); |
||
45 | |||
46 | /** |
||
47 | * @param Championship $championship |
||
48 | * @param $groupBy |
||
49 | */ |
||
50 | public function __construct(Championship $championship, $groupBy) |
||
57 | |||
58 | /** |
||
59 | * Generate tree groups for a championship. |
||
60 | * |
||
61 | * @throws TreeGenerationException |
||
62 | */ |
||
63 | public function run() |
||
68 | |||
69 | /** |
||
70 | * Get the biggest entity group. |
||
71 | * |
||
72 | * @param $userGroups |
||
73 | * |
||
74 | * @return int |
||
75 | */ |
||
76 | private function getMaxFightersByEntity($userGroups): int |
||
85 | |||
86 | /** |
||
87 | * Get Competitor's list ordered by entities |
||
88 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
89 | * |
||
90 | * @param $fighters |
||
91 | * |
||
92 | * @return Collection |
||
93 | */ |
||
94 | private function getFightersByEntity($fighters): Collection |
||
105 | |||
106 | /** |
||
107 | * Get the size the first round will have. |
||
108 | * |
||
109 | * @param $fighterCount |
||
110 | * @param $groupSize |
||
111 | * |
||
112 | * @return int |
||
113 | */ |
||
114 | protected function getTreeSize($fighterCount, $groupSize) |
||
136 | |||
137 | /** |
||
138 | * Repart BYE in the tree,. |
||
139 | * |
||
140 | * @param $fighterGroups |
||
141 | * @param int $max |
||
142 | * |
||
143 | * @return Collection |
||
144 | */ |
||
145 | private function repart($fighterGroups, $max) |
||
159 | |||
160 | /** |
||
161 | * Insert byes in an homogen way. |
||
162 | * |
||
163 | * @param Collection $fighters |
||
164 | * @param Collection $byeGroup |
||
165 | * |
||
166 | * @return Collection |
||
167 | */ |
||
168 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @param $order |
||
185 | * @param $round |
||
186 | * @param $parent |
||
187 | * |
||
188 | * @return FightersGroup |
||
189 | */ |
||
190 | protected function saveGroup($order, $round, $parent): FightersGroup |
||
208 | |||
209 | /** |
||
210 | * @param int $groupSize |
||
211 | * |
||
212 | * @return Collection |
||
213 | */ |
||
214 | public function createByeGroup($groupSize): Collection |
||
224 | |||
225 | /** |
||
226 | * @param $fighters |
||
227 | * @param Collection $fighterGroups |
||
228 | * |
||
229 | * @return Collection |
||
230 | */ |
||
231 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
246 | |||
247 | /** |
||
248 | * Get All Groups on previous round. |
||
249 | * |
||
250 | * @param $currentRound |
||
251 | * |
||
252 | * @return Collection |
||
253 | */ |
||
254 | private function getPreviousRound($currentRound) |
||
260 | |||
261 | /** |
||
262 | * Get the next group on the right ( parent ), final round being the ancestor. |
||
263 | * |
||
264 | * @param $matchNumber |
||
265 | * @param Collection $previousRound |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | private function getParentGroup($matchNumber, $previousRound) |
||
276 | |||
277 | /** |
||
278 | * Group Fighters by area. |
||
279 | * |
||
280 | * @throws TreeGenerationException |
||
281 | * |
||
282 | * @return Collection |
||
283 | */ |
||
284 | protected function getFightersByArea() |
||
317 | |||
318 | /** |
||
319 | * Attach a parent to every child for nestedSet Navigation. |
||
320 | * |
||
321 | * @param $numFightersElim |
||
322 | */ |
||
323 | protected function addParentToChildren($numFightersElim) |
||
342 | |||
343 | /** |
||
344 | * @param Collection $fighters |
||
345 | * @param $frequency |
||
346 | * @param $sizeGroupBy |
||
347 | * @param $bye |
||
348 | * |
||
349 | * @return Collection |
||
350 | */ |
||
351 | private function getFullFighterList(Collection $fighters, $frequency, $sizeGroupBy, $bye): Collection |
||
367 | |||
368 | /** |
||
369 | * @param $frequency |
||
370 | * @param $sizeGroupBy |
||
371 | * @param $count |
||
372 | * @param $byeCount |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | private function shouldInsertBye($frequency, $sizeGroupBy, $count, $byeCount): bool |
||
380 | |||
381 | /** |
||
382 | * Destroy Previous Fights for demo. |
||
383 | */ |
||
384 | protected function destroyPreviousFights() |
||
390 | |||
391 | /** |
||
392 | * Generate Fights for next rounds. |
||
393 | */ |
||
394 | public function generateNextRoundsFights() |
||
403 | |||
404 | /** |
||
405 | * @param $groupsByRound |
||
406 | */ |
||
407 | protected function updateParentFight($groupsByRound) |
||
420 | |||
421 | /** |
||
422 | * @param $group |
||
423 | * @param $parentFight |
||
424 | */ |
||
425 | protected function chooseAndUpdateParentFight($keyGroup, FightersGroup $group, Fight $parentFight) |
||
442 | |||
443 | /** |
||
444 | * Calculate the area of the group ( group is still not created ). |
||
445 | * |
||
446 | * @param $round |
||
447 | * @param $order |
||
448 | * |
||
449 | * @return int |
||
450 | */ |
||
451 | protected function getNumArea($round, $order) |
||
463 | |||
464 | |||
465 | protected function generateAllFights() |
||
473 | } |
||
474 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.