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 | abstract public function newQuery(); |
||
22 | |||
23 | /** |
||
24 | * Modify the order column value. |
||
25 | */ |
||
26 | public function setHighestOrderNumber() |
||
32 | |||
33 | /** |
||
34 | * Determine the order value for the new record. |
||
35 | */ |
||
36 | public function getHighestOrderNumber(): int |
||
40 | |||
41 | /** |
||
42 | * Let's be nice and provide an ordered scope. |
||
43 | * |
||
44 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
45 | * @param string $direction |
||
46 | * |
||
47 | * @return \Illuminate\Database\Query\Builder |
||
48 | */ |
||
49 | public function scopeOrdered(Builder $query, string $direction = 'asc') |
||
53 | |||
54 | /** |
||
55 | * This function reorders the records: the record with the first id in the array |
||
56 | * will get order 1, the record with the second it will get order 2, ... |
||
57 | * |
||
58 | * A starting order number can be optionally supplied (defaults to 1). |
||
59 | * |
||
60 | * @param array|\ArrayAccess $ids |
||
61 | * @param int $startOrder |
||
62 | */ |
||
63 | public static function setNewOrder($ids, int $startOrder = 1) |
||
80 | |||
81 | /* |
||
82 | * Determine the column name of the order column. |
||
83 | */ |
||
84 | protected function determineOrderColumnName(): string |
||
95 | |||
96 | /** |
||
97 | * Determine if the order column should be set when saving a new model instance. |
||
98 | */ |
||
99 | public function shouldSortWhenCreating(): bool |
||
103 | |||
104 | /** |
||
105 | * Swaps the order of this model with the model 'below' this model. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | View Code Duplication | public function moveOrderDown() |
|
124 | |||
125 | /** |
||
126 | * Swaps the order of this model with the model 'above' this model. |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | View Code Duplication | public function moveOrderUp() |
|
145 | |||
146 | /** |
||
147 | * Swap the order of this model with the order of another model. |
||
148 | * |
||
149 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function swapOrderWithModel(Sortable $otherModel) |
||
167 | |||
168 | /** |
||
169 | * Swap the order of two models. |
||
170 | * |
||
171 | * @param \Spatie\EloquentSortable\Sortable $model |
||
172 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
173 | */ |
||
174 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
178 | |||
179 | /** |
||
180 | * Moves this model to the first position. |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function moveToStart() |
||
203 | |||
204 | /** |
||
205 | * Moves this model to the last position. |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function moveToEnd() |
||
230 | |||
231 | /** |
||
232 | * @param QueryBuilder $query |
||
233 | * @param Model|SortableTrait $model |
||
234 | * |
||
235 | * @return QueryBuilder |
||
236 | */ |
||
237 | protected static function applySortableGroup($query, $model) { |
||
251 | |||
252 | /** |
||
253 | * @return string|null |
||
254 | */ |
||
255 | public static function getSortableGroupField() { |
||
259 | |||
260 | } |
||
261 |
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.