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 |
||
15 | class TreeGen implements TreeGenerable |
||
16 | { |
||
17 | protected $groupBy, $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() |
||
66 | |||
67 | /** |
||
68 | * @param $userGroups |
||
69 | * |
||
70 | * @return int |
||
71 | */ |
||
72 | private function getMaxCompetitorByEntity($userGroups): int |
||
84 | |||
85 | /** |
||
86 | * Get Competitor's list ordered by entities |
||
87 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
88 | * |
||
89 | * @return Collection |
||
90 | */ |
||
91 | private function getFightersByEntity($fighters): Collection |
||
118 | |||
119 | /** |
||
120 | * Calculate the Byes need to fill the Championship Tree. |
||
121 | * |
||
122 | * @param Championship $championship |
||
123 | * |
||
124 | * @return Collection |
||
125 | */ |
||
126 | private function getByeGroup(Championship $championship, $fighters) |
||
149 | |||
150 | /** |
||
151 | * @param $fighterCount |
||
152 | * |
||
153 | * @return int |
||
154 | */ |
||
155 | private function getTreeSize($fighterCount, $groupSize) |
||
170 | |||
171 | /** |
||
172 | * @param $byeCount |
||
173 | * |
||
174 | * @return Collection |
||
175 | */ |
||
176 | private function createNullsGroup($byeCount, $isTeam): Collection |
||
189 | |||
190 | /** |
||
191 | * @param $fighterGroups |
||
192 | * @param int $max |
||
193 | * |
||
194 | * @return Collection |
||
195 | */ |
||
196 | private function repart($fighterGroups, $max) |
||
210 | |||
211 | /** |
||
212 | * Insert byes in an homogen way. |
||
213 | * |
||
214 | * @param Collection $fighters |
||
215 | * @param Collection $byeGroup |
||
216 | * |
||
217 | * @return Collection |
||
218 | */ |
||
219 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
244 | |||
245 | private function getFighters() |
||
253 | |||
254 | /** |
||
255 | * @param $usersByArea |
||
256 | * @param $area |
||
257 | * |
||
258 | * @return Collection |
||
259 | */ |
||
260 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
283 | |||
284 | /** |
||
285 | * @param $area |
||
286 | * @param $fighters |
||
287 | * @param $order |
||
288 | * @param $round |
||
289 | * @return FightersGroup |
||
290 | */ |
||
291 | public function saveGroup($area, $fighters, $order, $round) |
||
311 | |||
312 | /** |
||
313 | * @param $numFighters |
||
314 | */ |
||
315 | private function pushEmptyGroupsToTree($numFighters) |
||
332 | } |
||
333 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.