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 |
||
38 | trait QueryTrait |
||
39 | { |
||
40 | /** |
||
41 | * Returns collection of active projects. |
||
42 | * |
||
43 | * @return Eloquent\Collection |
||
44 | */ |
||
45 | public static function activeProjects() |
||
51 | |||
52 | /** |
||
53 | * Returns collection of public projects. |
||
54 | * |
||
55 | * @return Eloquent\Collection |
||
56 | */ |
||
57 | public function publicProjects() |
||
63 | |||
64 | /** |
||
65 | * Returns all users that are not assigned in the current project. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | 1 | public function usersNotIn() |
|
80 | |||
81 | /** |
||
82 | * Fetch and filter issues in the project. |
||
83 | * |
||
84 | * @param int $status |
||
85 | * @param array $filter |
||
86 | * |
||
87 | * @return \Illuminate\Database\Eloquent\Collection |
||
88 | */ |
||
89 | public function listIssues($status = Project\Issue::STATUS_OPEN, array $filter = []) |
||
90 | { |
||
91 | $sortOrder = array_get($filter, 'sort.sortorder', 'desc'); |
||
92 | $sortBy = array_get($filter, 'sort.sortby', null); |
||
93 | |||
94 | $query = $this->issues() |
||
95 | ->with('countComments', 'user', 'updatedBy', 'tags', 'tags.parent') |
||
96 | ->with([ |
||
97 | 'tags' => function (Relation $query) use ($sortOrder) { |
||
98 | $query->orderBy('name', $sortOrder); |
||
99 | }, |
||
100 | ]) |
||
101 | ->where('status', '=', $status); |
||
102 | |||
103 | // Filter issues |
||
104 | $this->filterAssignTo($query, array_get($filter, 'assignto')); |
||
105 | $this->filterTitleOrBody($query, array_get($filter, 'keyword')); |
||
106 | $this->filterTags($query, array_get($filter, 'tag_status')); |
||
107 | $this->filterTags($query, array_get($filter, 'tag_type')); |
||
108 | |||
109 | // Sort |
||
110 | if ($sortBy == 'updated') { |
||
111 | $this->sortByUpdated($query, $sortOrder); |
||
112 | } elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) { |
||
113 | return $this->sortByTag($query, $tagGroup, $sortOrder); |
||
114 | } |
||
115 | |||
116 | return $query->get(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Fetch issues assigned to a user. |
||
121 | * |
||
122 | * @param int $userId |
||
123 | * |
||
124 | * @return \Illuminate\Database\Eloquent\Collection |
||
125 | */ |
||
126 | 1 | public function listAssignedIssues($userId) |
|
135 | |||
136 | /** |
||
137 | * Returns projects with issues details eager loaded. |
||
138 | * |
||
139 | * @param int $status |
||
140 | * @param int $private |
||
141 | * |
||
142 | * @return Relations\HasMany |
||
143 | */ |
||
144 | 4 | public function projectsWidthIssues($status = Project::STATUS_OPEN, $private = Project::PRIVATE_NO) |
|
169 | |||
170 | /** |
||
171 | * Returns collection of tags for Kanban view. |
||
172 | * |
||
173 | * @param User $user |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function getKanbanTagsForUser(User $user) |
||
188 | |||
189 | /** |
||
190 | * Returns collection of issues grouped by tags. |
||
191 | * |
||
192 | * @param $tagIds |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function issuesGroupByTags($tagIds) |
||
211 | |||
212 | /** |
||
213 | * Returns users assigned to the project that can fix issues (with edit permission). |
||
214 | * |
||
215 | * @return Relations\BelongsToMany |
||
216 | */ |
||
217 | 7 | public function usersCanFixIssue() |
|
221 | } |
||
222 |
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.