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 |
||
58 | class Issue extends BaseModel implements AccessControl |
||
59 | { |
||
60 | use CountAttributeTrait, |
||
61 | CountTrait, |
||
62 | CrudTrait, |
||
63 | CrudTagTrait, |
||
64 | RelationTrait, |
||
65 | QueryTrait, |
||
66 | QueueTrait, |
||
67 | LoggedUser; |
||
68 | |||
69 | /** |
||
70 | * Issue status: Open. |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | const STATUS_OPEN = 1; |
||
75 | |||
76 | /** |
||
77 | * Issue status: Closed. |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | const STATUS_CLOSED = 0; |
||
82 | |||
83 | /** |
||
84 | * Timestamp enabled. |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | public $timestamps = true; |
||
89 | |||
90 | /** |
||
91 | * Name of database table. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $table = 'projects_issues'; |
||
96 | |||
97 | /** |
||
98 | * List of allowed columns to be used in $this->fill(). |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $fillable = ['created_by', 'project_id', 'title', 'body', 'assigned_to', 'time_quote', 'lock_quote']; |
||
103 | |||
104 | /** |
||
105 | * Set attributes default value. |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $attributes = [ |
||
110 | 'status' => self::STATUS_OPEN, |
||
111 | ]; |
||
112 | |||
113 | /** |
||
114 | * Returns the aggregate value of number of comments in an issue. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | 6 | public function getCountCommentsAttribute() |
|
122 | |||
123 | /** |
||
124 | * Generate a URL for the active project. |
||
125 | * |
||
126 | * @param string $url |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | 30 | public function to($url = '') |
|
134 | |||
135 | /** |
||
136 | * Convert time quote from an array into seconds. |
||
137 | * |
||
138 | * @param array $value |
||
139 | */ |
||
140 | 36 | public function setTimeQuoteAttribute($value) |
|
150 | |||
151 | /** |
||
152 | * Returns the color of tag status. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getTypeColorAttribute() |
||
168 | |||
169 | /** |
||
170 | * Whether or not the issue is new. |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isNew() |
||
182 | |||
183 | /** |
||
184 | * Whether or not the issue is open or closed. |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | 27 | public function isOpen() |
|
192 | |||
193 | /** |
||
194 | * Check if the issue contains a tag with option to set the issue as readonly to current user. |
||
195 | * |
||
196 | * @param Model\User $user |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | 14 | public function hasReadOnlyTag(Model\User $user) |
|
206 | |||
207 | /** |
||
208 | * Whether or not the issue quote is locked by manager. |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | 11 | public function isQuoteLocked() |
|
216 | |||
217 | /** |
||
218 | * Check if a user is allowed to see the issue quote. |
||
219 | * |
||
220 | * @param Model\User $user |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 23 | public function canUserViewQuote(Model\User $user = null) |
|
234 | |||
235 | /** |
||
236 | * Whether or not a user is the creator of the issue. |
||
237 | * |
||
238 | * @param Model\User $user |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | 17 | public function isCreatedBy(Model\User $user) |
|
246 | |||
247 | /** |
||
248 | * Whether a user can view the issue. |
||
249 | * |
||
250 | * @param Model\User $user |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | 9 | public function canView(Model\User $user) |
|
266 | |||
267 | /** |
||
268 | * Whether a user can edit the issue. |
||
269 | * |
||
270 | * @param Model\User $user |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | 17 | public function canEdit(Model\User $user) |
|
279 | |||
280 | /** |
||
281 | * @param string $permission |
||
282 | * @param Model\User $user |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 9 | View Code Duplication | public function can($permission, Model\User $user) |
300 | } |
||
301 |
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.