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 the column name of the group column. |
||
96 | */ |
||
97 | protected function determineGroupColumnName(): string |
||
98 | { |
||
108 | |||
109 | /** |
||
110 | * Determine the order value for the new record. |
||
111 | */ |
||
112 | public function getGroupColumnValue() |
||
117 | |||
118 | /** |
||
119 | * Determine if it should be sorted by group column. |
||
120 | */ |
||
121 | public function shouldSortByGroup(): bool |
||
125 | |||
126 | /** |
||
127 | * Determine if the order column should be set when saving a new model instance. |
||
128 | */ |
||
129 | public function shouldSortWhenCreating(): bool |
||
133 | |||
134 | /** |
||
135 | * Swaps the order of this model with the model 'below' this model. |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | View Code Duplication | public function moveOrderDown() |
|
154 | |||
155 | /** |
||
156 | * Swaps the order of this model with the model 'above' this model. |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | View Code Duplication | public function moveOrderUp() |
|
175 | |||
176 | /** |
||
177 | * Swap the order of this model with the order of another model. |
||
178 | * |
||
179 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function swapOrderWithModel(Sortable $otherModel) |
||
197 | |||
198 | /** |
||
199 | * Swap the order of two models. |
||
200 | * |
||
201 | * @param \Spatie\EloquentSortable\Sortable $model |
||
202 | * @param \Spatie\EloquentSortable\Sortable $otherModel |
||
203 | */ |
||
204 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
208 | |||
209 | /** |
||
210 | * Moves this model to the first position. |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function moveToStart() |
||
233 | |||
234 | /** |
||
235 | * Moves this model to the last position. |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function moveToEnd() |
||
260 | |||
261 | /** |
||
262 | * Build eloquent builder of sortable. |
||
263 | * |
||
264 | * @return \Illuminate\Database\Eloquent\Builder |
||
265 | */ |
||
266 | public function buildSortQuery() |
||
274 | } |
||
275 |
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.