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() |
||
| 13 | { |
||
| 14 | static::creating(function ($model) { |
||
|
|
|||
| 15 | if ($model instanceof Sortable && $model->shouldSortWhenCreating()) { |
||
| 16 | $model->setHighestOrderNumber(); |
||
| 17 | } |
||
| 18 | }); |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Modify the order column value. |
||
| 23 | */ |
||
| 24 | public function setHighestOrderNumber() |
||
| 25 | { |
||
| 26 | $orderColumnName = $this->determineOrderColumnName(); |
||
| 27 | |||
| 28 | $this->$orderColumnName = $this->getHighestOrderNumber() + 1; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Determine the order value for the new record. |
||
| 33 | */ |
||
| 34 | public function getHighestOrderNumber(): int |
||
| 35 | { |
||
| 36 | return (int) $this->buildSortQuery()->max($this->determineOrderColumnName()); |
||
| 37 | } |
||
| 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') |
||
| 48 | { |
||
| 49 | return $query->orderBy($this->determineOrderColumnName(), $direction); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * This function reorders the records: the record with the first id in the array |
||
| 54 | * will get order 1, the record with the second it will get order 2, ... |
||
| 55 | * |
||
| 56 | * A starting order number can be optionally supplied (defaults to 1). |
||
| 57 | * |
||
| 58 | * @param array|\ArrayAccess $ids |
||
| 59 | * @param int $startOrder |
||
| 60 | */ |
||
| 61 | public static function setNewOrder($ids, int $startOrder = 1) |
||
| 62 | { |
||
| 63 | if (! is_array($ids) && ! $ids instanceof ArrayAccess) { |
||
| 64 | throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $model = new static; |
||
| 68 | |||
| 69 | $orderColumnName = $model->determineOrderColumnName(); |
||
| 70 | $primaryKeyColumn = $model->getKeyName(); |
||
| 71 | |||
| 72 | foreach ($ids as $id) { |
||
| 73 | static::withoutGlobalScope(SoftDeletingScope::class) |
||
| 74 | ->where($primaryKeyColumn, $id) |
||
| 75 | ->update([$orderColumnName => $startOrder++]); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /* |
||
| 80 | * Determine the column name of the order column. |
||
| 81 | */ |
||
| 82 | protected function determineOrderColumnName(): string |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Determine if the order column should be set when saving a new model instance. |
||
| 96 | */ |
||
| 97 | public function shouldSortWhenCreating(): bool |
||
| 98 | { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Swaps the order of this model with the model 'below' this model. |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function moveOrderDown() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Swaps the order of this model with the model 'above' this model. |
||
| 125 | * |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function moveOrderUp() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Swap the order of this model with the order of another model. |
||
| 146 | * |
||
| 147 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
| 148 | * |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function swapOrderWithModel(Sortable $otherModel) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Swap the order of two models. |
||
| 168 | * |
||
| 169 | * @param \Spatie\EloquentSortable\Sortable $model |
||
| 170 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
| 171 | */ |
||
| 172 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Moves this model to the first position. |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function moveToStart() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Moves this model to the last position. |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function moveToEnd() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Moves this model to a specified position |
||
| 231 | * |
||
| 232 | * @param integer $position |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function moveToPosition($position) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Build eloquent builder of sortable. |
||
| 255 | * |
||
| 256 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 257 | */ |
||
| 258 | public function buildSortQuery() |
||
| 262 | } |
||
| 263 |
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.