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 | public function setHighestOrderNumber() |
||
22 | { |
||
23 | $orderColumnName = $this->determineOrderColumnName(); |
||
24 | |||
25 | $this->$orderColumnName = $this->getHighestOrderNumber() + 1; |
||
26 | } |
||
27 | |||
28 | public function getHighestOrderNumber(): int |
||
29 | { |
||
30 | return (int) $this->buildSortQuery()->max($this->determineOrderColumnName()); |
||
31 | } |
||
32 | |||
33 | public function scopeOrdered(Builder $query, string $direction = 'asc') |
||
34 | { |
||
35 | return $query->orderBy($this->determineOrderColumnName(), $direction); |
||
36 | } |
||
37 | |||
38 | public function setOrderAfter($id) |
||
39 | { |
||
40 | if (! $afterModel = $this->buildSortQuery()->find($id)) { |
||
41 | throw new InvalidArgumentException('You must pass an existing model id'); |
||
42 | } |
||
43 | |||
44 | if ($afterModel->id === $this->id) { |
||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | $orderColumnName = $this->determineOrderColumnName(); |
||
49 | |||
50 | if ($afterModel->$orderColumnName === $this->$orderColumnName) { |
||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | if ($this->$orderColumnName < $afterModel->$orderColumnName) { |
||
55 | $this->buildSortQuery() |
||
56 | ->where($orderColumnName, '>', $this->$orderColumnName) |
||
57 | ->where($orderColumnName, '<=', $afterModel->$orderColumnName) |
||
58 | ->decrement($orderColumnName); |
||
59 | |||
60 | return tap($this)->update([ |
||
61 | $orderColumnName => $afterModel->$orderColumnName |
||
62 | ]); |
||
63 | } |
||
64 | |||
65 | $this->buildSortQuery() |
||
66 | ->where($orderColumnName, '>', $afterModel->$orderColumnName) |
||
67 | ->where($orderColumnName, '<', $this->$orderColumnName) |
||
68 | ->increment($orderColumnName); |
||
69 | |||
70 | return tap($this)->update([ |
||
71 | $orderColumnName => $afterModel->$orderColumnName + 1 |
||
72 | ]); |
||
73 | } |
||
74 | |||
75 | public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null) |
||
76 | { |
||
77 | if (! is_array($ids) && ! $ids instanceof ArrayAccess) { |
||
78 | throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); |
||
79 | } |
||
80 | |||
81 | $model = new static; |
||
82 | |||
83 | $orderColumnName = $model->determineOrderColumnName(); |
||
84 | |||
85 | if (is_null($primaryKeyColumn)) { |
||
86 | $primaryKeyColumn = $model->getKeyName(); |
||
87 | } |
||
88 | |||
89 | foreach ($ids as $id) { |
||
90 | static::withoutGlobalScope(SoftDeletingScope::class) |
||
91 | ->where($primaryKeyColumn, $id) |
||
92 | ->update([$orderColumnName => $startOrder++]); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public static function setNewOrderByCustomColumn(string $primaryKeyColumn, $ids, int $startOrder = 1) |
||
97 | { |
||
98 | self::setNewOrder($ids, $startOrder, $primaryKeyColumn); |
||
99 | } |
||
100 | |||
101 | protected function determineOrderColumnName(): string |
||
112 | |||
113 | /** |
||
114 | * Determine if the order column should be set when saving a new model instance. |
||
115 | */ |
||
116 | public function shouldSortWhenCreating(): bool |
||
120 | |||
121 | View Code Duplication | public function moveOrderDown() |
|
122 | { |
||
123 | $orderColumnName = $this->determineOrderColumnName(); |
||
124 | |||
125 | $swapWithModel = $this->buildSortQuery()->limit(1) |
||
126 | ->ordered() |
||
136 | |||
137 | View Code Duplication | public function moveOrderUp() |
|
152 | |||
153 | public function swapOrderWithModel(Sortable $otherModel) |
||
167 | |||
168 | public static function swapOrder(Sortable $model, Sortable $otherModel) |
||
172 | |||
173 | public function moveToStart() |
||
192 | |||
193 | public function moveToEnd() |
||
214 | |||
215 | public function buildSortQuery() |
||
219 | } |
||
220 |
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.