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 | * Get First Fighter. |
||
| 32 | * |
||
| 33 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 34 | */ |
||
| 35 | public function group() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Championship $championship |
||
| 42 | * |
||
| 43 | * @return Collection |
||
| 44 | */ |
||
| 45 | private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get First Fighter. |
||
| 73 | * |
||
| 74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 75 | */ |
||
| 76 | public function competitor1() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get Second Fighter. |
||
| 83 | * |
||
| 84 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 85 | */ |
||
| 86 | public function competitor2() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get First Fighter. |
||
| 93 | * |
||
| 94 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 95 | */ |
||
| 96 | public function team1() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get Second Fighter. |
||
| 103 | * |
||
| 104 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 105 | */ |
||
| 106 | public function team2() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Save a Fight. |
||
| 113 | * |
||
| 114 | * @param Collection $groups |
||
| 115 | * @param int $numGroup |
||
| 116 | */ |
||
| 117 | public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * @param Championship $championship |
||
| 161 | */ |
||
| 162 | public static function saveGroupFights(Championship $championship) |
||
| 192 | |||
| 193 | public function getFighterAttr($numFighter, $attr) |
||
| 210 | |||
| 211 | // public function getFighterShortId($numFighter,$attr) |
||
| 212 | // { |
||
| 213 | // $isTeam = $this->group->championship->category->isTeam; |
||
| 214 | // if ($isTeam) { |
||
| 215 | // $teamToUpdate = 'team' . $numFighter; |
||
| 216 | // return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr; |
||
| 217 | // } |
||
| 218 | // $competitorToUpdate = 'competitor' . $numFighter; |
||
| 219 | // return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id; |
||
| 220 | // } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Update parent Fight |
||
| 224 | */ |
||
| 225 | public function updateParentFight($fighterToUpdate, $fight) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns the parent field that need to be updated |
||
| 241 | * @return null|string |
||
| 242 | */ |
||
| 243 | public function getParentFighterToUpdate() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * In the original fight ( child ) return the field that contains data to copy to parent |
||
| 262 | * @return null|string |
||
| 263 | */ |
||
| 264 | public function getValueToUpdate() |
||
| 277 | |||
| 278 | } |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: