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 Record with the highest order (located at the "End"). |
||
| 33 | * |
||
| 34 | * @return int |
||
| 35 | */ |
||
| 36 | public function getHighestOrderNumber(): int |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Let's be nice and provide an ordered scope. |
||
| 48 | * |
||
| 49 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 50 | * @param string $direction |
||
| 51 | * |
||
| 52 | * @return \Illuminate\Database\Query\Builder |
||
| 53 | */ |
||
| 54 | public function scopeOrdered(Builder $query, string $direction = 'asc') |
||
| 58 | |||
| 59 | /** |
||
| 60 | * This function reorders the records: the record with the first id in the array |
||
| 61 | * will get order 1, the record with the second it will get order 2, ... |
||
| 62 | * |
||
| 63 | * A starting order number can be optionally supplied (defaults to 1). |
||
| 64 | * |
||
| 65 | * @param array|\ArrayAccess $ids |
||
| 66 | * @param int $startOrder |
||
| 67 | */ |
||
| 68 | public static function setNewOrder($ids, int $startOrder = 1) |
||
| 85 | |||
| 86 | /* |
||
| 87 | * Determine the column name of the order column. |
||
| 88 | */ |
||
| 89 | protected function determineOrderColumnName(): string |
||
| 100 | |||
| 101 | public function determineSortScope() { |
||
| 111 | |||
| 112 | public function hasSortScope() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Determine if the order column should be set when saving a new model instance. |
||
| 118 | */ |
||
| 119 | public function shouldSortWhenCreating(): bool |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Swaps the order of this model with the model 'below' this model. |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function moveOrderDown() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Swaps the order of this model with the model 'above' this model. |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function moveOrderUp() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Swap the order of this model with the order of another model. |
||
| 179 | * |
||
| 180 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function swapOrderWithModel(Sortable $otherModel) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Swap the order of two models. |
||
| 201 | * |
||
| 202 | * @param \Spatie\EloquentSortable\Sortable $model |
||
| 203 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
| 204 | */ |
||
| 205 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Moves this model to the first position. |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function moveToStart() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Moves this model to the last position. |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | public function moveToEnd() |
||
| 272 | } |
||
| 273 |
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.