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|null |
||
49 | */ |
||
50 | 9 | protected function getTags($type) |
|
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() |
|
305 | |||
306 | /** |
||
307 | * Returns upload field. |
||
308 | * |
||
309 | * @return array |
||
310 | */ |
||
311 | 6 | protected function fieldUpload() |
|
319 | |||
320 | /** |
||
321 | * Returns time quote field. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | 8 | protected function fieldTimeQuote() |
|
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 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.