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 | * Let's be nice and provide an ordered scope. |
||
41 | * |
||
42 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
43 | * @param string $direction |
||
44 | * |
||
45 | * @return \Illuminate\Database\Query\Builder |
||
46 | */ |
||
47 | public function scopeOrdered(Builder $query, string $direction = 'asc') |
||
51 | |||
52 | /** |
||
53 | * Query records which are ordered above the given position. |
||
54 | * |
||
55 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
56 | * @param int $position |
||
57 | * |
||
58 | * @return \Illuminate\Database\Query\Builder |
||
59 | */ |
||
60 | public function scopeAbove(Builder $query, int $position) |
||
64 | |||
65 | /** |
||
66 | * Query records which are ordered below the given position. |
||
67 | * |
||
68 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
69 | * @param int $position |
||
70 | * |
||
71 | * @return \Illuminate\Database\Query\Builder |
||
72 | */ |
||
73 | public function scopeBelow(Builder $query, int $position) |
||
77 | |||
78 | /** |
||
79 | * This function reorders the records: the record with the first id in the array |
||
80 | * will get order 1, the record with the second it will get order 2, ... |
||
81 | * |
||
82 | * A starting order number can be optionally supplied (defaults to 1). |
||
83 | * |
||
84 | * @param array|\ArrayAccess $ids |
||
85 | * @param int $startOrder |
||
86 | */ |
||
87 | public static function setNewOrder($ids, int $startOrder = 1) |
||
104 | |||
105 | /* |
||
106 | * Determine the column name of the order column. |
||
107 | */ |
||
108 | protected function determineOrderColumnName(): string |
||
119 | |||
120 | /** |
||
121 | * Determine if the order column should be set when saving a new model instance. |
||
122 | */ |
||
123 | public function shouldSortWhenCreating(): bool |
||
127 | |||
128 | /** |
||
129 | * Swaps the order of this model with the model 'below' this model. |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | View Code Duplication | public function moveOrderDown() |
|
148 | |||
149 | /** |
||
150 | * Swaps the order of this model with the model 'above' this model. |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | View Code Duplication | public function moveOrderUp() |
|
169 | |||
170 | /** |
||
171 | * Swap the order of this model with the order of another model. |
||
172 | * |
||
173 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function swapOrderWithModel(Sortable $otherModel) |
||
191 | |||
192 | /** |
||
193 | * Swap the order of two models. |
||
194 | * |
||
195 | * @param \Spatie\EloquentSortable\Sortable $model |
||
196 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
197 | */ |
||
198 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
202 | |||
203 | /** |
||
204 | * Moves this model to the first position. |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function moveToStart() |
||
227 | |||
228 | /** |
||
229 | * Moves this model to the last position. |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function moveToEnd() |
||
254 | } |
||
255 |
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.