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:
Complex classes like Issue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Issue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Issue extends FormAbstract |
||
23 | { |
||
24 | /** |
||
25 | * An instance of project model. |
||
26 | * |
||
27 | * @var Model\Project |
||
28 | */ |
||
29 | protected $project; |
||
30 | |||
31 | /** |
||
32 | * Collection of all tags. |
||
33 | * |
||
34 | * @var \Illuminate\Database\Eloquent\Collection |
||
35 | */ |
||
36 | protected $tags = null; |
||
37 | |||
38 | /** |
||
39 | * Is issue readonly. |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $readOnly = false; |
||
44 | |||
45 | /** |
||
46 | * @param string $type |
||
47 | * |
||
48 | * @return \Illuminate\Database\Eloquent\Collection |
||
49 | */ |
||
50 | 9 | View Code Duplication | protected function getTags($type) |
|
|||
51 | { |
||
52 | 9 | if ($this->tags === null) { |
|
53 | 9 | $this->tags = (new Model\Tag())->getGroupTags(); |
|
54 | } |
||
55 | |||
56 | 9 | return $this->tags->where('name', $type)->first()->tags; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns an array of tags to be used as the selectable options. |
||
61 | * |
||
62 | * @param string $type |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | 9 | protected function getSelectableTags($type = null) |
|
67 | { |
||
68 | 9 | $currentTag = $this->getIssueTag($type); |
|
69 | |||
70 | 9 | if ($currentTag->id && (!$currentTag->canView() || $this->readOnly)) { |
|
71 | 1 | $tags = [$currentTag]; |
|
72 | 9 | } elseif ($this->readOnly) { |
|
73 | 1 | $tags = []; |
|
74 | } else { |
||
75 | 9 | $tags = $this->getTags($type); |
|
76 | } |
||
77 | |||
78 | 9 | return $this->generateTagRadioButtonOptions($tags, $type); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get issue tag for specific type/group. |
||
83 | * |
||
84 | * @param string $type |
||
85 | * |
||
86 | * @return Model\Tag |
||
87 | */ |
||
88 | 9 | protected function getIssueTag($type) |
|
89 | { |
||
90 | 9 | if ($this->isEditing()) { |
|
91 | 4 | $groupId = $this->getTags($type)->first()->parent_id; |
|
92 | 4 | $selectedTag = $this->getModel()->tags->where('parent_id', $groupId); |
|
93 | |||
94 | 4 | if ($selectedTag->count() > 0) { |
|
95 | 1 | return $selectedTag->last(); |
|
96 | } |
||
97 | } |
||
98 | |||
99 | 9 | return new Model\Tag(); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param array $params |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 8 | public function setup(array $params) |
|
108 | { |
||
109 | 8 | $this->project = $params['project']; |
|
110 | 8 | if (!empty($params['issue'])) { |
|
111 | 4 | $this->editingModel($params['issue']); |
|
112 | 4 | $this->readOnly = $this->getModel()->hasReadOnlyTag($this->getLoggedUser()); |
|
113 | } |
||
114 | 8 | } |
|
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | 6 | public function actions() |
|
120 | { |
||
121 | 6 | $actions = []; |
|
122 | |||
123 | // Check if issue is in readonly tag |
||
124 | 6 | if (!$this->readOnly) { |
|
125 | $actions = [ |
||
126 | 6 | 'submit' => $this->isEditing() ? 'update_issue' : 'create_issue', |
|
127 | ]; |
||
128 | |||
129 | 6 | if ($this->isEditing() && $this->getLoggedUser()->permission(Model\Permission::PERM_ISSUE_MODIFY)) { |
|
130 | 2 | $actions['delete'] = [ |
|
131 | 2 | 'type' => 'danger_submit', |
|
132 | 2 | 'label' => trans('tinyissue.delete_something', ['name' => '#' . $this->getModel()->id]), |
|
133 | 2 | 'class' => 'close-issue', |
|
134 | 2 | 'name' => 'delete-issue', |
|
135 | 2 | 'data-message' => trans('tinyissue.delete_issue_confirm'), |
|
136 | ]; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | 6 | return $actions; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | 8 | public function fields() |
|
147 | { |
||
148 | 8 | $issueModify = $this->getLoggedUser()->permission('issue-modify'); |
|
149 | |||
150 | 8 | $fields = []; |
|
151 | 8 | $fields += $this->readOnlyMessage(); |
|
152 | 8 | $fields += $this->fieldTitle(); |
|
153 | 8 | $fields += $this->fieldBody(); |
|
154 | 8 | $fields += $this->fieldTag('type'); |
|
155 | |||
156 | // Only on creating new issue |
||
157 | 8 | if (!$this->isEditing()) { |
|
158 | 5 | $fields += $this->fieldUpload(); |
|
159 | } |
||
160 | |||
161 | // Show fields for users with issue modify permission |
||
162 | 8 | if ($issueModify) { |
|
163 | 8 | $fields += $this->issueModifyFields(); |
|
164 | } |
||
165 | |||
166 | 8 | return $fields; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return a list of fields for users with issue modify permission. |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 8 | protected function issueModifyFields() |
|
175 | { |
||
176 | 8 | $fields = []; |
|
177 | |||
178 | 8 | $fields['internal_status'] = [ |
|
179 | 'type' => 'legend', |
||
180 | ]; |
||
181 | |||
182 | // Status tags |
||
183 | 8 | $fields += $this->fieldTag('status'); |
|
184 | |||
185 | // Assign users |
||
186 | 8 | $fields += $this->fieldAssignedTo(); |
|
187 | |||
188 | // Quotes |
||
189 | 8 | $fields += $this->fieldTimeQuote(); |
|
190 | |||
191 | // Resolution tags |
||
192 | 8 | $fields += $this->fieldResolutionTags(); |
|
193 | |||
194 | 8 | return $fields; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Returns message about read only issue. |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 8 | protected function readOnlyMessage() |
|
203 | { |
||
204 | 8 | $field = []; |
|
205 | |||
206 | 8 | if ($this->readOnly) { |
|
207 | $field = [ |
||
208 | 'readonly' => [ |
||
209 | 1 | 'type' => 'plaintext', |
|
210 | 1 | 'label' => ' ', |
|
211 | 1 | 'value' => '<div class="alert alert-warning">' . trans('tinyissue.readonly_issue_message') . '</div>', |
|
212 | ], |
||
213 | ]; |
||
214 | } |
||
215 | |||
216 | 8 | return $field; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns title field. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 9 | protected function fieldTitle() |
|
233 | |||
234 | /** |
||
235 | * Returns body field. |
||
236 | * |
||
237 | * @return array |
||
238 | */ |
||
239 | 9 | protected function fieldBody() |
|
240 | { |
||
248 | |||
249 | /** |
||
250 | * Returns tag field. |
||
251 | * |
||
252 | * @param string $type |
||
253 | * @param array $prepend |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 9 | protected function fieldTag($type, array $prepend = []) |
|
271 | |||
272 | /** |
||
273 | * Returns tags field. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | 8 | protected function fieldResolutionTags() |
|
288 | |||
289 | /** |
||
290 | * Returns assigned to field. |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | 8 | protected function fieldAssignedTo() |
|
295 | { |
||
296 | return [ |
||
297 | 'assigned_to' => [ |
||
298 | 8 | 'type' => 'select', |
|
299 | 8 | 'label' => 'assigned_to', |
|
300 | 8 | 'options' => [0 => ''] + $this->project->usersCanFixIssue()->get()->lists('fullname', 'id')->all(), |
|
301 | 8 | 'value' => (int) $this->project->default_assignee, |
|
302 | ], |
||
303 | ]; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Returns upload field. |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | 6 | protected function fieldUpload() |
|
312 | { |
||
313 | 6 | $user = $this->getLoggedUser(); |
|
314 | 6 | $fields = $this->projectUploadFields('upload', $this->project, $user); |
|
315 | 6 | $fields['upload']['label'] = 'attachments'; |
|
316 | |||
317 | 6 | return $fields; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Returns time quote field. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | 8 | protected function fieldTimeQuote() |
|
326 | { |
||
327 | $fields = [ |
||
328 | 'time_quote' => [ |
||
329 | 8 | 'type' => 'groupField', |
|
330 | 8 | 'label' => 'quote', |
|
331 | 'fields' => [ |
||
332 | 'h' => [ |
||
333 | 8 | 'type' => 'number', |
|
334 | 8 | 'append' => trans('tinyissue.hours'), |
|
335 | 8 | 'value' => $this->extractQuoteValue('h'), |
|
336 | 8 | 'addGroupClass' => 'col-sm-5 col-md-5 col-lg-4', |
|
337 | ], |
||
338 | 'm' => [ |
||
339 | 8 | 'type' => 'number', |
|
340 | 8 | 'append' => trans('tinyissue.minutes'), |
|
341 | 8 | 'value' => $this->extractQuoteValue('m'), |
|
342 | 8 | 'addGroupClass' => 'col-sm-5 col-md-5 col-lg-4', |
|
343 | ], |
||
344 | 'lock' => [ |
||
345 | 8 | 'type' => 'checkboxButton', |
|
346 | 8 | 'label' => '', |
|
347 | 'noLabel' => true, |
||
348 | 8 | 'class' => 'eee', |
|
349 | 8 | 'addGroupClass' => 'sss col-sm-12 col-md-12 col-lg-4', |
|
350 | 'checkboxes' => [ |
||
351 | 'Lock Quote' => [ |
||
352 | 8 | 'value' => 1, |
|
353 | 8 | 'data-tags' => 1, |
|
354 | 8 | 'color' => 'red', |
|
355 | 8 | 'checked' => $this->isEditing() && $this->getModel()->isQuoteLocked(), |
|
356 | ], |
||
357 | ], |
||
358 | 'grouped' => true, |
||
359 | ], |
||
360 | ], |
||
361 | 8 | 'addClass' => 'row issue-quote', |
|
362 | 8 | ], |
|
363 | ]; |
||
364 | |||
365 | // If user does not have access to lock quote, then remove the field |
||
366 | 8 | if (!$this->getLoggedUser()->permission(Model\Permission::PERM_ISSUE_LOCK_QUOTE)) { |
|
367 | 3 | unset($fields['time_quote']['fields']['lock']); |
|
368 | |||
369 | // If quote is locked then remove quote fields |
||
370 | 3 | if ($this->isEditing() && $this->getModel()->isQuoteLocked()) { |
|
371 | 1 | return []; |
|
372 | } |
||
373 | } |
||
374 | |||
375 | 8 | return $fields; |
|
376 | } |
||
377 | |||
378 | /** |
||
379 | * @return array |
||
380 | */ |
||
381 | 7 | public function rules() |
|
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | public function getRedirectUrl() |
||
402 | |||
403 | /** |
||
404 | * Extract number of hours, or minutes, or seconds from a quote. |
||
405 | * |
||
406 | * @param string $part |
||
407 | * |
||
408 | * @return float|int |
||
409 | */ |
||
410 | 8 | protected function extractQuoteValue($part) |
|
425 | |||
426 | /** |
||
427 | * Returns an array structured for radio button element. |
||
428 | * |
||
429 | * @param Collection|array $tags |
||
430 | * @param string $type |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | 9 | protected function generateTagRadioButtonOptions($tags, $type) |
|
451 | } |
||
452 |
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.