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 | class TreeGen implements TreeGenerable |
||
15 | { |
||
16 | protected $groupBy; |
||
17 | protected $tree; |
||
18 | public $championship; |
||
19 | public $settings; |
||
20 | protected $numFighters; |
||
21 | |||
22 | /** |
||
23 | * @param Championship $championship |
||
24 | * @param $groupBy |
||
25 | */ |
||
26 | public function __construct(Championship $championship, $groupBy) |
||
33 | |||
34 | /** |
||
35 | * Generate tree groups for a championship. |
||
36 | * |
||
37 | * @throws TreeGenerationException |
||
38 | */ |
||
39 | public function run() |
||
49 | |||
50 | /** |
||
51 | * @param $userGroups |
||
52 | * |
||
53 | * @return int |
||
54 | */ |
||
55 | private function getMaxFightersByEntity($userGroups): int |
||
73 | |||
74 | /** |
||
75 | * Get Competitor's list ordered by entities |
||
76 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
77 | * |
||
78 | * @return Collection |
||
79 | */ |
||
80 | private function getFightersByEntity($fighters): Collection |
||
93 | |||
94 | /** |
||
95 | * Calculate the Byes need to fill the Championship Tree. |
||
96 | * |
||
97 | * @param Championship $championship |
||
98 | * |
||
99 | * @return Collection |
||
100 | */ |
||
101 | private function getByeGroup(Championship $championship, $fighters) |
||
124 | |||
125 | /** |
||
126 | * @param $fighterCount |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | private function getTreeSize($fighterCount, $groupSize) |
||
144 | |||
145 | /** |
||
146 | * @param $byeCount |
||
147 | * |
||
148 | * @return Collection |
||
149 | */ |
||
150 | private function createNullsGroup($byeCount, $isTeam): Collection |
||
163 | |||
164 | /** |
||
165 | * @param $fighterGroups |
||
166 | * @param int $max |
||
167 | * |
||
168 | * @return Collection |
||
169 | */ |
||
170 | private function repart($fighterGroups, $max) |
||
184 | |||
185 | /** |
||
186 | * Insert byes in an homogen way. |
||
187 | * |
||
188 | * @param Collection $fighters |
||
189 | * @param Collection $byeGroup |
||
190 | * |
||
191 | * @return Collection |
||
192 | */ |
||
193 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
218 | |||
219 | private function getFighters() |
||
227 | |||
228 | /** |
||
229 | * @param $usersByArea |
||
230 | * @param $area |
||
231 | * |
||
232 | */ |
||
233 | public function generateGroupsForRound($usersByArea, $area, $round, $shuffle) |
||
246 | |||
247 | /** |
||
248 | * @param $fighters |
||
249 | * @param $area |
||
250 | * @param $order |
||
251 | * @param $round |
||
252 | * @return FightersGroup |
||
253 | */ |
||
254 | public function saveGroupAndSync($fighters, $area, $order, $round, $parent, $shuffle) |
||
270 | |||
271 | /** |
||
272 | * Create empty groups for direct Elimination Tree |
||
273 | * @param $numFighters |
||
274 | */ |
||
275 | public function pushEmptyGroupsToTree($numFighters) |
||
286 | |||
287 | /** |
||
288 | * @param $area |
||
289 | * @param $order |
||
290 | * @param $round |
||
291 | * @param $parent |
||
292 | * @return FightersGroup |
||
293 | */ |
||
294 | private function saveGroup($area, $order, $round, $parent): FightersGroup |
||
307 | |||
308 | private function createByeFighter() |
||
314 | |||
315 | public function createByeGroup($groupSize): Collection |
||
324 | |||
325 | /** |
||
326 | * @param $fighters |
||
327 | * @param $fighterGroups |
||
328 | * @return Collection |
||
329 | */ |
||
330 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
346 | |||
347 | /** |
||
348 | * Get All Groups on previous round |
||
349 | * @param $currentRound |
||
350 | * @return Collection |
||
351 | */ |
||
352 | private function getPreviousRound($currentRound) |
||
358 | |||
359 | /** |
||
360 | * Get the next group on the right ( parent ), final round being the ancestor |
||
361 | * @param $matchNumber |
||
362 | * @param $previousRound |
||
363 | * @return mixed |
||
364 | */ |
||
365 | private function getParentGroup($matchNumber, $previousRound) |
||
371 | |||
372 | /** |
||
373 | * Save Groups with their parent info |
||
374 | * @param $numRounds |
||
375 | * @param $numFightersEliminatory |
||
376 | */ |
||
377 | private function pushGroups($numRounds, $numFightersEliminatory, $shuffle = true) |
||
387 | |||
388 | /** |
||
389 | * @return Collection |
||
390 | * @throws TreeGenerationException |
||
391 | */ |
||
392 | private function getFightersByArea() |
||
409 | |||
410 | /** |
||
411 | * @param $round |
||
412 | * @param $shuffle |
||
413 | * @param $fightersByEntity |
||
414 | * @return mixed |
||
415 | */ |
||
416 | private function chunkAndShuffle($round, $shuffle, $fightersByEntity) |
||
429 | |||
430 | /** |
||
431 | * Attach a parent to every child for nestedSet Navigation |
||
432 | */ |
||
433 | private function addParentToChildren($numFightersEliminatory) |
||
455 | } |
||
456 |
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.