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 | View Code Duplication | class Link extends Model |
|
1 ignored issue
–
show
|
|||
9 | { |
||
10 | use SoftDeletes; |
||
11 | |||
12 | /** |
||
13 | * The table associated with the model. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $table = 'links'; |
||
18 | |||
19 | /** |
||
20 | * The attributes that should be mutated to dates. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $dates = ['deleted_at']; |
||
25 | |||
26 | /** |
||
27 | * The attributes that are mass assignable. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $fillable = ['name', 'text', 'url', 'order']; |
||
32 | |||
33 | /** |
||
34 | * A link belongs to a single project. |
||
35 | * |
||
36 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
37 | */ |
||
38 | public function project() |
||
42 | |||
43 | /** |
||
44 | * Return the link id. |
||
45 | * |
||
46 | * @return int |
||
47 | */ |
||
48 | public function id() |
||
52 | |||
53 | /** |
||
54 | * Return the link name. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function name() |
||
62 | |||
63 | /** |
||
64 | * Return the link text. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function text() |
||
72 | |||
73 | /** |
||
74 | * Return the link url. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function url() |
||
82 | |||
83 | /** |
||
84 | * Return the link order value. |
||
85 | * |
||
86 | * @return int |
||
87 | */ |
||
88 | public function order() |
||
92 | } |
||
93 |
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.