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 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | View Code Duplication | public function getFighter1Name() |
|
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | View Code Duplication | public function getFighter2Name() |
|
220 | |||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | View Code Duplication | public function getFighter1ShortId() |
|
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | View Code Duplication | public function getFighter2ShortId() |
|
246 | |||
247 | /** |
||
248 | * Update parent Fight |
||
249 | */ |
||
250 | public function updateParentFight($fighterToUpdate, $fight) |
||
264 | |||
265 | /** |
||
266 | * Returns the parent field that need to be updated |
||
267 | * @return null|string |
||
268 | */ |
||
269 | public function getParentFighterToUpdate() |
||
285 | |||
286 | /** |
||
287 | * In the original fight ( child ) return the field that contains data to copy to parent |
||
288 | * @return null|string |
||
289 | */ |
||
290 | public function getValueToUpdate() |
||
297 | } |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: