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 |
||
| 17 | abstract class TreeGen implements TreeGenerable |
||
| 18 | { |
||
| 19 | protected $groupBy; |
||
| 20 | protected $tree; |
||
| 21 | public $championship; |
||
| 22 | public $settings; |
||
| 23 | protected $numFighters; |
||
| 24 | |||
| 25 | abstract protected function pushEmptyGroupsToTree($numFighters); |
||
| 26 | abstract protected function generateFights(); |
||
| 27 | abstract protected function createByeFighter(); |
||
| 28 | abstract protected function chunkAndShuffle($round, Collection $fightersByEntity); |
||
| 29 | abstract protected function addFighterToGroup(FightersGroup $group, $fighter); |
||
| 30 | abstract protected function syncGroup(FightersGroup $group, $fighters); |
||
| 31 | abstract protected function getByeGroup($fighters); |
||
| 32 | abstract protected function getFighter($fighterId); |
||
| 33 | abstract protected function getFighters(); |
||
| 34 | abstract protected function getNumRounds($fightersCount); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Championship $championship |
||
| 38 | * @param $groupBy |
||
| 39 | */ |
||
| 40 | public function __construct(Championship $championship, $groupBy) |
||
| 41 | { |
||
| 42 | $this->championship = $championship; |
||
| 43 | $this->groupBy = $groupBy; |
||
| 44 | $this->settings = $championship->getSettings(); |
||
| 45 | $this->tree = new Collection(); |
||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Generate tree groups for a championship. |
||
| 51 | * |
||
| 52 | * @throws TreeGenerationException |
||
| 53 | */ |
||
| 54 | public function run() |
||
| 55 | { |
||
| 56 | $usersByArea = $this->getFightersByArea(); |
||
| 57 | $numFighters = sizeof($usersByArea->collapse()); |
||
| 58 | |||
| 59 | $this->generateGroupsForRound($usersByArea, 1, 1); |
||
| 60 | $this->pushEmptyGroupsToTree($numFighters); |
||
| 61 | $this->addParentToChildren($numFighters); |
||
| 62 | $this->generateFights(); |
||
| 63 | $this->generateNextRoundsFights(); |
||
| 64 | Fight::generateFightsId($this->championship); |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the biggest entity group |
||
| 70 | * @param $userGroups |
||
| 71 | * |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | private function getMaxFightersByEntity($userGroups): int |
||
| 75 | { |
||
| 76 | return $userGroups |
||
| 77 | ->sortByDesc(function ($group) { |
||
| 78 | return $group->count(); |
||
| 79 | }) |
||
| 80 | ->first() |
||
| 81 | ->count(); |
||
| 82 | |||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get Competitor's list ordered by entities |
||
| 87 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
| 88 | * |
||
| 89 | * @param $fighters |
||
| 90 | * @return Collection |
||
| 91 | */ |
||
| 92 | private function getFightersByEntity($fighters): Collection |
||
| 93 | { |
||
| 94 | // Right now, we are treating users and teams as equals. |
||
| 95 | // It doesn't matter right now, because we only need name attribute which is common to both models |
||
| 96 | |||
| 97 | // $this->groupBy contains federation_id, association_id, club_id, etc. |
||
| 98 | if (($this->groupBy) != null) { |
||
| 99 | $fighterGroups = $fighters->groupBy($this->groupBy); // Collection of Collection |
||
| 100 | } else { |
||
| 101 | $fighterGroups = $fighters->chunk(1); // Collection of Collection |
||
| 102 | } |
||
| 103 | return $fighterGroups; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the size the first round will have |
||
| 108 | * @param $fighterCount |
||
| 109 | * @param $groupSize |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | protected function getTreeSize($fighterCount, $groupSize) |
||
| 113 | { |
||
| 114 | $square = collect([1, 2, 4, 8, 16, 32, 64]); |
||
| 115 | $squareMultiplied = $square->map(function ($item) use ($groupSize) { |
||
| 116 | return $item * $groupSize; |
||
| 117 | }); |
||
| 118 | |||
| 119 | foreach ($squareMultiplied as $limit) { |
||
| 120 | if ($fighterCount <= $limit) { |
||
| 121 | return $limit; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | return 64 * $groupSize; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Repart BYE in the tree, |
||
| 129 | * @param $fighterGroups |
||
| 130 | * @param int $max |
||
| 131 | * |
||
| 132 | * @return Collection |
||
| 133 | */ |
||
| 134 | private function repart($fighterGroups, $max) |
||
| 135 | { |
||
| 136 | $fighters = new Collection(); |
||
| 137 | for ($i = 0; $i < $max; $i++) { |
||
| 138 | foreach ($fighterGroups as $fighterGroup) { |
||
| 139 | $fighter = $fighterGroup->values()->get($i); |
||
| 140 | if ($fighter != null) { |
||
| 141 | $fighters->push($fighter); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $fighters; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Insert byes in an homogen way. |
||
| 151 | * |
||
| 152 | * @param Collection $fighters |
||
| 153 | * @param Collection $byeGroup |
||
| 154 | * |
||
| 155 | * @return Collection |
||
| 156 | */ |
||
| 157 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
| 158 | { |
||
| 159 | $bye = count($byeGroup) > 0 ? $byeGroup[0] : []; |
||
| 160 | $sizeFighters = count($fighters); |
||
| 161 | $sizeGroupBy = count($byeGroup); |
||
| 162 | |||
| 163 | $frequency = $sizeGroupBy != 0 |
||
| 164 | ? (int)floor($sizeFighters / $sizeGroupBy) |
||
| 165 | : -1; |
||
| 166 | |||
| 167 | // Create Copy of $competitors |
||
| 168 | return $this->getFullFighterList($fighters, $frequency, $sizeGroupBy, $bye); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param Collection $usersByArea |
||
| 173 | * @param integer $area |
||
| 174 | * @param integer $round |
||
| 175 | * |
||
| 176 | */ |
||
| 177 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
| 178 | { |
||
| 179 | foreach ($usersByArea as $fightersByEntity) { |
||
| 180 | // Chunking to make small round robin groups |
||
| 181 | $chunkedFighters = $this->chunkAndShuffle($round, $fightersByEntity); |
||
| 182 | $order = 1; |
||
| 183 | foreach ($chunkedFighters as $fighters) { |
||
| 184 | $fighters = $fighters->pluck('id'); |
||
| 185 | if (!App::runningUnitTests()) { |
||
| 186 | $fighters = $fighters->shuffle(); |
||
| 187 | } |
||
| 188 | $group = $this->saveGroup($area, $order, $round, null); |
||
| 189 | $this->syncGroup($group, $fighters); |
||
| 190 | $order++; |
||
| 191 | } |
||
| 192 | $area++; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $area |
||
| 198 | * @param $order |
||
| 199 | * @param $round |
||
| 200 | * @param $parent |
||
| 201 | * @return FightersGroup |
||
| 202 | */ |
||
| 203 | protected function saveGroup($area, $order, $round, $parent): FightersGroup |
||
| 204 | { |
||
| 205 | $group = new FightersGroup(); |
||
| 206 | $group->area = $area; |
||
| 207 | $group->order = $order; |
||
| 208 | $group->round = $round; |
||
| 209 | $group->championship_id = $this->championship->id; |
||
| 210 | if ($parent != null) { |
||
| 211 | $group->parent_id = $parent->id; |
||
| 212 | } |
||
| 213 | $group->save(); |
||
| 214 | return $group; |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * @param integer $groupSize |
||
| 220 | * @return Collection |
||
| 221 | */ |
||
| 222 | public function createByeGroup($groupSize): Collection |
||
| 223 | { |
||
| 224 | $byeFighter = $this->createByeFighter(); |
||
| 225 | $group = new Collection(); |
||
| 226 | for ($i = 0; $i < $groupSize; $i++) { |
||
| 227 | $group->push($byeFighter); |
||
| 228 | } |
||
| 229 | return $group; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param $fighters |
||
| 234 | * @param Collection $fighterGroups |
||
| 235 | * @return Collection |
||
| 236 | */ |
||
| 237 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
| 238 | { |
||
| 239 | $tmpFighterGroups = clone $fighterGroups; |
||
| 240 | $byeGroup = $this->getByeGroup($fighters); |
||
| 241 | |||
| 242 | // Get biggest competitor's group |
||
| 243 | $max = $this->getMaxFightersByEntity($tmpFighterGroups); |
||
| 244 | |||
| 245 | // We reacommodate them so that we can mix them up and they don't fight with another competitor of his entity. |
||
| 246 | |||
| 247 | $fighters = $this->repart($fighterGroups, $max); |
||
| 248 | $fighters = $this->insertByes($fighters, $byeGroup); |
||
| 249 | |||
| 250 | return $fighters; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get All Groups on previous round |
||
| 255 | * @param $currentRound |
||
| 256 | * @return Collection |
||
| 257 | */ |
||
| 258 | private function getPreviousRound($currentRound) |
||
| 259 | { |
||
| 260 | $previousRound = $this->championship->groupsByRound($currentRound + 1)->get(); |
||
| 261 | return $previousRound; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the next group on the right ( parent ), final round being the ancestor |
||
| 266 | * @param $matchNumber |
||
| 267 | * @param Collection $previousRound |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | private function getParentGroup($matchNumber, $previousRound) |
||
| 271 | { |
||
| 272 | $parentIndex = intval(($matchNumber + 1) / 2); |
||
| 273 | $parent = $previousRound->get($parentIndex - 1); |
||
| 274 | return $parent; |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Group Fighters by area |
||
| 280 | * @return Collection |
||
| 281 | * @throws TreeGenerationException |
||
| 282 | */ |
||
| 283 | private function getFightersByArea() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Attach a parent to every child for nestedSet Navigation |
||
| 304 | * @param $numFightersElim |
||
| 305 | */ |
||
| 306 | private function addParentToChildren($numFightersElim) |
||
| 307 | { |
||
| 308 | $numRounds = $this->getNumRounds($numFightersElim); |
||
| 309 | $groupsDesc = $this->championship |
||
| 310 | ->fightersGroups() |
||
| 311 | ->where('round', '<', $numRounds) |
||
| 312 | ->orderByDesc('id')->get(); |
||
| 313 | |||
| 314 | $groupsDescByRound = $groupsDesc->groupBy('round'); |
||
| 315 | |||
| 316 | foreach ($groupsDescByRound as $round => $groups) { |
||
| 317 | $previousRound = $this->getPreviousRound($round); |
||
| 318 | foreach ($groups->reverse()->values() as $matchNumber => $group) { |
||
| 319 | $parent = $this->getParentGroup($matchNumber + 1, $previousRound); |
||
| 320 | $group->parent_id = $parent->id; |
||
| 321 | $group->save(); |
||
| 322 | } |
||
| 323 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param Collection $fighters |
||
| 328 | * @param $frequency |
||
| 329 | * @param $sizeGroupBy |
||
| 330 | * @param $bye |
||
| 331 | * @return Collection |
||
| 332 | */ |
||
| 333 | private function getFullFighterList(Collection $fighters, $frequency, $sizeGroupBy, $bye): Collection |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param $frequency |
||
| 351 | * @param $sizeGroupBy |
||
| 352 | * @param $count |
||
| 353 | * @param $byeCount |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | private function shouldInsertBye($frequency, $sizeGroupBy, $count, $byeCount): bool |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * Destroy Previous Fights for demo |
||
| 364 | */ |
||
| 365 | protected function destroyPreviousFights() |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Generate Fights for next rounds |
||
| 375 | */ |
||
| 376 | public function generateNextRoundsFights() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $groupsByRound |
||
| 388 | */ |
||
| 389 | protected function updateParentFight($groupsByRound) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param $group |
||
| 403 | * @param $parentFight |
||
| 404 | */ |
||
| 405 | protected function chooseAndUpdateParentFight($keyGroup, FightersGroup $group, Fight $parentFight) |
||
| 422 | } |
||
| 423 |