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 |
||
39 | class Attachment extends BaseModel implements AccessControl |
||
40 | { |
||
41 | use CrudTrait, |
||
42 | RelationTrait; |
||
43 | |||
44 | /** |
||
45 | * Timestamp enabled. |
||
46 | * |
||
47 | * @var bool |
||
48 | */ |
||
49 | public $timestamps = true; |
||
50 | |||
51 | /** |
||
52 | * Name of database table. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $table = 'projects_issues_attachments'; |
||
57 | |||
58 | /** |
||
59 | * List of allowed columns to be used in $this->fill(). |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $fillable = [ |
||
64 | 'uploaded_by', |
||
65 | 'filename', |
||
66 | 'fileextension', |
||
67 | 'filesize', |
||
68 | 'upload_token', |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * Whether or not the file extension is supported image type. |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 3 | public function isImage() |
|
89 | |||
90 | /** |
||
91 | * Url to attachment download. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 3 | public function download() |
|
99 | |||
100 | /** |
||
101 | * Url to display attachment. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function display() |
||
109 | |||
110 | /** |
||
111 | * Generate a URL to delete attachment. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 2 | public function toDelete() |
|
119 | |||
120 | /** |
||
121 | * Whether a user can view the issue. |
||
122 | * |
||
123 | * @param User $user |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function canView(User $user) |
||
131 | |||
132 | /** |
||
133 | * Whether a user can edit the attachment. |
||
134 | * |
||
135 | * @param User $user |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | 2 | public function canEdit(User $user) |
|
143 | |||
144 | /** |
||
145 | * @param string $permission |
||
146 | * @param User $user |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | View Code Duplication | public function can($permission, User $user) |
|
163 | } |
||
164 |
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.