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 |
||
39 | trait QueryTrait |
||
40 | { |
||
41 | /** |
||
42 | * Returns collection of active projects. |
||
43 | * |
||
44 | * @return Eloquent\Collection |
||
45 | */ |
||
46 | public static function activeProjects() |
||
52 | |||
53 | /** |
||
54 | * Returns collection of public projects. |
||
55 | * |
||
56 | * @return Eloquent\Collection |
||
57 | */ |
||
58 | public function publicProjects() |
||
64 | |||
65 | /** |
||
66 | * Returns all users that are not assigned in the current project. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 1 | public function usersNotIn() |
|
81 | |||
82 | /** |
||
83 | * Fetch and filter issues in the project. |
||
84 | * |
||
85 | * @param int $status |
||
86 | * @param array $filter |
||
87 | * |
||
88 | * @return \Illuminate\Database\Eloquent\Collection |
||
89 | */ |
||
90 | 2 | public function listIssues($status = Project\Issue::STATUS_OPEN, array $filter = []) |
|
91 | { |
||
92 | 2 | $sortOrder = array_get($filter, 'sort.sortorder', 'desc'); |
|
93 | 2 | $sortBy = array_get($filter, 'sort.sortby', null); |
|
94 | |||
95 | 2 | $query = $this->issues() |
|
96 | 2 | ->with('countComments', 'user', 'updatedBy', 'tags', 'tags.parent') |
|
97 | 2 | ->with([ |
|
98 | 'tags' => function (Relation $query) use ($status, $sortOrder) { |
||
99 | 2 | $status = $status == Project\Issue::STATUS_OPEN ? Tag::STATUS_OPEN : Tag::STATUS_CLOSED; |
|
|
|||
100 | 2 | $query->where('name', '!=', |
|
101 | 2 | ($status == Project\Issue::STATUS_OPEN ? Tag::STATUS_OPEN : Tag::STATUS_CLOSED)); |
|
102 | 2 | $query->orderBy('name', $sortOrder); |
|
103 | 2 | }, |
|
104 | ]) |
||
105 | 2 | ->where('status', '=', $status); |
|
106 | |||
107 | // Filter issues |
||
108 | 2 | $this->filterAssignTo($query, array_get($filter, 'assignto')); |
|
109 | 2 | $this->filterTitleOrBody($query, array_get($filter, 'keyword')); |
|
110 | 2 | $this->filterTags($query, array_get($filter, 'tag_status')); |
|
111 | 2 | $this->filterTags($query, array_get($filter, 'tag_type')); |
|
112 | |||
113 | // Sort |
||
114 | 2 | if ($sortBy == 'updated') { |
|
115 | $this->sortByUpdated($query, $sortOrder); |
||
116 | 2 | } elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) { |
|
117 | return $this->sortByTag($query, $tagGroup, $sortOrder); |
||
118 | } |
||
119 | |||
120 | 2 | return $query->get(); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * Fetch issues assigned to a user. |
||
125 | * |
||
126 | * @param int $userId |
||
127 | * |
||
128 | * @return \Illuminate\Database\Eloquent\Collection |
||
129 | */ |
||
130 | 1 | public function listAssignedIssues($userId) |
|
131 | { |
||
132 | 1 | return $this->issues() |
|
133 | 1 | ->with('countComments', 'user', 'updatedBy') |
|
134 | 1 | ->where('status', '=', Project\Issue::STATUS_OPEN) |
|
135 | 1 | ->where('assigned_to', '=', $userId) |
|
136 | 1 | ->orderBy('updated_at', 'DESC') |
|
137 | 1 | ->get(); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Returns projects with issues details eager loaded. |
||
142 | * |
||
143 | * @param int $status |
||
144 | * @param int $private |
||
145 | * |
||
146 | * @return Relations\HasMany |
||
147 | */ |
||
148 | 4 | public function projectsWidthIssues($status = Project::STATUS_OPEN, $private = Project::PRIVATE_NO) |
|
149 | { |
||
150 | $query = $this |
||
151 | 4 | ->where('status', '=', $status) |
|
152 | 4 | ->orderBy('name'); |
|
153 | |||
154 | 4 | if ($private !== Project::PRIVATE_ALL) { |
|
155 | 4 | $query->where('private', '=', $private); |
|
156 | } |
||
157 | |||
158 | 4 | $query->with([ |
|
159 | View Code Duplication | 'issues' => function (Relations\Relation $query) use ($status) { |
|
160 | 3 | $query->with('updatedBy'); |
|
161 | 3 | if ($status === Project::STATUS_OPEN) { |
|
162 | 3 | $query->where('status', '=', Project\Issue::STATUS_OPEN); |
|
163 | } |
||
164 | 4 | }, |
|
165 | 'issues.user' => function () { |
||
166 | 4 | }, |
|
167 | 'issues.countComments' => function () { |
||
168 | 4 | }, |
|
169 | ]); |
||
170 | |||
171 | 4 | return $query; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns collection of tags for Kanban view. |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function getKanbanTags() |
||
194 | |||
195 | /** |
||
196 | * Returns collection of issues grouped by tags. |
||
197 | * |
||
198 | * @param $tagIds |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | public function issuesGroupByTags($tagIds) |
||
203 | { |
||
204 | $issues = $this->issues() |
||
205 | ->with('user', 'tags') |
||
206 | ->orderBy('id') |
||
207 | ->get() |
||
208 | ->groupBy(function (Project\Issue $issue) use ($tagIds) { |
||
209 | // Group by tag status |
||
210 | $tag = $issue->tags->filter(function (Tag $tag) use ($tagIds) { |
||
211 | return in_array($tag->id, $tagIds); |
||
212 | })->last(); |
||
213 | |||
214 | if (!$tag) { |
||
215 | // Workaround: Some older issues before the tags feature may not have open/close tag |
||
216 | return $issue->status === Project\Issue::STATUS_OPEN ? Tag::STATUS_OPEN : Tag::STATUS_CLOSED; |
||
217 | } |
||
218 | |||
219 | return $tag->name; |
||
220 | }); |
||
221 | |||
222 | return $issues; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns users assigned to the project that can fix issues (with edit permission). |
||
227 | * |
||
228 | * @return Relations\BelongsToMany |
||
229 | */ |
||
230 | 8 | public function usersCanFixIssue() |
|
234 | } |
||
235 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope