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 | |||
21 | /** |
||
22 | * @param \Xoco70\KendoTournaments\Models\ChampionshipSettings $settings |
||
23 | */ |
||
24 | public function __construct(Championship $championship, $groupBy, $settings) |
||
30 | |||
31 | /** |
||
32 | * Generate tree groups for a championship. |
||
33 | * |
||
34 | * @throws TreeGenerationException |
||
35 | * |
||
36 | * @return Collection |
||
37 | */ |
||
38 | public function run() |
||
68 | |||
69 | /** |
||
70 | * @param $userGroups |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | private function getMaxFightersByEntity($userGroups): int |
||
86 | |||
87 | /** |
||
88 | * Get Competitor's list ordered by entities |
||
89 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
90 | * |
||
91 | * @return Collection |
||
92 | */ |
||
93 | private function getFightersByEntity($fighters): Collection |
||
106 | |||
107 | /** |
||
108 | * Calculate the Byes need to fill the Championship Tree. |
||
109 | * |
||
110 | * @param Championship $championship |
||
111 | * |
||
112 | * @return Collection |
||
113 | */ |
||
114 | private function getByeGroup(Championship $championship, $fighters) |
||
137 | |||
138 | /** |
||
139 | * @param $fighterCount |
||
140 | * |
||
141 | * @return int |
||
142 | */ |
||
143 | private function getTreeSize($fighterCount, $groupSize) |
||
158 | |||
159 | /** |
||
160 | * @param $byeCount |
||
161 | * |
||
162 | * @return Collection |
||
163 | */ |
||
164 | private function createNullsGroup($byeCount, $isTeam): Collection |
||
177 | |||
178 | /** |
||
179 | * @param $fighterGroups |
||
180 | * @param int $max |
||
181 | * |
||
182 | * @return Collection |
||
183 | */ |
||
184 | private function repart($fighterGroups, $max) |
||
198 | |||
199 | /** |
||
200 | * Insert byes in an homogen way. |
||
201 | * |
||
202 | * @param Collection $fighters |
||
203 | * @param Collection $byeGroup |
||
204 | * |
||
205 | * @return Collection |
||
206 | */ |
||
207 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
232 | |||
233 | private function getFighters() |
||
241 | |||
242 | /** |
||
243 | * @param $usersByArea |
||
244 | * @param $area |
||
245 | * |
||
246 | */ |
||
247 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
271 | |||
272 | /** |
||
273 | * @param $fighters |
||
274 | * @param $area |
||
275 | * @param $order |
||
276 | * @param $round |
||
277 | * @return FightersGroup |
||
278 | */ |
||
279 | public function saveGroupAndSync($fighters, $area, $order, $round, $parent) |
||
295 | |||
296 | /** |
||
297 | * @param $numFighters |
||
298 | */ |
||
299 | private function pushEmptyGroupsToTree($numFighters) |
||
310 | |||
311 | /** |
||
312 | * @param $area |
||
313 | * @param $order |
||
314 | * @param $round |
||
315 | * @return FightersGroup |
||
316 | */ |
||
317 | private function saveGroup($area, $order, $round, $parent): FightersGroup |
||
330 | |||
331 | private function createByeFighter() |
||
337 | |||
338 | public function createByeGroup($groupSize): Collection |
||
347 | |||
348 | /** |
||
349 | * @param $fighters |
||
350 | * @param $fighterGroups |
||
351 | * @return Collection |
||
352 | */ |
||
353 | private function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
369 | |||
370 | /** |
||
371 | * @param $currentRound |
||
372 | * @param $numRounds |
||
373 | * @return Collection |
||
374 | */ |
||
375 | private function getPreviousRound($currentRound, $numRounds = 0) |
||
383 | |||
384 | /** |
||
385 | * @param $roundNumber |
||
386 | * @param $numRounds |
||
387 | * @param $matchNumber |
||
388 | * @param $previousRound |
||
389 | * @return mixed |
||
390 | */ |
||
391 | private function getParentGroup($roundNumber, $numRounds = 0, $matchNumber, $previousRound) |
||
400 | |||
401 | /** |
||
402 | * @param $numRounds |
||
403 | * @param $numFightersEliminatory |
||
404 | */ |
||
405 | private function pushGroups($numRounds, $numFightersEliminatory) |
||
417 | } |
||
418 |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.