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 | protected function getTags($type = null) |
|
43 | { |
||
44 | 7 | if ($this->tags === null) { |
|
45 | 7 | $this->tags = (new Model\Tag())->getGroupTags(); |
|
46 | } |
||
47 | |||
48 | 7 | if ($type) { |
|
|
|||
49 | 7 | return $this->tags->where('name', $type)->first()->tags; |
|
50 | } |
||
51 | |||
52 | return $this->tags; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get issue tag for specific type/group. |
||
57 | * |
||
58 | * @param string $type |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | 7 | protected function getIssueTagId($type) |
|
63 | { |
||
64 | 7 | if (!$this->isEditing()) { |
|
65 | 2 | return 0; |
|
66 | 2 | } |
|
67 | |||
68 | 2 | $groupId = $this->getTags($type)->first()->parent_id; |
|
69 | $selectedTag = $this->getModel()->tags->where('parent_id', $groupId); |
||
70 | |||
71 | if ($selectedTag->count() === 0) { |
||
72 | return 0; |
||
73 | 7 | } |
|
74 | |||
75 | return $selectedTag->last()->id; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param array $params |
||
80 | * |
||
81 | 6 | * @return void |
|
82 | */ |
||
83 | 6 | public function setup(array $params) |
|
84 | 6 | { |
|
85 | 2 | $this->project = $params['project']; |
|
86 | if (!empty($params['issue'])) { |
||
87 | 6 | $this->editingModel($params['issue']); |
|
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | 6 | * @return array |
|
93 | */ |
||
94 | public function actions() |
||
95 | 6 | { |
|
96 | return [ |
||
97 | 'submit' => $this->isEditing() ? 'update_issue' : 'create_issue', |
||
98 | ]; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | 6 | * @return array |
|
103 | */ |
||
104 | 6 | public function fields() |
|
105 | { |
||
106 | 6 | $issueModify = \Auth::user()->permission('issue-modify'); |
|
107 | 6 | ||
108 | 6 | $fields = $this->fieldTitle(); |
|
109 | $fields += $this->fieldBody(); |
||
110 | $fields += $this->fieldTypeTags(); |
||
111 | 6 | ||
112 | 5 | // Only on creating new issue |
|
113 | if (!$this->isEditing()) { |
||
114 | $fields += $this->fieldUpload(); |
||
115 | } |
||
116 | 6 | ||
117 | 6 | // Show fields for users with issue modify permission |
|
118 | if ($issueModify) { |
||
119 | $fields += $this->issueModifyFields(); |
||
120 | 6 | } |
|
121 | |||
122 | return $fields; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Return a list of fields for users with issue modify permission. |
||
127 | * |
||
128 | 6 | * @return array |
|
129 | */ |
||
130 | 6 | protected function issueModifyFields() |
|
152 | |||
153 | /** |
||
154 | * Returns title field. |
||
155 | * |
||
156 | 7 | * @return array |
|
157 | */ |
||
158 | protected function fieldTitle() |
||
159 | { |
||
160 | return [ |
||
161 | 'title' => [ |
||
162 | 7 | 'type' => 'text', |
|
163 | 'label' => 'title', |
||
164 | ], |
||
165 | ]; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns body field. |
||
170 | * |
||
171 | 7 | * @return array |
|
172 | */ |
||
173 | protected function fieldBody() |
||
174 | { |
||
175 | return [ |
||
176 | 'body' => [ |
||
177 | 7 | 'type' => 'textarea', |
|
178 | 'label' => 'issue', |
||
179 | ], |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Returns status tag field. |
||
185 | * |
||
186 | 6 | * @return array |
|
187 | */ |
||
188 | 6 | View Code Duplication | protected function fieldStatusTags() |
189 | { |
||
190 | 6 | $tags = $this->getTags('status'); |
|
191 | $options = []; |
||
192 | foreach ($tags as $tag) { |
||
193 | 6 | $options[ucwords($tag->name)] = [ |
|
194 | 'name' => 'tag_status', |
||
195 | 'value' => $tag->id, |
||
196 | 6 | 'data-tags' => $tag->id, |
|
197 | 6 | 'color' => $tag->bgcolor, |
|
198 | 6 | ]; |
|
199 | 6 | } |
|
200 | 6 | ||
201 | 6 | $fields['tag_status'] = [ |
|
202 | 6 | 'label' => 'status', |
|
203 | 'type' => 'radioButton', |
||
204 | 'radios' => $options, |
||
205 | 'check' => $this->getIssueTagId('status'), |
||
206 | 6 | ]; |
|
207 | 6 | ||
208 | 6 | return $fields; |
|
209 | 6 | } |
|
210 | 6 | /** |
|
211 | * Returns tags field. |
||
212 | * |
||
213 | 6 | * @return array |
|
214 | */ |
||
215 | View Code Duplication | protected function fieldTypeTags() |
|
216 | { |
||
217 | $tags = $this->getTags('type'); |
||
218 | $options = []; |
||
219 | foreach ($tags as $tag) { |
||
220 | $options[ucwords($tag->name)] = [ |
||
221 | 7 | 'name' => 'tag_type', |
|
222 | 'value' => $tag->id, |
||
223 | 7 | 'data-tags' => $tag->id, |
|
224 | 'color' => $tag->bgcolor, |
||
225 | 7 | ]; |
|
226 | } |
||
227 | |||
228 | 7 | $fields['tag_type'] = [ |
|
229 | 'label' => 'type', |
||
230 | 'type' => 'radioButton', |
||
231 | 7 | 'radios' => $options, |
|
232 | 7 | 'check' => $this->getIssueTagId('type'), |
|
233 | 7 | ]; |
|
234 | 7 | ||
235 | 7 | return $fields; |
|
236 | 7 | } |
|
237 | 7 | ||
238 | /** |
||
239 | * Returns tags field. |
||
240 | * |
||
241 | 7 | * @return array |
|
242 | 7 | */ |
|
243 | 7 | protected function fieldResolutionTags() |
|
244 | 7 | { |
|
245 | 7 | $tags = $this->getTags('resolution'); |
|
246 | $options = [ |
||
247 | trans('tinyissue.none') => [ |
||
248 | 7 | 'name' => 'tag_resolution', |
|
249 | 'value' => 0, |
||
250 | 'data-tags' => 0, |
||
251 | 'color' => '#62CFFC', |
||
252 | ], |
||
253 | ]; |
||
254 | foreach ($tags as $tag) { |
||
255 | $options[ucwords($tag->name)] = [ |
||
256 | 6 | 'name' => 'tag_resolution', |
|
257 | 'value' => $tag->id, |
||
258 | 6 | 'data-tags' => $tag->id, |
|
259 | 'color' => $tag->bgcolor, |
||
260 | 6 | ]; |
|
261 | } |
||
262 | |||
263 | 6 | $fields['tag_resolution'] = [ |
|
264 | 'label' => 'resolution', |
||
265 | 'type' => 'radioButton', |
||
266 | 'radios' => $options, |
||
267 | 6 | 'check' => $this->getIssueTagId('resolution'), |
|
268 | ]; |
||
269 | |||
270 | return $fields; |
||
271 | } |
||
272 | 6 | ||
273 | /** |
||
274 | 6 | * Returns assigned to field. |
|
275 | 6 | * |
|
276 | 6 | * @return array |
|
277 | 6 | */ |
|
278 | 6 | protected function fieldAssignedTo() |
|
279 | 6 | { |
|
280 | return [ |
||
281 | 'assigned_to' => [ |
||
282 | 'type' => 'select', |
||
283 | 6 | 'label' => 'assigned_to', |
|
284 | 6 | 'options' => [0 => ''] + $this->project->usersCanFixIssue()->get()->lists('fullname', 'id')->all(), |
|
285 | 6 | 'value' => (int) $this->project->default_assignee, |
|
286 | 6 | ], |
|
287 | 6 | ]; |
|
288 | } |
||
289 | |||
290 | 6 | /** |
|
291 | * Returns upload field. |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | protected function fieldUpload() |
||
296 | { |
||
297 | $user = \Auth::guest() ? new Model\User() : \Auth::user(); |
||
298 | 6 | $fields = $this->projectUploadFields('upload', $this->project, $user); |
|
299 | $fields['upload']['label'] = 'attachments'; |
||
300 | |||
301 | return $fields; |
||
302 | 6 | } |
|
303 | 6 | ||
304 | 6 | /** |
|
305 | 6 | * Returns time quote field. |
|
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | protected function fieldTimeQuote() |
||
310 | { |
||
311 | return [ |
||
312 | 'time_quote' => [ |
||
313 | 'type' => 'groupField', |
||
314 | 'label' => 'quote', |
||
315 | 6 | 'fields' => [ |
|
316 | 'h' => [ |
||
317 | 6 | 'type' => 'number', |
|
318 | 6 | 'append' => trans('tinyissue.hours'), |
|
319 | 6 | 'value' => $this->extractQuoteValue('h'), |
|
320 | 'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4', |
||
321 | 6 | ], |
|
322 | 'm' => [ |
||
323 | 'type' => 'number', |
||
324 | 'append' => trans('tinyissue.minutes'), |
||
325 | 'value' => $this->extractQuoteValue('m'), |
||
326 | 'addGroupClass' => 'col-sm-12 col-md-12 col-lg-4', |
||
327 | ], |
||
328 | ], |
||
329 | 6 | 'addClass' => 'row issue-quote', |
|
330 | ], |
||
331 | ]; |
||
332 | } |
||
333 | 6 | ||
334 | 6 | /** |
|
335 | * @return array |
||
336 | */ |
||
337 | 6 | public function rules() |
|
338 | 6 | { |
|
339 | 6 | $rules = [ |
|
340 | 6 | 'title' => 'required|max:200', |
|
341 | 'body' => 'required', |
||
342 | ]; |
||
343 | 6 | ||
344 | 6 | return $rules; |
|
345 | 6 | } |
|
346 | 6 | ||
347 | /** |
||
348 | * @return string |
||
349 | 6 | */ |
|
350 | public function getRedirectUrl() |
||
351 | { |
||
352 | if ($this->isEditing()) { |
||
353 | return $this->getModel()->to('edit'); |
||
354 | } |
||
355 | |||
356 | return 'project/' . $this->project->id . '/issue/new'; |
||
357 | 7 | } |
|
358 | |||
359 | /** |
||
360 | 7 | * Extract number of hours, or minutes, or seconds from a quote. |
|
361 | * |
||
362 | * @param string $part |
||
363 | * |
||
364 | 7 | * @return float|int |
|
365 | */ |
||
366 | protected function extractQuoteValue($part) |
||
381 | } |
||
382 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: