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:
| 1 | <?php | ||
| 11 | abstract class DirectEliminationTreeGen extends TreeGen | ||
| 12 | { | ||
| 13 | /** | ||
| 14 | * Calculate the Byes need to fill the Championship Tree. | ||
| 15 | * | ||
| 16 | * @param $fighters | ||
| 17 | * | ||
| 18 | * @return Collection | ||
| 19 | */ | ||
| 20 | View Code Duplication | protected function getByeGroup($fighters) | |
|  | |||
| 21 |     { | ||
| 22 | $fighterCount = $fighters->count(); | ||
| 23 | $firstRoundGroupSize = $this->firstRoundGroupSize(); | ||
| 24 | $treeSize = $this->getTreeSize($fighterCount, $firstRoundGroupSize); | ||
| 25 | $byeCount = $treeSize - $fighterCount; | ||
| 26 | return $this->createByeGroup($byeCount); | ||
| 27 | } | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Save Groups with their parent info. | ||
| 31 | * | ||
| 32 | * @param int $numRounds | ||
| 33 | * @param int $numFighters | ||
| 34 | */ | ||
| 35 | View Code Duplication | protected function pushGroups($numRounds, $numFighters) | |
| 36 |     { | ||
| 37 | // TODO Here is where you should change when enable several winners for preliminary | ||
| 38 |         for ($roundNumber = 2; $roundNumber <= $numRounds + 1; $roundNumber++) { | ||
| 39 | // From last match to first match | ||
| 40 | $maxMatches = ($numFighters / pow(2, $roundNumber)); | ||
| 41 | |||
| 42 |             for ($matchNumber = 1; $matchNumber <= $maxMatches; $matchNumber++) { | ||
| 43 | $fighters = $this->createByeGroup(2); | ||
| 44 | $group = $this->saveGroup($matchNumber, $roundNumber, null); | ||
| 45 | $this->syncGroup($group, $fighters); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Create empty groups for direct Elimination Tree. | ||
| 52 | * | ||
| 53 | * @param $numFighters | ||
| 54 | */ | ||
| 55 | protected function pushEmptyGroupsToTree($numFighters) | ||
| 56 |     { | ||
| 57 |         if ($this->championship->hasPreliminary()) { | ||
| 58 | $numFightersElim = $numFighters / $this->championship->getSettings()->preliminaryGroupSize * 2; | ||
| 59 | // We calculate how much rounds we will have | ||
| 60 | $numRounds = intval(log($numFightersElim, 2)); // 3 rounds, but begining from round 2 ( ie => 4) | ||
| 61 | return $this->pushGroups($numRounds, $numFightersElim); | ||
| 62 | } | ||
| 63 | // We calculate how much rounds we will have | ||
| 64 | $numRounds = $this->getNumRounds($numFighters); | ||
| 65 | |||
| 66 | return $this->pushGroups($numRounds, $numFighters); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Chunk Fighters into groups for fighting, and optionnaly shuffle. | ||
| 71 | * | ||
| 72 | * @param $fightersByEntity | ||
| 73 | * | ||
| 74 | * @return Collection|null | ||
| 75 | */ | ||
| 76 | protected function chunkAndShuffle(Collection $fightersByEntity) | ||
| 77 |     { | ||
| 78 | //TODO Should Pull down to know if team or competitor | ||
| 79 |         if ($this->championship->hasPreliminary()) { | ||
| 80 | return (new PlayOffCompetitorTreeGen($this->championship, null))->chunkAndShuffle($fightersByEntity); | ||
| 81 | } | ||
| 82 | $fightersGroup = null; | ||
| 83 | |||
| 84 | $fightersGroup = $fightersByEntity->chunk(2); | ||
| 85 |         if (!app()->runningUnitTests()) { | ||
| 86 | $fightersGroup = $fightersGroup->shuffle(); | ||
| 87 | } | ||
| 88 | |||
| 89 | return $fightersGroup; | ||
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Generate First Round Fights. | ||
| 94 | */ | ||
| 95 | protected function generateFights() | ||
| 96 |     { | ||
| 97 | // First Round Fights | ||
| 98 | $settings = $this->championship->getSettings(); | ||
| 99 | $initialRound = 1; | ||
| 100 | |||
| 101 | // Very specific case to common case : Preliminary with 3 fighters | ||
| 102 |         if ($this->championship->hasPreliminary() && $settings->preliminaryGroupSize == 3) { | ||
| 103 | // First we make all first fights of all groups | ||
| 104 | // Then we make all second fights of all groups | ||
| 105 | // Then we make all third fights of all groups | ||
| 106 | $groups = $this->championship->groupsByRound(1)->get(); | ||
| 107 |             for ($numFight = 1; $numFight <= $settings->preliminaryGroupSize; $numFight++) { | ||
| 108 | $fight = new PreliminaryFight(); | ||
| 109 | $fight->saveFights($groups, $numFight); | ||
| 110 | } | ||
| 111 | $initialRound++; | ||
| 112 | } | ||
| 113 | // Save Next rounds | ||
| 114 | $fight = new DirectEliminationFight(); | ||
| 115 | $fight->saveFights($this->championship, $initialRound); | ||
| 116 | } | ||
| 117 | |||
| 118 | /** | ||
| 119 | * Return number of rounds for the tree based on fighter count. | ||
| 120 | * | ||
| 121 | * @param $numFighters | ||
| 122 | * | ||
| 123 | * @return int | ||
| 124 | */ | ||
| 125 | protected function getNumRounds($numFighters) | ||
| 129 | |||
| 130 | private function firstRoundGroupSize() | ||
| 136 | |||
| 137 | protected function generateAllTrees() | ||
| 138 |     { | ||
| 139 | $this->minFightersCheck(); | ||
| 140 | $usersByArea = $this->getFightersByArea(); | ||
| 141 | $numFighters = count($usersByArea->collapse()); | ||
| 147 | |||
| 148 | /** | ||
| 149 | * @param Collection $usersByArea | ||
| 150 | * @param $round | ||
| 151 | */ | ||
| 152 | public function generateGroupsForRound(Collection $usersByArea, $round) | ||
| 169 | |||
| 170 | /** | ||
| 171 | * Check if there is enough fighters, throw exception otherwise | ||
| 172 | * @throws TreeGenerationException | ||
| 173 | */ | ||
| 174 | private function minFightersCheck() | ||
| 199 | } | ||
| 200 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.