Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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() |
||
| 40 | { |
||
| 41 | $usersByArea = $this->getFightersByArea(); |
||
| 42 | $numFighters = sizeof($usersByArea->collapse()); |
||
| 43 | |||
| 44 | $this->generateGroupsForRound($usersByArea, 1, 1); |
||
| 45 | $this->pushEmptyGroupsToTree($numFighters); |
||
|
|
|||
| 46 | $this->addParentToChildren($numFighters); |
||
| 47 | $this->generateFights(); |
||
| 48 | $this->generateNextRoundsFights(); |
||
| 49 | // Fight::generateFightsId($this->championship); |
||
| 50 | //dd("ok"); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the biggest entity group |
||
| 55 | * @param $userGroups |
||
| 56 | * |
||
| 57 | * @return int |
||
| 58 | */ |
||
| 59 | private function getMaxFightersByEntity($userGroups): int |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get Competitor's list ordered by entities |
||
| 72 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
| 73 | * |
||
| 74 | * @param $fighters |
||
| 75 | * @return Collection |
||
| 76 | */ |
||
| 77 | private function getFightersByEntity($fighters): Collection |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the size the first round will have |
||
| 93 | * @param $fighterCount |
||
| 94 | * @param $groupSize |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | protected function getTreeSize($fighterCount, $groupSize) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Repart BYE in the tree, |
||
| 114 | * @param $fighterGroups |
||
| 115 | * @param int $max |
||
| 116 | * |
||
| 117 | * @return Collection |
||
| 118 | */ |
||
| 119 | private function repart($fighterGroups, $max) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Insert byes in an homogen way. |
||
| 136 | * |
||
| 137 | * @param Collection $fighters |
||
| 138 | * @param Collection $byeGroup |
||
| 139 | * |
||
| 140 | * @return Collection |
||
| 141 | */ |
||
| 142 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param Collection $usersByArea |
||
| 158 | * @param integer $area |
||
| 159 | * @param integer $round |
||
| 160 | * |
||
| 161 | */ |
||
| 162 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param $area |
||
| 183 | * @param $order |
||
| 184 | * @param $round |
||
| 185 | * @param $parent |
||
| 186 | * @return FightersGroup |
||
| 187 | */ |
||
| 188 | protected function saveGroup($area, $order, $round, $parent): FightersGroup |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param integer $groupSize |
||
| 205 | * @return Collection |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function createByeGroup($groupSize): Collection |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param $fighters |
||
| 219 | * @param Collection $fighterGroups |
||
| 220 | * @return Collection |
||
| 221 | */ |
||
| 222 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get All Groups on previous round |
||
| 240 | * @param $currentRound |
||
| 241 | * @return Collection |
||
| 242 | */ |
||
| 243 | private function getPreviousRound($currentRound) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get the next group on the right ( parent ), final round being the ancestor |
||
| 251 | * @param $matchNumber |
||
| 252 | * @param Collection $previousRound |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | private function getParentGroup($matchNumber, $previousRound) |
||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * Group Fighters by area |
||
| 266 | * @return Collection |
||
| 267 | * @throws TreeGenerationException |
||
| 268 | */ |
||
| 269 | private function getFightersByArea() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Attach a parent to every child for nestedSet Navigation |
||
| 290 | * @param $numFightersElim |
||
| 291 | */ |
||
| 292 | private function addParentToChildren($numFightersElim) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Create Bye Groups to adjust tree |
||
| 314 | * @param $byeCount |
||
| 315 | * @return Collection |
||
| 316 | */ |
||
| 317 | View Code Duplication | protected function createNullsGroup($byeCount): Collection |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param Collection $fighters |
||
| 329 | * @param $frequency |
||
| 330 | * @param $sizeGroupBy |
||
| 331 | * @param $bye |
||
| 332 | * @return Collection |
||
| 333 | */ |
||
| 334 | private function getFullFighterList(Collection $fighters, $frequency, $sizeGroupBy, $bye): Collection |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param $frequency |
||
| 352 | * @param $sizeGroupBy |
||
| 353 | * @param $count |
||
| 354 | * @param $byeCount |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | private function shouldInsertBye($frequency, $sizeGroupBy, $count, $byeCount): bool |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * @param Championship $championship |
||
| 365 | */ |
||
| 366 | protected function destroyPreviousFights() |
||
| 372 | } |
||
| 373 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: