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() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the biggest entity group |
||
| 54 | * @param $userGroups |
||
| 55 | * |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | private function getMaxFightersByEntity($userGroups): int |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get Competitor's list ordered by entities |
||
| 71 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
| 72 | * |
||
| 73 | * @param $fighters |
||
| 74 | * @return Collection |
||
| 75 | */ |
||
| 76 | private function getFightersByEntity($fighters): Collection |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the size the first round will have |
||
| 92 | * @param $fighterCount |
||
| 93 | * @param $groupSize |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | protected function getTreeSize($fighterCount, $groupSize) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Repart BYE in the tree, |
||
| 113 | * @param $fighterGroups |
||
| 114 | * @param int $max |
||
| 115 | * |
||
| 116 | * @return Collection |
||
| 117 | */ |
||
| 118 | private function repart($fighterGroups, $max) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Insert byes in an homogen way. |
||
| 135 | * |
||
| 136 | * @param Collection $fighters |
||
| 137 | * @param Collection $byeGroup |
||
| 138 | * |
||
| 139 | * @return Collection |
||
| 140 | */ |
||
| 141 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Collection $usersByArea |
||
| 157 | * @param integer $area |
||
| 158 | * @param integer $round |
||
| 159 | * |
||
| 160 | */ |
||
| 161 | public function generateGroupsForRound($usersByArea, $area, $round) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $area |
||
| 182 | * @param $order |
||
| 183 | * @param $round |
||
| 184 | * @param $parent |
||
| 185 | * @return FightersGroup |
||
| 186 | */ |
||
| 187 | protected function saveGroup($area, $order, $round, $parent): FightersGroup |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @param integer $groupSize |
||
| 204 | * @return Collection |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function createByeGroup($groupSize): Collection |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param $fighters |
||
| 218 | * @param Collection $fighterGroups |
||
| 219 | * @return Collection |
||
| 220 | */ |
||
| 221 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get All Groups on previous round |
||
| 239 | * @param $currentRound |
||
| 240 | * @return Collection |
||
| 241 | */ |
||
| 242 | private function getPreviousRound($currentRound) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the next group on the right ( parent ), final round being the ancestor |
||
| 250 | * @param $matchNumber |
||
| 251 | * @param Collection $previousRound |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | private function getParentGroup($matchNumber, $previousRound) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Save Groups with their parent info |
||
| 263 | * @param integer $numRounds |
||
| 264 | * @param $numFightersElim |
||
| 265 | */ |
||
| 266 | protected function pushGroups($numRounds, $numFightersElim) |
||
| 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) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Create Bye Groups to adjust tree |
||
| 329 | * @param $byeCount |
||
| 330 | * @return Collection |
||
| 331 | */ |
||
| 332 | View Code Duplication | protected function createNullsGroup($byeCount): Collection |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param Collection $fighters |
||
| 344 | * @param $frequency |
||
| 345 | * @param $sizeGroupBy |
||
| 346 | * @param $bye |
||
| 347 | * @return Collection |
||
| 348 | */ |
||
| 349 | private function getFullFighterList(Collection $fighters, $frequency, $sizeGroupBy, $bye): Collection |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param $frequency |
||
| 367 | * @param $sizeGroupBy |
||
| 368 | * @param $count |
||
| 369 | * @param $byeCount |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | private function shouldInsertBye($frequency, $sizeGroupBy, $count, $byeCount): bool |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * @param Championship $championship |
||
| 380 | */ |
||
| 381 | protected static function destroyPreviousFights(Championship $championship) |
||
| 387 | } |
||
| 388 |
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: