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() |
||
59 | { |
||
60 | return $this->where('private', '=', Project::PRIVATE_NO) |
||
61 | ->orderBy('name', 'ASC') |
||
62 | ->get(); |
||
63 | } |
||
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 = []) |
|
120 | |||
121 | /** |
||
122 | * Fetch issues assigned to a user. |
||
123 | * |
||
124 | * @param User $user |
||
125 | * |
||
126 | * @return \Illuminate\Database\Eloquent\Collection |
||
127 | */ |
||
128 | 1 | public function listAssignedOrCreatedIssues(User $user) |
|
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 | * @param User $user |
||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | 1 | public function getKanbanTagsForUser(User $user) |
|
182 | { |
||
183 | 1 | $tags = $this->kanbanTags() |
|
184 | ->where(function (Eloquent\Builder $query) use ($user) { |
||
185 | 1 | $query->where('role_limit', '<=', $user->role_id); |
|
186 | 1 | $query->orWhere('role_limit', '=', null); |
|
187 | 1 | }) |
|
188 | 1 | ->get(); |
|
189 | |||
190 | 1 | return $tags; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns collection of issues grouped by tags. |
||
195 | * |
||
196 | * @param $tagIds |
||
197 | * |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function issuesGroupByTags($tagIds) |
||
215 | |||
216 | /** |
||
217 | * Returns users assigned to the project that can fix issues (with edit permission). |
||
218 | * |
||
219 | * @return Relations\BelongsToMany |
||
220 | */ |
||
221 | 18 | public function usersCanFixIssue() |
|
225 | } |
||
226 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.