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 | /** |
||
32 | * Get First Fighter. |
||
33 | * |
||
34 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
35 | */ |
||
36 | public function group() |
||
40 | |||
41 | /** |
||
42 | * @param FightersGroup|null $group |
||
43 | * @return Collection |
||
44 | * @internal param Championship $championship |
||
45 | * |
||
46 | */ |
||
47 | protected static function getFightersWithNull(FightersGroup $group) |
||
48 | { |
||
49 | if ($group == null) return null; |
||
50 | $fighters = $group->getFighters(); |
||
51 | $fighterType = $group->getFighterType(); |
||
52 | if (sizeof($fighters) == 0) { |
||
53 | $fighters->push(new $fighterType); |
||
54 | $fighters->push(new $fighterType); |
||
55 | } else if (count($fighters) % 2 != 0) { |
||
56 | $fighters->push(new $fighterType); |
||
57 | } |
||
58 | |||
59 | return $fighters; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get First Fighter. |
||
64 | * |
||
65 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
66 | */ |
||
67 | public function competitor1() |
||
71 | |||
72 | /** |
||
73 | * Get Second Fighter. |
||
74 | * |
||
75 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
76 | */ |
||
77 | public function competitor2() |
||
81 | |||
82 | /** |
||
83 | * Get First Fighter. |
||
84 | * |
||
85 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
86 | */ |
||
87 | public function team1() |
||
91 | |||
92 | /** |
||
93 | * Get Second Fighter. |
||
94 | * |
||
95 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
96 | */ |
||
97 | public function team2() |
||
101 | |||
102 | |||
103 | public function getFighterAttr($numFighter, $attr) |
||
120 | |||
121 | /** |
||
122 | * Update parent Fight |
||
123 | * @param $fighterToUpdate |
||
124 | * @param $fight |
||
125 | */ |
||
126 | public function updateParentFight($fighterToUpdate, $fight) |
||
141 | |||
142 | /** |
||
143 | * Returns the parent field that need to be updated |
||
144 | * @return null|string |
||
145 | */ |
||
146 | public function getParentFighterToUpdate() |
||
162 | |||
163 | /** |
||
164 | * In the original fight ( child ) return the field that contains data to copy to parent |
||
165 | * @return null|string |
||
166 | */ |
||
167 | public function getValueToUpdate() |
||
180 | |||
181 | /** |
||
182 | * Check if we are able to fill the parent fight or not |
||
183 | * If one of the children has c1 x c2, then we must wait to fill parent |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function hasDeterminedParent() |
||
196 | |||
197 | public function shouldBeInFightList() |
||
213 | |||
214 | /** |
||
215 | * return true if fight has 2 fighters ( No BYE ) |
||
216 | * @return bool |
||
217 | */ |
||
218 | private function has2Fighters(): bool |
||
222 | |||
223 | private function belongsToFirstRound() |
||
229 | |||
230 | private function dontHave2Fighters() // 1 or 0 |
||
234 | |||
235 | |||
236 | public static function generateFightsId($championship) |
||
243 | |||
244 | /** |
||
245 | * @param $order |
||
246 | * @return int |
||
247 | */ |
||
248 | public function updateShortId($order) |
||
257 | |||
258 | } |
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.