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 |
||
12 | trait IsSortable |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $sort = [ |
||
18 | /* |
||
19 | * The query builder instance from the Sorted scope. |
||
20 | * |
||
21 | * @var Builder |
||
22 | */ |
||
23 | 'query' => null, |
||
24 | |||
25 | /* |
||
26 | * The data applying the "sorted" scope on a model. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | 'data' => null, |
||
31 | |||
32 | /* |
||
33 | * The Neurony\Sort\Objects\Sort instance. |
||
34 | * This is used to get the sorting rules, just like a request. |
||
35 | * |
||
36 | * @var Sort |
||
37 | */ |
||
38 | 'instance' => null, |
||
39 | |||
40 | /* |
||
41 | * The field to sort by. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | 'field' => Sort::DEFAULT_SORT_FIELD, |
||
46 | |||
47 | /* |
||
48 | * The direction to sort in. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | 'direction' => Sort::DEFAULT_DIRECTION_FIELD, |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * The filter scope. |
||
57 | * Should be called on the model when building the query. |
||
58 | * |
||
59 | * @param Builder $query |
||
60 | * @param array $data |
||
61 | * @param Sort $sort |
||
62 | */ |
||
63 | public function scopeSorted($query, array $data, Sort $sort = null) |
||
88 | |||
89 | /** |
||
90 | * Verify if all sorting conditions are met. |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function isValidSort() |
||
100 | |||
101 | /** |
||
102 | * Set the sort field if an Neurony\Sort\Objects\Sort instance has been provided as a parameter for the sorted scope. |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | protected function setFieldToSortBy() |
||
112 | |||
113 | /** |
||
114 | * Set the sort direction if an Neurony\Sort\Objects\Sort instance has been provided as a parameter for the sorted scope. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | protected function setDirectionToSortIn() |
||
124 | |||
125 | /** |
||
126 | * Sort model records using columns from the model's table itself. |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | protected function sortNormally() |
||
137 | |||
138 | /** |
||
139 | * Sort model records using columns from the model relation's table. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | protected function sortByRelation() |
||
198 | |||
199 | /** |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function shouldSortByRelation() |
||
206 | |||
207 | /** |
||
208 | * Verify if the desired join exists already, possibly included by a global scope. |
||
209 | * |
||
210 | * @param string $table |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | protected function alreadyJoinedForSorting($table) |
||
218 | |||
219 | /** |
||
220 | * Verify if the direction provided matches one of the directions from: |
||
221 | * Neurony\Sort\Objects\Sort::$directions. |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | protected function checkSortingDirection() |
||
231 | |||
232 | /** |
||
233 | * Verify if the desired relation to sort by is one of: HasOne or BelongsTo. |
||
234 | * Sorting by "many" relations or "morph" ones is not possible. |
||
235 | * |
||
236 | * @param Model $model |
||
237 | * @param string $relation |
||
238 | */ |
||
239 | protected function checkRelationToSortBy(Model $model, $relation) |
||
245 | } |
||
246 |
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.