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 |
||
| 22 | class FilterIssue 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 | * @param string $type |
||
| 40 | * |
||
| 41 | * @return \Illuminate\Database\Eloquent\Collection|null |
||
| 42 | */ |
||
| 43 | 30 | View Code Duplication | protected function getTags($type) |
|
|
|||
| 44 | { |
||
| 45 | 30 | if ($this->tags === null) { |
|
| 46 | 30 | $this->tags = (new Model\Tag())->getGroupTags(); |
|
| 47 | } |
||
| 48 | |||
| 49 | 30 | return $this->tags->where('name', $type)->first()->tags; |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $params |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | 36 | public function setup(array $params) |
|
| 58 | { |
||
| 59 | 36 | $this->project = $params['project']; |
|
| 60 | 36 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | 2 | public function actions() |
|
| 66 | { |
||
| 67 | return [ |
||
| 68 | 'filter' => [ |
||
| 69 | 'name' => 'filter', |
||
| 70 | 'label' => 'filter', |
||
| 71 | 'type' => 'info_submit', |
||
| 72 | 2 | ], |
|
| 73 | ]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | 30 | public function fields() |
|
| 80 | { |
||
| 81 | // Prefix tag groups with "tag:" |
||
| 82 | 30 | $tagGroups = (new Model\Tag())->groupsDropdown(); |
|
| 83 | |||
| 84 | // Array of sort optins |
||
| 85 | 30 | $sort = ['updated' => trans('tinyissue.updated')] + $tagGroups; |
|
| 86 | |||
| 87 | // Array of project users |
||
| 88 | 30 | $assignTo = [0 => trans('tinyissue.allusers')] + $this->project->users()->get()->lists('fullname', 'id')->all(); |
|
| 89 | |||
| 90 | $fields = [ |
||
| 91 | 'keyword' => [ |
||
| 92 | 30 | 'type' => 'text', |
|
| 93 | 30 | 'placeholder' => trans('tinyissue.keywords'), |
|
| 94 | 30 | 'onGroupAddClass' => 'toolbar-item first', |
|
| 95 | ], |
||
| 96 | 'tag_status' => [ |
||
| 97 | 30 | 'type' => 'select', |
|
| 98 | 30 | 'placeholder' => trans('tinyissue.status'), |
|
| 99 | 30 | 'onGroupAddClass' => 'toolbar-item', |
|
| 100 | 30 | 'options' => $this->getTags('status')->lists('fullname', 'id'), |
|
| 101 | ], |
||
| 102 | 'tag_type' => [ |
||
| 103 | 30 | 'type' => 'select', |
|
| 104 | 30 | 'placeholder' => trans('tinyissue.type'), |
|
| 105 | 30 | 'onGroupAddClass' => 'toolbar-item', |
|
| 106 | 30 | 'options' => $this->getTags('type')->lists('fullname', 'id'), |
|
| 107 | ], |
||
| 108 | 'sort' => [ |
||
| 109 | 30 | 'type' => 'groupField', |
|
| 110 | 30 | 'onGroupAddClass' => 'toolbar-item', |
|
| 111 | 'fields' => [ |
||
| 112 | 'sortby' => [ |
||
| 113 | 30 | 'type' => 'select', |
|
| 114 | 30 | 'placeholder' => trans('tinyissue.sortby'), |
|
| 115 | 30 | 'options' => $sort, |
|
| 116 | 30 | 'onGroupClass' => 'control-inline control-sortby', |
|
| 117 | ], |
||
| 118 | 'sortorder' => [ |
||
| 119 | 30 | 'type' => 'select', |
|
| 120 | 30 | 'options' => ['asc' => trans('tinyissue.sort_asc'), 'desc' => trans('tinyissue.sort_desc')], |
|
| 121 | 30 | 'onGroupClass' => 'control-inline control-sortorder', |
|
| 122 | ], |
||
| 123 | ], |
||
| 124 | ], |
||
| 125 | 'assignto' => [ |
||
| 126 | 30 | 'type' => 'select', |
|
| 127 | 30 | 'placeholder' => trans('tinyissue.assigned_to'), |
|
| 128 | 30 | 'options' => $assignTo, |
|
| 129 | 30 | 'onGroupAddClass' => 'toolbar-item last', |
|
| 130 | ], |
||
| 131 | ]; |
||
| 132 | |||
| 133 | 30 | return $fields; |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getRedirectUrl() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | 2 | public function openType() |
|
| 151 | } |
||
| 152 |
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.