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() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the biggest entity group |
||
| 52 | * @param $userGroups |
||
| 53 | * |
||
| 54 | * @return int |
||
| 55 | */ |
||
| 56 | private function getMaxFightersByEntity($userGroups): int |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get Competitor's list ordered by entities |
||
| 69 | * Countries for Internation Tournament, State for a National Tournament, etc. |
||
| 70 | * |
||
| 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 | * @return int |
||
| 91 | */ |
||
| 92 | protected function getTreeSize($fighterCount, $groupSize) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Repart BYE in the tree, |
||
| 109 | * @param $fighterGroups |
||
| 110 | * @param int $max |
||
| 111 | * |
||
| 112 | * @return Collection |
||
| 113 | */ |
||
| 114 | private function repart($fighterGroups, $max) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Insert byes in an homogen way. |
||
| 131 | * |
||
| 132 | * @param Collection $fighters |
||
| 133 | * @param Collection $byeGroup |
||
| 134 | * |
||
| 135 | * @return Collection |
||
| 136 | */ |
||
| 137 | private function insertByes(Collection $fighters, Collection $byeGroup) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param Collection $usersByArea |
||
| 165 | * @param integer $area |
||
| 166 | * @param integer $round |
||
| 167 | * @param integer $shuffle |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | public function generateGroupsForRound($usersByArea, $area, $round, $shuffle) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param $area |
||
| 191 | * @param $order |
||
| 192 | * @param $round |
||
| 193 | * @param $parent |
||
| 194 | * @return FightersGroup |
||
| 195 | */ |
||
| 196 | protected function saveGroup($area, $order, $round, $parent): FightersGroup |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param integer $groupSize |
||
| 213 | * @return Collection |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function createByeGroup($groupSize): Collection |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param $fighters |
||
| 227 | * @param Collection $fighterGroups |
||
| 228 | * @return Collection |
||
| 229 | */ |
||
| 230 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get All Groups on previous round |
||
| 248 | * @param $currentRound |
||
| 249 | * @return Collection |
||
| 250 | */ |
||
| 251 | private function getPreviousRound($currentRound) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the next group on the right ( parent ), final round being the ancestor |
||
| 259 | * @param $matchNumber |
||
| 260 | * @param Collection $previousRound |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | private function getParentGroup($matchNumber, $previousRound) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Save Groups with their parent info |
||
| 272 | * @param integer $numRounds |
||
| 273 | * @param $numFightersEliminatory |
||
| 274 | */ |
||
| 275 | protected function pushGroups($numRounds, $numFightersEliminatory, $shuffle = true) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Group Fighters by area |
||
| 289 | * @return Collection |
||
| 290 | * @throws TreeGenerationException |
||
| 291 | */ |
||
| 292 | private function getFightersByArea() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Chunk Fighters into groups for fighting, and optionnaly shuffle |
||
| 312 | * @param $round |
||
| 313 | * @param $shuffle |
||
| 314 | * @param $fightersByEntity |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | private function chunkAndShuffle($round, $shuffle, $fightersByEntity) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Attach a parent to every child for nestedSet Navigation |
||
| 337 | */ |
||
| 338 | private function addParentToChildren($numFightersEliminatory) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Create Bye Groups to adjust tree |
||
| 361 | * @param $byeCount |
||
| 362 | * @return Collection |
||
| 363 | */ |
||
| 364 | View Code Duplication | protected function createNullsGroup($byeCount): Collection |
|
| 373 | |||
| 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: