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 |
||
8 | class Fight extends Model |
||
9 | { |
||
10 | /** |
||
11 | * Fight constructor. |
||
12 | */ |
||
13 | public function __construct($userId1 = null, $userId2 = null) |
||
18 | |||
19 | protected $table = 'fight'; |
||
20 | public $timestamps = true; |
||
21 | |||
22 | protected $fillable = [ |
||
23 | 'group_id', |
||
24 | 'c1', |
||
25 | 'c2', |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @param Championship $championship |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
||
58 | |||
59 | /** |
||
60 | * Get First Fighter. |
||
61 | * |
||
62 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
63 | */ |
||
64 | public function competitor1() |
||
68 | |||
69 | /** |
||
70 | * Get Second Fighter. |
||
71 | * |
||
72 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
73 | */ |
||
74 | public function competitor2() |
||
78 | |||
79 | /** |
||
80 | * Get First Fighter. |
||
81 | * |
||
82 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
83 | */ |
||
84 | public function team1() |
||
88 | |||
89 | /** |
||
90 | * Get Second Fighter. |
||
91 | * |
||
92 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
93 | */ |
||
94 | public function team2() |
||
98 | |||
99 | /** |
||
100 | * Save a Fight. |
||
101 | * |
||
102 | * @param Collection $groups |
||
103 | * @param int $numGroup |
||
104 | */ |
||
105 | public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
||
146 | |||
147 | |||
148 | /** |
||
149 | * @param Collection $groups |
||
150 | */ |
||
151 | public static function saveGroupFights(Championship $championship, $groups) |
||
185 | } |
||
186 |
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.