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