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 |
||
9 | class Team extends DsManagerOrm |
||
10 | { |
||
11 | /** |
||
12 | * |
||
13 | */ |
||
14 | const PLAYED_LIMIT = 5; |
||
15 | /** |
||
16 | * |
||
17 | */ |
||
18 | const FUTURE_LIMIT = 3; |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | */ |
||
23 | const TEAM_STATS_LIMIT = 5; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $table = 'teams'; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $fillable = [ |
||
34 | 'name', |
||
35 | 'nationality' |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
40 | */ |
||
41 | public function roster() |
||
45 | |||
46 | /** |
||
47 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
48 | */ |
||
49 | public function coach() |
||
53 | |||
54 | /** |
||
55 | * @return mixed |
||
56 | */ |
||
57 | View Code Duplication | public function playedMatchesHome() |
|
66 | |||
67 | /** |
||
68 | * @return mixed |
||
69 | */ |
||
70 | View Code Duplication | public function futureMatchesHome() |
|
79 | |||
80 | /** |
||
81 | * @return mixed |
||
82 | */ |
||
83 | View Code Duplication | public function playedMatchesAway() |
|
92 | |||
93 | /** |
||
94 | * @return mixed |
||
95 | */ |
||
96 | View Code Duplication | public function futureMatchesAway() |
|
105 | |||
106 | |||
107 | /** |
||
108 | * @param $query |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function scopeComplete($query) |
||
126 | |||
127 | /** |
||
128 | * @return array |
||
129 | */ |
||
130 | View Code Duplication | public static function getBest() |
|
144 | |||
145 | } |
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.