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  | 
            ||
| 12 | class TreeGen implements TreeGenerable  | 
            ||
| 13 | { | 
            ||
| 14 | protected $groupBy;  | 
            ||
| 15 | protected $tree;  | 
            ||
| 16 | public $championship;  | 
            ||
| 17 | public $settings;  | 
            ||
| 18 | protected $numFighters;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * @param Championship $championship  | 
            ||
| 22 | * @param $groupBy  | 
            ||
| 23 | */  | 
            ||
| 24 | public function __construct(Championship $championship, $groupBy)  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * Generate tree groups for a championship.  | 
            ||
| 34 | *  | 
            ||
| 35 | * @throws TreeGenerationException  | 
            ||
| 36 | */  | 
            ||
| 37 | public function run()  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Get the biggest entity group  | 
            ||
| 50 | * @param $userGroups  | 
            ||
| 51 | *  | 
            ||
| 52 | * @return int  | 
            ||
| 53 | */  | 
            ||
| 54 | private function getMaxFightersByEntity($userGroups): int  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * Get Competitor's list ordered by entities  | 
            ||
| 67 | * Countries for Internation Tournament, State for a National Tournament, etc.  | 
            ||
| 68 | *  | 
            ||
| 69 | * @return Collection  | 
            ||
| 70 | */  | 
            ||
| 71 | private function getFightersByEntity($fighters): Collection  | 
            ||
| 84 | |||
| 85 | /**  | 
            ||
| 86 | * Get the size the first round will have  | 
            ||
| 87 | * @param $fighterCount  | 
            ||
| 88 | * @return int  | 
            ||
| 89 | */  | 
            ||
| 90 | protected function getTreeSize($fighterCount, $groupSize)  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * Repart BYE in the tree,  | 
            ||
| 107 | * @param $fighterGroups  | 
            ||
| 108 | * @param int $max  | 
            ||
| 109 | *  | 
            ||
| 110 | * @return Collection  | 
            ||
| 111 | */  | 
            ||
| 112 | private function repart($fighterGroups, $max)  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * Insert byes in an homogen way.  | 
            ||
| 129 | *  | 
            ||
| 130 | * @param Collection $fighters  | 
            ||
| 131 | * @param Collection $byeGroup  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return Collection  | 
            ||
| 134 | */  | 
            ||
| 135 | private function insertByes(Collection $fighters, Collection $byeGroup)  | 
            ||
| 160 | |||
| 161 | /**  | 
            ||
| 162 | * @param Collection $usersByArea  | 
            ||
| 163 | * @param integer $area  | 
            ||
| 164 | * @param integer $round  | 
            ||
| 165 | * @param integer $shuffle  | 
            ||
| 166 | *  | 
            ||
| 167 | */  | 
            ||
| 168 | public function generateGroupsForRound($usersByArea, $area, $round, $shuffle)  | 
            ||
| 186 | |||
| 187 | /**  | 
            ||
| 188 | * @param $area  | 
            ||
| 189 | * @param $order  | 
            ||
| 190 | * @param $round  | 
            ||
| 191 | * @param $parent  | 
            ||
| 192 | * @return FightersGroup  | 
            ||
| 193 | */  | 
            ||
| 194 | protected function saveGroup($area, $order, $round, $parent): FightersGroup  | 
            ||
| 207 | |||
| 208 | |||
| 209 | /**  | 
            ||
| 210 | * @param integer $groupSize  | 
            ||
| 211 | * @return Collection  | 
            ||
| 212 | */  | 
            ||
| 213 | View Code Duplication | public function createByeGroup($groupSize): Collection  | 
            |
| 222 | |||
| 223 | /**  | 
            ||
| 224 | * @param $fighters  | 
            ||
| 225 | * @param Collection $fighterGroups  | 
            ||
| 226 | * @return Collection  | 
            ||
| 227 | */  | 
            ||
| 228 | public function adjustFightersGroupWithByes($fighters, $fighterGroups): Collection  | 
            ||
| 243 | |||
| 244 | /**  | 
            ||
| 245 | * Get All Groups on previous round  | 
            ||
| 246 | * @param $currentRound  | 
            ||
| 247 | * @return Collection  | 
            ||
| 248 | */  | 
            ||
| 249 | private function getPreviousRound($currentRound)  | 
            ||
| 254 | |||
| 255 | /**  | 
            ||
| 256 | * Get the next group on the right ( parent ), final round being the ancestor  | 
            ||
| 257 | * @param $matchNumber  | 
            ||
| 258 | * @param Collection $previousRound  | 
            ||
| 259 | * @return mixed  | 
            ||
| 260 | */  | 
            ||
| 261 | private function getParentGroup($matchNumber, $previousRound)  | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * Save Groups with their parent info  | 
            ||
| 270 | * @param integer $numRounds  | 
            ||
| 271 | * @param $numFightersEliminatory  | 
            ||
| 272 | */  | 
            ||
| 273 | protected function pushGroups($numRounds, $numFightersEliminatory)  | 
            ||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * Group Fighters by area  | 
            ||
| 287 | * @return Collection  | 
            ||
| 288 | * @throws TreeGenerationException  | 
            ||
| 289 | */  | 
            ||
| 290 | private function getFightersByArea()  | 
            ||
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * Attach a parent to every child for nestedSet Navigation  | 
            ||
| 311 | */  | 
            ||
| 312 | private function addParentToChildren($numFightersEliminatory)  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * Create Bye Groups to adjust tree  | 
            ||
| 335 | * @param $byeCount  | 
            ||
| 336 | * @return Collection  | 
            ||
| 337 | */  | 
            ||
| 338 | View Code Duplication | protected function createNullsGroup($byeCount): Collection  | 
            |
| 347 | }  | 
            ||
| 348 | 
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: