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 |
||
18 | class Menu extends Model |
||
19 | { |
||
20 | use AdminBuilder, ModelTree { |
||
21 | ModelTree::boot as treeBoot; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * The attributes that are mass assignable. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission']; |
||
30 | |||
31 | /** |
||
32 | * Create a new Eloquent model instance. |
||
33 | * |
||
34 | * @param array $attributes |
||
35 | */ |
||
36 | View Code Duplication | public function __construct(array $attributes = []) |
|
46 | |||
47 | /** |
||
48 | * A Menu belongs to many roles. |
||
49 | * |
||
50 | * @return BelongsToMany |
||
51 | */ |
||
52 | public function roles() : BelongsToMany |
||
60 | |||
61 | /** |
||
62 | * @return array |
||
63 | */ |
||
64 | public function allNodes() : array |
||
73 | |||
74 | /** |
||
75 | * determine if enable menu bind permission. |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function withPermission() |
||
83 | |||
84 | /** |
||
85 | * Detach models from the relationship. |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | protected static function boot() |
||
97 | } |
||
98 |
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.