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 |
||
10 | trait SortableTrait |
||
11 | { |
||
12 | public static function bootSortableTrait() |
||
20 | |||
21 | /** |
||
22 | * Modify the order column value. |
||
23 | */ |
||
24 | public function setHighestOrderNumber() |
||
30 | |||
31 | /** |
||
32 | * Determine the order value for the new record. |
||
33 | */ |
||
34 | public function getHighestOrderNumber(): int |
||
38 | |||
39 | /** |
||
40 | * Determine the order value of a model at a specified Nth position. |
||
41 | * |
||
42 | * @param int $position The position of the model. Positions start at 1. |
||
43 | * |
||
44 | * @return int |
||
45 | */ |
||
46 | public function getOrderNumberAtPosition(int $position): int |
||
53 | |||
54 | /** |
||
55 | * Let's be nice and provide an ordered scope. |
||
56 | * |
||
57 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
58 | * @param string $direction |
||
59 | * |
||
60 | * @return \Illuminate\Database\Query\Builder |
||
61 | */ |
||
62 | public function scopeOrdered(Builder $query, string $direction = 'asc') |
||
66 | |||
67 | /** |
||
68 | * This function reorders the records: the record with the first id in the array |
||
69 | * will get order 1, the record with the second it will get order 2, ... |
||
70 | * |
||
71 | * A starting order number can be optionally supplied (defaults to 1). |
||
72 | * |
||
73 | * @param array|\ArrayAccess $ids |
||
74 | * @param int $startOrder |
||
75 | */ |
||
76 | public static function setNewOrder($ids, int $startOrder = 1) |
||
93 | |||
94 | /* |
||
95 | * Determine the column name of the order column. |
||
96 | */ |
||
97 | protected function determineOrderColumnName(): string |
||
108 | |||
109 | /** |
||
110 | * Determine if the order column should be set when saving a new model instance. |
||
111 | */ |
||
112 | public function shouldSortWhenCreating(): bool |
||
116 | |||
117 | /** |
||
118 | * Swaps the order of this model with the model 'below' this model. |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | View Code Duplication | public function moveOrderDown() |
|
137 | |||
138 | /** |
||
139 | * Swaps the order of this model with the model 'above' this model. |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | View Code Duplication | public function moveOrderUp() |
|
158 | |||
159 | /** |
||
160 | * Swap the order of this model with the order of another model. |
||
161 | * |
||
162 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function swapOrderWithModel(Sortable $otherModel) |
||
180 | |||
181 | /** |
||
182 | * Swap the order of two models. |
||
183 | * |
||
184 | * @param \Spatie\EloquentSortable\Sortable $model |
||
185 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
186 | */ |
||
187 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
191 | |||
192 | /** |
||
193 | * Moves this model to the first position. |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function moveToStart() |
||
216 | |||
217 | /** |
||
218 | * Moves this model to the last position. |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function moveToEnd() |
||
243 | |||
244 | /** |
||
245 | * Move a model into a specified position |
||
246 | * Positions starts at 1. 0 would be the same as start. |
||
247 | * |
||
248 | * @param int $newPosition |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function moveToPosition(int $newPosition): self |
||
279 | |||
280 | /** |
||
281 | * Build eloquent builder of sortable. |
||
282 | * |
||
283 | * @return \Illuminate\Database\Eloquent\Builder |
||
284 | */ |
||
285 | public function buildSortQuery() |
||
289 | } |
||
290 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.