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 |
||
20 | View Code Duplication | class Usergroup extends Model |
|
|
|||
21 | { |
||
22 | use SoftDeletes, AdminModelTrait; |
||
23 | |||
24 | /** |
||
25 | * The database table used by the model. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $table = 'usergroup'; |
||
30 | |||
31 | /** |
||
32 | * The attributes that should be mutated to dates. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
37 | |||
38 | /** |
||
39 | * The attributes that are mass assignable. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $fillable = ['name']; |
||
44 | |||
45 | /** |
||
46 | * Fields to search in fulltext mode |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $fulltextFields = [ |
||
51 | 'id', |
||
52 | 'name' => [ |
||
53 | 'operator' => 'LIKE', |
||
54 | 'prefix' => '%', |
||
55 | 'sufix' => '%' |
||
56 | ], |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * Default order by |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $defaultOrderBy = [ |
||
65 | 'id' => 'desc' |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * Slide link |
||
70 | * |
||
71 | * @return object |
||
72 | */ |
||
73 | public function users(){ |
||
77 | |||
78 | /** |
||
79 | * Process relationships |
||
80 | * |
||
81 | * @param query $query |
||
82 | * @return query |
||
83 | */ |
||
84 | public function scopeRelationships($query){ |
||
88 | } |
||
89 |
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.