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 |
||
18 | trait CustomOrderTrait |
||
19 | { |
||
20 | /** |
||
21 | * Check $order_fields and $order_defaults are set |
||
22 | * |
||
23 | * @param string $orderField |
||
24 | * @param string $direction |
||
25 | * @return bool |
||
26 | */ |
||
27 | protected function hasOrderFieldsAndDefaults($orderField, $direction) |
||
31 | |||
32 | /** |
||
33 | * Check $this->order_fields set correctly |
||
34 | * |
||
35 | * @return bool |
||
36 | */ |
||
37 | View Code Duplication | protected function hasOrderFields() |
|
|
|||
38 | { |
||
39 | if (!isset($this->order_fields) || !is_array($this->order_fields)) { |
||
40 | throw new InvalidArgumentException(get_class($this) . ' order fields not set correctly.'); |
||
41 | } else { |
||
42 | return true; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Check order defaults set correctly |
||
48 | * |
||
49 | * @param string $orderField |
||
50 | * @param string $direction |
||
51 | * @return bool |
||
52 | */ |
||
53 | protected function hasOrderDefaults($orderField, $direction) |
||
62 | |||
63 | /** |
||
64 | * Check $this->search_fields set correctly |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | View Code Duplication | protected function hasSearchFields() |
|
76 | |||
77 | /** |
||
78 | * Override column if provided column not valid |
||
79 | * |
||
80 | * @param string $column |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function setOrderColumn($column) |
||
92 | |||
93 | /** |
||
94 | * Override direction if provided direction not valid |
||
95 | * |
||
96 | * @param string $direction |
||
97 | * @return string |
||
98 | */ |
||
99 | protected function setOrderDirection($direction) |
||
108 | |||
109 | /** |
||
110 | * Set order based on order_fields |
||
111 | * |
||
112 | * @param Builder $query |
||
113 | * @param string $column |
||
114 | * @param string $direction |
||
115 | * @return Builder |
||
116 | */ |
||
117 | protected function setOrder($query, $column, $direction) |
||
129 | |||
130 | /** |
||
131 | * Join a related table if not already joined |
||
132 | * |
||
133 | * @param Builder $query |
||
134 | * @param string $table |
||
135 | * @return Builder |
||
136 | */ |
||
137 | protected function joinRelatedTable($query, $table) |
||
154 | |||
155 | /** |
||
156 | * Check if this model has already been joined to a table or relation |
||
157 | * |
||
158 | * @param Builder $builder |
||
159 | * @param string $table |
||
160 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
161 | * @return bool |
||
162 | */ |
||
163 | protected function hasJoin(Builder $builder, $table, $relation) |
||
171 | |||
172 | /** |
||
173 | * Check if model is currently joined to $table |
||
174 | * |
||
175 | * @param Builder $builder |
||
176 | * @param string $table |
||
177 | * @return bool |
||
178 | */ |
||
179 | protected function isJoinedToTable(Builder $builder, $table) |
||
192 | |||
193 | /** |
||
194 | * Check if relation exists in eager loads |
||
195 | * |
||
196 | * @param Builder $builder |
||
197 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
198 | * @return bool |
||
199 | */ |
||
200 | protected function isEagerLoaded(Builder $builder, $relation) |
||
206 | } |
||
207 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.