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 |
||
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) |
|
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() |
|
110 | |||
111 | /** |
||
112 | * @return array |
||
113 | */ |
||
114 | 6 | public function fields() |
|
115 | { |
||
116 | 6 | $issueModify = \Auth::user()->permission('issue-modify'); |
|
117 | |||
118 | 6 | $fields = $this->fieldTitle(); |
|
119 | 6 | $fields += $this->fieldBody(); |
|
120 | 6 | $fields += $this->fieldTypeTags(); |
|
121 | |||
122 | // Only on creating new issue |
||
123 | 6 | if (!$this->isEditing()) { |
|
124 | 5 | $fields += $this->fieldUpload(); |
|
125 | } |
||
126 | |||
127 | // Show fields for users with issue modify permission |
||
128 | 6 | if ($issueModify) { |
|
129 | 6 | $fields += $this->issueModifyFields(); |
|
130 | } |
||
131 | |||
132 | 6 | return $fields; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Return a list of fields for users with issue modify permission. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 6 | protected function issueModifyFields() |
|
162 | |||
163 | /** |
||
164 | * Returns title field. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 7 | protected function fieldTitle() |
|
177 | |||
178 | /** |
||
179 | * Returns body field. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | 7 | protected function fieldBody() |
|
192 | |||
193 | /** |
||
194 | * Returns status tag field. |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | 6 | protected function fieldStatusTags() |
|
227 | |||
228 | /** |
||
229 | * Returns tags field. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 7 | protected function fieldTypeTags() |
|
262 | |||
263 | /** |
||
264 | * Returns tags field. |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 6 | protected function fieldResolutionTags() |
|
304 | |||
305 | /** |
||
306 | * Returns assigned to field. |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | 6 | protected function fieldAssignedTo() |
|
321 | |||
322 | /** |
||
323 | * Returns upload field. |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | 6 | protected function fieldUpload() |
|
335 | |||
336 | /** |
||
337 | * Returns time quote field. |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | 6 | protected function fieldTimeQuote() |
|
394 | |||
395 | /** |
||
396 | * @return array |
||
397 | */ |
||
398 | 7 | public function rules() |
|
407 | |||
408 | /** |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getRedirectUrl() |
||
419 | |||
420 | /** |
||
421 | * Extract number of hours, or minutes, or seconds from a quote. |
||
422 | * |
||
423 | * @param string $part |
||
424 | * |
||
425 | * @return float|int |
||
426 | */ |
||
427 | 6 | protected function extractQuoteValue($part) |
|
442 | } |
||
443 |
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.