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 |
||
5 | trait ModelQuerying |
||
6 | { |
||
7 | /** |
||
8 | * Allows filtering of the query, for instance:. |
||
9 | * |
||
10 | * $query_filter = [ |
||
11 | * 'whereNotNull' => ['parent_id'], |
||
12 | * 'where' => ['name', 'John'], |
||
13 | * ] |
||
14 | * |
||
15 | * Would result in an Eloquent query with the following scope: |
||
16 | * Model::whereNotNull('parent_id')->where('name', 'John')->get(); |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $query_filter = []; |
||
21 | |||
22 | /** |
||
23 | * The number of models to return for pagination. |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $perPage = 15; |
||
28 | |||
29 | /** |
||
30 | * Order By - Column/Attribute to OrderBy. |
||
31 | * |
||
32 | * Primary Key of Model by default |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $orderBy; |
||
37 | |||
38 | /** |
||
39 | * Sort By - Either Desc or Asc. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $sortBy; |
||
44 | |||
45 | /** |
||
46 | * Finds an existing Model entry and sets it to the current model. |
||
47 | * |
||
48 | * @param int $modelitem_id |
||
49 | * |
||
50 | * @return |
||
51 | */ |
||
52 | public function find($modelitem_id) |
||
58 | |||
59 | /** |
||
60 | * Returns Model Items, either all() or paginated(). |
||
61 | * |
||
62 | * Filtered by any defined query filters ($query_filter) |
||
63 | * Ordered by Managed Model orderBy and sortBy methods |
||
64 | * |
||
65 | * @return |
||
66 | */ |
||
67 | public function items() |
||
73 | |||
74 | /** |
||
75 | * Returns All Model Items, either all() or paginated(). |
||
76 | * |
||
77 | * Filtered by any defined query filters ($query_filter) |
||
78 | * Ordered by Managed Model orderBy and sortBy methods |
||
79 | * |
||
80 | * @return |
||
81 | */ |
||
82 | View Code Duplication | public function allItems() |
|
92 | |||
93 | /** |
||
94 | * Returns Model Items, either all() or paginated(). |
||
95 | * |
||
96 | * Filtered by any defined query filters ($query_filter) |
||
97 | * Ordered by Managed Model orderBy and sortBy methods |
||
98 | * |
||
99 | * @return |
||
100 | */ |
||
101 | View Code Duplication | public function onlyTrashedItems() |
|
111 | |||
112 | /** |
||
113 | * Performs the Model Query |
||
114 | * |
||
115 | * @param \Illuminate\Database\Eloquent\Model $model |
||
116 | * |
||
117 | * @return \Illuminate\Database\Eloquent\Collection |
||
118 | */ |
||
119 | private function query($model) |
||
143 | |||
144 | /** |
||
145 | * Return Totals of All, With Trashed and Only Trashed |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public function totals() |
||
161 | |||
162 | /** |
||
163 | * Return Managed Model OrderBy. |
||
164 | * |
||
165 | * Primary key is default. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function orderBy() |
||
179 | |||
180 | /** |
||
181 | * Return Managed Model SortBy (Asc or Desc). |
||
182 | * |
||
183 | * Descending is default. |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | public function sortBy() |
||
199 | |||
200 | /** |
||
201 | * Get the number of models to return per page. |
||
202 | * |
||
203 | * @return int |
||
204 | */ |
||
205 | public function getPerPage() |
||
209 | |||
210 | /** |
||
211 | * Set the number of models to return per page. |
||
212 | * |
||
213 | * @param int $perPage |
||
214 | */ |
||
215 | public function setPerPage($perPage) |
||
219 | } |
||
220 |
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.