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 TextLine 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 = 'text_lines'; |
||
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', 'order']; |
||
32 | |||
33 | /** |
||
34 | * Return the text block id. |
||
35 | * |
||
36 | * @return int |
||
37 | */ |
||
38 | public function id() |
||
42 | |||
43 | /** |
||
44 | * Return the text block name. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function name() |
||
52 | |||
53 | /** |
||
54 | * Return the raw block text. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | public function text() |
||
62 | |||
63 | /** |
||
64 | * Return the text block order value. |
||
65 | * |
||
66 | * @return int |
||
67 | */ |
||
68 | public function order() |
||
72 | } |
||
73 |
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.