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() |
|
71 | { |
||
72 | 1 | if ($this->id > 0) { |
|
73 | $userIds = $this->users()->lists('user_id')->all(); |
||
74 | $users = User::where('deleted', '=', User::NOT_DELETED_USERS)->whereNotIn('id', $userIds)->get(); |
||
75 | } else { |
||
76 | 1 | $users = User::where('deleted', '=', User::NOT_DELETED_USERS)->get(); |
|
77 | } |
||
78 | |||
79 | 1 | return $users->lists('fullname', 'id')->all(); |
|
80 | } |
||
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 | 2 | ]) |
|
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, 'tags')); |
|
111 | |||
112 | // Sort |
||
113 | 2 | if ($sortBy == 'updated') { |
|
114 | $this->sortByUpdated($query, $sortOrder); |
||
115 | 2 | } elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) { |
|
116 | return $this->sortByTag($query, $tagGroup, $sortOrder); |
||
117 | } |
||
118 | |||
119 | 2 | return $query->get(); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Fetch issues assigned to a user. |
||
124 | * |
||
125 | * @param int $userId |
||
126 | * |
||
127 | * @return \Illuminate\Database\Eloquent\Collection |
||
128 | */ |
||
129 | 1 | public function listAssignedIssues($userId) |
|
138 | |||
139 | /** |
||
140 | * Returns projects with issues details eager loaded. |
||
141 | * |
||
142 | * @param int $status |
||
143 | * @param int $private |
||
144 | * |
||
145 | * @return Relations\HasMany |
||
146 | */ |
||
147 | 4 | public function projectsWidthIssues($status = Project::STATUS_OPEN, $private = Project::PRIVATE_NO) |
|
172 | |||
173 | /** |
||
174 | * Returns collection of tags for Kanban view. |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | public function getKanbanTags() |
||
193 | |||
194 | /** |
||
195 | * Returns collection of issues grouped by tags. |
||
196 | * |
||
197 | * @param $tagIds |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function issuesGroupByTags($tagIds) |
||
202 | { |
||
203 | $issues = $this->issues() |
||
204 | ->with('user', 'tags') |
||
205 | ->orderBy('id') |
||
206 | ->get() |
||
207 | ->groupBy(function (Project\Issue $issue) use ($tagIds) { |
||
208 | // Group by tag status |
||
209 | $tag = $issue->tags->filter(function (Tag $tag) use ($tagIds) { |
||
210 | return in_array($tag->id, $tagIds); |
||
211 | })->last(); |
||
212 | |||
213 | if (!$tag) { |
||
214 | // Workaround: Some older issues before the tags feature may not have open/close tag |
||
215 | return $issue->status === Project\Issue::STATUS_OPEN ? Tag::STATUS_OPEN : Tag::STATUS_CLOSED; |
||
216 | } |
||
217 | |||
218 | return $tag->name; |
||
219 | }); |
||
220 | |||
221 | return $issues; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Returns users assigned to the project that can fix issues (with edit permission). |
||
226 | * |
||
227 | * @return Relations\BelongsToMany |
||
228 | */ |
||
229 | 8 | public function usersCanFixIssue() |
|
233 | } |
||
234 |
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