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 |
||
| 13 | class TreeGen implements TreeGenerable |
||
| 14 | { |
||
| 15 | protected $groupBy; |
||
| 16 | protected $tree; |
||
| 17 | public $championship; |
||
| 18 | public $settings; |
||
| 19 | protected $numFighters; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param Championship $championship |
||
| 23 | * @param $groupBy |
||
| 24 | */ |
||
| 25 | public function __construct(Championship $championship, $groupBy) |
||
| 26 | { |
||
| 27 | $this->championship = $championship; |
||
| 28 | $this->groupBy = $groupBy; |
||
| 29 | $this->settings = $championship->getSettings(); |
||
| 30 | $this->tree = new Collection(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Generate tree groups for a championship. |
||
| 35 | * |
||
| 36 | * @throws TreeGenerationException |
||
| 37 | */ |
||
| 38 | public function run() |
||
| 39 | { |
||
| 40 | $usersByArea = $this->getFightersByArea(); |
||
| 41 | $numFighters = sizeof($usersByArea->collapse()); |
||
| 42 | |||
| 43 | $this->generateGroupsForRound($usersByArea, 1, 1); |
||
| 44 | $this->pushEmptyGroupsToTree($numFighters); |
||
|
|
|||
| 45 | $this->addParentToChildren($numFighters); |
||
| 46 | // Now add parents to all |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the biggest entity group |
||
| 51 | * @param $userGroups |
||
| 52 | * |
||
| 53 | * @return int |
||
| 54 | */ |
||
| 55 | private function getMaxFightersByEntity($userGroups): int |
||
| 56 | { |
||
| 57 | return $userGroups |
||
| 58 | ->sortByDesc(function ($group) { |
||
| 59 | return $group->count(); |
||
| 60 | }) |
||
| 61 | ->first() |
||
| 62 | ->count(); |
||
| 63 | |||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get Competitor's list ordered by entities |
||
| 68 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
| 69 | * |
||
| 70 | * @param $fighters |
||
| 71 | * @return Collection |
||
| 72 | */ |
||
| 73 | private function getFightersByEntity($fighters): Collection |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the size the first round will have |
||
| 89 | * @param $fighterCount |
||
| 90 | * @param $groupSize |
||
| 91 | * @return int |
||
| 92 | */ |
||
| 93 | protected function getTreeSize($fighterCount, $groupSize) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Repart BYE in the tree, |
||
| 110 | * @param $fighterGroups |
||
| 111 | * @param int $max |
||
| 112 | * |
||
| 113 | * @return Collection |
||
| 114 | */ |
||
| 115 | private function repart($fighterGroups, $max) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Insert byes in an homogen way. |
||
| 132 | * |
||
| 133 | * @param Collection $fighters |
||
| 134 | * @param Collection $byeGroup |
||
| 135 | * |
||
| 136 | * @return Collection |
||
| 137 | */ |
||
| 138 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param Collection $usersByArea |
||
| 154 | * @param integer $area |
||
| 155 | * @param integer $round |
||
| 156 | * @param integer $shuffle |
||
| 157 | * |
||
| 158 | */ |
||
| 159 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param $area |
||
| 180 | * @param $order |
||
| 181 | * @param $round |
||
| 182 | * @param $parent |
||
| 183 | * @return FightersGroup |
||
| 184 | */ |
||
| 185 | protected function saveGroup($area, $order, $round, $parent): FightersGroup |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param integer $groupSize |
||
| 202 | * @return Collection |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function createByeGroup($groupSize): Collection |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param $fighters |
||
| 216 | * @param Collection $fighterGroups |
||
| 217 | * @return Collection |
||
| 218 | */ |
||
| 219 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get All Groups on previous round |
||
| 237 | * @param $currentRound |
||
| 238 | * @return Collection |
||
| 239 | */ |
||
| 240 | private function getPreviousRound($currentRound) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get the next group on the right ( parent ), final round being the ancestor |
||
| 248 | * @param $matchNumber |
||
| 249 | * @param Collection $previousRound |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | private function getParentGroup($matchNumber, $previousRound) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Save Groups with their parent info |
||
| 261 | * @param integer $numRounds |
||
| 262 | * @param $numFightersElim |
||
| 263 | */ |
||
| 264 | protected function pushGroups($numRounds, $numFightersElim) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Group Fighters by area |
||
| 278 | * @return Collection |
||
| 279 | * @throws TreeGenerationException |
||
| 280 | */ |
||
| 281 | private function getFightersByArea() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Attach a parent to every child for nestedSet Navigation |
||
| 302 | * @param $numFightersElim |
||
| 303 | */ |
||
| 304 | private function addParentToChildren($numFightersElim) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Create Bye Groups to adjust tree |
||
| 327 | * @param $byeCount |
||
| 328 | * @return Collection |
||
| 329 | */ |
||
| 330 | View Code Duplication | protected function createNullsGroup($byeCount): Collection |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param Collection $fighters |
||
| 342 | * @param $frequency |
||
| 343 | * @param $sizeGroupBy |
||
| 344 | * @param $bye |
||
| 345 | * @return Collection |
||
| 346 | */ |
||
| 347 | private function getFullFighterList(Collection $fighters, $frequency, $sizeGroupBy, $bye): Collection |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $frequency |
||
| 365 | * @param $sizeGroupBy |
||
| 366 | * @param $count |
||
| 367 | * @param $byeCount |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | private function shouldInsertBye($frequency, $sizeGroupBy, $count, $byeCount): bool |
||
| 374 | } |
||
| 375 |
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: