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 |
||
41 | class Comment extends BaseModel implements AccessControl |
||
42 | { |
||
43 | use CrudTrait, |
||
44 | RelationTrait, |
||
45 | QueueTrait; |
||
46 | |||
47 | /** |
||
48 | * Timestamp enabled. |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | public $timestamps = true; |
||
53 | |||
54 | /** |
||
55 | * Name of database table. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $table = 'projects_issues_comments'; |
||
60 | |||
61 | /** |
||
62 | * List of allowed columns to be used in $this->fill(). |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $fillable = [ |
||
67 | 'created_by', |
||
68 | 'project_id', |
||
69 | 'issue_id', |
||
70 | 'comment', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Whether a user can view the issue. |
||
75 | * |
||
76 | * @param User $user |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 1 | public function canView(User $user) |
|
84 | |||
85 | /** |
||
86 | * Whether a user can edit the comment. |
||
87 | * |
||
88 | * @param User $user |
||
89 | * |
||
90 | * @return bool |
||
91 | */ |
||
92 | 8 | public function canEdit(User $user) |
|
96 | |||
97 | /** |
||
98 | * @param string $permission |
||
99 | * @param User $user |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | View Code Duplication | public function can($permission, User $user) |
|
116 | } |
||
117 |
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.