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 |
||
21 | class Issue extends FormAbstract |
||
22 | { |
||
23 | /** |
||
24 | * An instance of project model. |
||
25 | * |
||
26 | * @var Model\Project |
||
27 | */ |
||
28 | protected $project; |
||
29 | |||
30 | /** |
||
31 | * Collection of all tags. |
||
32 | * |
||
33 | * @var \Illuminate\Database\Eloquent\Collection |
||
34 | */ |
||
35 | protected $tags = null; |
||
36 | |||
37 | /** |
||
38 | * @param string $type |
||
39 | * |
||
40 | * @return \Illuminate\Database\Eloquent\Collection|null |
||
41 | */ |
||
42 | 7 | View Code Duplication | protected function getTags($type = null) |
54 | |||
55 | /** |
||
56 | * Get issue tag for specific type/group. |
||
57 | * |
||
58 | * @param string $type |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | 7 | protected function getIssueTag($type) |
|
63 | { |
||
64 | 7 | if ($this->isEditing()) { |
|
65 | 2 | $groupId = $this->getTags($type)->first()->parent_id; |
|
66 | 2 | $selectedTag = $this->getModel()->tags->where('parent_id', $groupId); |
|
67 | |||
68 | 2 | if ($selectedTag->count() > 0) { |
|
69 | return $selectedTag->last(); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | 7 | return new Model\Tag(); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param array $params |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | 6 | public function setup(array $params) |
|
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | 6 | public function actions() |
|
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | */ |
||
102 | 6 | public function fields() |
|
103 | { |
||
104 | 6 | $issueModify = \Auth::user()->permission('issue-modify'); |
|
105 | |||
106 | 6 | $fields = $this->fieldTitle(); |
|
107 | 6 | $fields += $this->fieldBody(); |
|
108 | 6 | $fields += $this->fieldTypeTags(); |
|
109 | |||
110 | // Only on creating new issue |
||
111 | 6 | if (!$this->isEditing()) { |
|
112 | 5 | $fields += $this->fieldUpload(); |
|
113 | } |
||
114 | |||
115 | // Show fields for users with issue modify permission |
||
116 | 6 | if ($issueModify) { |
|
117 | 6 | $fields += $this->issueModifyFields(); |
|
118 | } |
||
119 | |||
120 | 6 | return $fields; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Return a list of fields for users with issue modify permission. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 6 | protected function issueModifyFields() |
|
150 | |||
151 | /** |
||
152 | * Returns title field. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 7 | protected function fieldTitle() |
|
165 | |||
166 | /** |
||
167 | * Returns body field. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 7 | protected function fieldBody() |
|
180 | |||
181 | /** |
||
182 | * Returns status tag field. |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | 6 | View Code Duplication | protected function fieldStatusTags() |
215 | |||
216 | /** |
||
217 | * Returns tags field. |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | 7 | View Code Duplication | protected function fieldTypeTags() |
250 | |||
251 | /** |
||
252 | * Returns tags field. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 6 | protected function fieldResolutionTags() |
|
292 | |||
293 | /** |
||
294 | * Returns assigned to field. |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | 6 | protected function fieldAssignedTo() |
|
309 | |||
310 | /** |
||
311 | * Returns upload field. |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | 6 | protected function fieldUpload() |
|
323 | |||
324 | /** |
||
325 | * Returns time quote field. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 6 | protected function fieldTimeQuote() |
|
353 | |||
354 | /** |
||
355 | * @return array |
||
356 | */ |
||
357 | 7 | public function rules() |
|
366 | |||
367 | /** |
||
368 | * @return string |
||
369 | */ |
||
370 | public function getRedirectUrl() |
||
378 | |||
379 | /** |
||
380 | * Extract number of hours, or minutes, or seconds from a quote. |
||
381 | * |
||
382 | * @param string $part |
||
383 | * |
||
384 | * @return float|int |
||
385 | */ |
||
386 | 6 | protected function extractQuoteValue($part) |
|
401 | } |
||
402 |
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.