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() |
||
102 | { |
||
103 | if ( |
||
104 | isset($this->sortable['sort_scope']) && |
||
105 | ! empty($this->sortable['sort_scope']) |
||
106 | ) { |
||
107 | return $this->sortable['sort_scope']; |
||
108 | } |
||
109 | |||
110 | return ''; |
||
111 | } |
||
112 | |||
113 | public function hasSortScope() |
||
114 | { |
||
115 | return ! empty($this->determineSortScope()); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Determine if the order column should be set when saving a new model instance. |
||
120 | */ |
||
121 | public function shouldSortWhenCreating(): bool |
||
122 | { |
||
123 | return $this->sortable['sort_when_creating'] ?? true; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Swaps the order of this model with the model 'below' this model. |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | View Code Duplication | public function moveOrderDown() |
|
132 | { |
||
133 | $orderColumnName = $this->determineOrderColumnName(); |
||
134 | |||
135 | $swapWithModel = static::limit(1) |
||
136 | ->ordered() |
||
137 | ->where($orderColumnName, '>', $this->$orderColumnName); |
||
138 | |||
139 | if ($this->hasSortScope()) { |
||
140 | $swapWithModel = $swapWithModel->where($this->determineSortScope(), $this->{$this->determineSortScope()}); |
||
141 | } |
||
142 | |||
143 | $swapWithModel = $swapWithModel->first(); |
||
144 | |||
145 | if (! $swapWithModel) { |
||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | return $this->swapOrderWithModel($swapWithModel); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Swaps the order of this model with the model 'above' this model. |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | View Code Duplication | public function moveOrderUp() |
|
158 | { |
||
159 | $orderColumnName = $this->determineOrderColumnName(); |
||
160 | |||
161 | $swapWithModel = static::limit(1) |
||
162 | ->ordered('desc') |
||
163 | ->where($orderColumnName, '<', $this->$orderColumnName); |
||
164 | if ($this->hasSortScope()) { |
||
165 | $swapWithModel = $swapWithModel->where($this->determineSortScope(), $this->{$this->determineSortScope()}); |
||
166 | } |
||
167 | |||
168 | $swapWithModel = $swapWithModel->first(); |
||
169 | |||
170 | if (! $swapWithModel) { |
||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | return $this->swapOrderWithModel($swapWithModel); |
||
175 | } |
||
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.