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 Fight 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 Fight, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Fight extends Model |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Fight constructor. |
||
| 12 | * @param int $userId1 |
||
| 13 | * @param int $userId2 |
||
| 14 | */ |
||
| 15 | public function __construct($userId1 = null, $userId2 = null) |
||
| 20 | |||
| 21 | protected $table = 'fight'; |
||
| 22 | public $timestamps = true; |
||
| 23 | |||
| 24 | protected $fillable = [ |
||
| 25 | 'group_id', |
||
| 26 | 'c1', |
||
| 27 | 'c2', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param $group |
||
| 32 | * @param $competitor1 |
||
| 33 | * @param $competitor2 |
||
| 34 | * @param $order |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | private static function createPreliminaryFight($group, $competitor1, $competitor2, $order) |
||
| 38 | { |
||
| 39 | $fight = new self(); |
||
| 40 | $fight->fighters_group_id = $group->id; |
||
| 41 | $fight->c1 = $competitor1 != null ? $competitor1->id : null; |
||
| 42 | $fight->c2 = $competitor2 != null ? $competitor2->id : null; |
||
| 43 | $fight->short_id = $order++; |
||
| 44 | $fight->area = $group->area; |
||
| 45 | $fight->save(); |
||
| 46 | return $order; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get First Fighter. |
||
| 51 | * |
||
| 52 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 53 | */ |
||
| 54 | public function group() |
||
| 55 | { |
||
| 56 | return $this->belongsTo(FightersGroup::class, 'fighters_group_id'); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Championship $championship |
||
| 61 | * |
||
| 62 | * @return Collection |
||
| 63 | */ |
||
| 64 | private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
||
| 65 | { |
||
| 66 | if ($group == null) return null; |
||
| 67 | $fighters = $group->getFighters(); |
||
| 68 | $fighterType = $group->getFighterType(); |
||
| 69 | if (sizeof($fighters) == 0) { |
||
| 70 | $fighters->push(new $fighterType); |
||
| 71 | $fighters->push(new $fighterType); |
||
| 72 | } else if (count($fighters) % 2 != 0) { |
||
| 73 | $fighters->push(new $fighterType); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $fighters; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get First Fighter. |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 83 | */ |
||
| 84 | public function competitor1() |
||
| 85 | { |
||
| 86 | return $this->belongsTo(Competitor::class, 'c1', 'id'); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get Second Fighter. |
||
| 91 | * |
||
| 92 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 93 | */ |
||
| 94 | public function competitor2() |
||
| 95 | { |
||
| 96 | return $this->belongsTo(Competitor::class, 'c2', 'id'); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get First Fighter. |
||
| 101 | * |
||
| 102 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 103 | */ |
||
| 104 | public function team1() |
||
| 105 | { |
||
| 106 | return $this->belongsTo(Team::class, 'c1', 'id'); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get Second Fighter. |
||
| 111 | * |
||
| 112 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 113 | */ |
||
| 114 | public function team2() |
||
| 115 | { |
||
| 116 | return $this->belongsTo(Team::class, 'c2', 'id'); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Save a Fight. |
||
| 121 | * |
||
| 122 | * @param Collection $groups |
||
| 123 | * @param int $numGroup |
||
| 124 | */ |
||
| 125 | public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
||
| 126 | { |
||
| 127 | $competitor1 = $competitor2 = null; |
||
| 128 | $order = 1; |
||
| 129 | |||
| 130 | foreach ($groups as $group) { |
||
| 131 | |||
| 132 | $fighters = $group->getFighters(); |
||
| 133 | |||
| 134 | $fighter1 = $fighters->get(0); |
||
| 135 | $fighter2 = $fighters->get(1); |
||
| 136 | $fighter3 = $fighters->get(2); |
||
| 137 | |||
| 138 | switch ($numGroup) { |
||
| 139 | case 1: |
||
| 140 | $competitor1 = $fighter1; |
||
| 141 | $competitor2 = $fighter2; |
||
| 142 | break; |
||
| 143 | case 2: |
||
| 144 | $competitor1 = $fighter2; |
||
| 145 | $competitor2 = $fighter3; |
||
| 146 | break; |
||
| 147 | case 3: |
||
| 148 | $competitor1 = $fighter3; |
||
| 149 | $competitor2 = $fighter1; |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | $order = self::createPreliminaryFight($group, $competitor1, $competitor2, $order); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Championship $championship |
||
| 159 | */ |
||
| 160 | public static function saveGroupFights(Championship $championship) |
||
| 161 | { |
||
| 162 | // $order = 1; |
||
|
|
|||
| 163 | $round = []; |
||
| 164 | foreach ($championship->fightersGroups()->get() as $group) { |
||
| 165 | $fighters = self::getActorsToFights($championship, $group); |
||
| 166 | $away = $fighters->splice(count($fighters) / 2); // 2 |
||
| 167 | $home = $fighters; // 1 |
||
| 168 | |||
| 169 | $countHome = count($home); |
||
| 170 | $countAway = count($away); |
||
| 171 | for ($i = 0; $i < $countHome + $countAway - 1; $i++) { |
||
| 172 | for ($j = 0; $j < $countHome; $j++) { |
||
| 173 | |||
| 174 | $round[$i][$j]['Home'] = $home[$j]; |
||
| 175 | $round[$i][$j]['Away'] = $away[$j]; |
||
| 176 | $fight = new self(); |
||
| 177 | $fight->fighters_group_id = $group->id; |
||
| 178 | $fight->c1 = $round[$i][$j]['Home']->id; |
||
| 179 | $fight->c2 = $round[$i][$j]['Away']->id; |
||
| 180 | $fight->area = $group->area; |
||
| 181 | $fight->save(); |
||
| 182 | |||
| 183 | } |
||
| 184 | if ($countHome + $countAway - 1 > 2) { |
||
| 185 | $away->prepend($home->splice(1, 1)->shift()); |
||
| 186 | $home->push($away->pop()); |
||
| 187 | // $order++; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getFighterAttr($numFighter, $attr) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Update parent Fight |
||
| 213 | * @param $fighterToUpdate |
||
| 214 | * @param $fight |
||
| 215 | */ |
||
| 216 | public function updateParentFight($fighterToUpdate, $fight) |
||
| 217 | { |
||
| 218 | if ($fight != null) { |
||
| 219 | View Code Duplication | if (($fight->c1 != null || $fight->c2 == null)) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the parent field that need to be updated |
||
| 234 | * @return null|string |
||
| 235 | */ |
||
| 236 | public function getParentFighterToUpdate() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * In the original fight ( child ) return the field that contains data to copy to parent |
||
| 255 | * @return null|string |
||
| 256 | */ |
||
| 257 | public function getValueToUpdate() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Check if we are able to fill the parent fight or not |
||
| 273 | * If one of the children has c1 x c2, then we must wait to fill parent |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasDeterminedParent() |
||
| 286 | |||
| 287 | public function shouldBeInFightList() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * return true if fight has 2 fighters ( No BYE ) |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | private function has2Fighters(): bool |
||
| 312 | |||
| 313 | private function belongsToFirstRound() |
||
| 319 | |||
| 320 | private function dontHave2Fighters() // 1 or 0 |
||
| 324 | |||
| 325 | |||
| 326 | public static function generateFightsId($championship) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param $order |
||
| 336 | * @return int |
||
| 337 | */ |
||
| 338 | public function updateShortId($order) |
||
| 347 | |||
| 348 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.