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 |
||
13 | class Field extends Fluent |
||
14 | { |
||
15 | use HasAuthorizations; |
||
16 | |||
17 | /** |
||
18 | * Field type. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $type = 'text'; |
||
23 | |||
24 | /** |
||
25 | * Password constructor. |
||
26 | * |
||
27 | * @param array $attributes |
||
28 | */ |
||
29 | public function __construct($attributes = []) |
||
35 | |||
36 | /** |
||
37 | * Make a new instance of a field. |
||
38 | * |
||
39 | * @param string $name |
||
40 | * @param string $label |
||
41 | * @return static|\Yajra\DataTables\Html\Editor\Fields\Field |
||
42 | */ |
||
43 | public static function make($name, $label = '') |
||
56 | |||
57 | /** |
||
58 | * @param string $label |
||
59 | * @return $this |
||
60 | * @see https://editor.datatables.net/reference/option/fields.label |
||
61 | */ |
||
62 | public function label($label) |
||
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | * @return $this |
||
72 | * @see https://editor.datatables.net/reference/option/fields.name |
||
73 | */ |
||
74 | public function name($name) |
||
80 | |||
81 | /** |
||
82 | * @param string $data |
||
83 | * @return $this |
||
84 | * @see https://editor.datatables.net/reference/option/fields.data |
||
85 | */ |
||
86 | public function data($data) |
||
92 | |||
93 | /** |
||
94 | * @param string $type |
||
95 | * @return $this |
||
96 | * @see https://editor.datatables.net/reference/option/fields.type |
||
97 | */ |
||
98 | public function type($type) |
||
104 | |||
105 | /** |
||
106 | * Get options from a model. |
||
107 | * |
||
108 | * @param mixed $model |
||
109 | * @param string $value |
||
110 | * @param string $key |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function modelOptions($model, $value, $key = 'id') |
||
119 | |||
120 | /** |
||
121 | * Set select options. |
||
122 | * |
||
123 | * @param array|mixed $options |
||
124 | * @return $this |
||
125 | */ |
||
126 | View Code Duplication | public function options($options) |
|
|
|||
127 | { |
||
128 | if ($options instanceof Arrayable) { |
||
129 | $options = $options->toArray(); |
||
130 | } |
||
131 | |||
132 | $this->attributes['options'] = $options; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get options from a table. |
||
139 | * |
||
140 | * @param mixed $table |
||
141 | * @param string $value |
||
142 | * @param string $key |
||
143 | * @param \Closure $whereCallback |
||
144 | * @param string|null $key |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function tableOptions($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null) |
||
153 | |||
154 | /** |
||
155 | * Set checkbox separator. |
||
156 | * |
||
157 | * @param string $separator |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function separator($separator = ',') |
||
166 | |||
167 | /** |
||
168 | * Set dateTime format. |
||
169 | * |
||
170 | * @param string $format |
||
171 | * @return $this |
||
172 | * @see https://editor.datatables.net/reference/field/datetime |
||
173 | */ |
||
174 | public function format($format) |
||
180 | |||
181 | /** |
||
182 | * Set field default value. |
||
183 | * |
||
184 | * @param mixed $value |
||
185 | * @return $this |
||
186 | * @see https://editor.datatables.net/reference/option/fields.def |
||
187 | */ |
||
188 | public function default($value) |
||
194 | |||
195 | /** |
||
196 | * Set field message value. |
||
197 | * |
||
198 | * @param string $value |
||
199 | * @return $this |
||
200 | * @see https://editor.datatables.net/reference/option/fields.message |
||
201 | */ |
||
202 | public function message($value) |
||
208 | |||
209 | /** |
||
210 | * Set field fieldInfo value. |
||
211 | * |
||
212 | * @param string $value |
||
213 | * @return $this |
||
214 | * @see https://editor.datatables.net/reference/option/fields.fieldInfo |
||
215 | */ |
||
216 | public function fieldInfo($value) |
||
222 | |||
223 | /** |
||
224 | * Set field labelInfo value. |
||
225 | * |
||
226 | * @param string $value |
||
227 | * @return $this |
||
228 | * @see https://editor.datatables.net/reference/option/fields.labelInfo |
||
229 | */ |
||
230 | public function labelInfo($value) |
||
236 | |||
237 | /** |
||
238 | * Set field entityDecode value. |
||
239 | * |
||
240 | * @param mixed|bool $value |
||
241 | * @return $this |
||
242 | * @see https://editor.datatables.net/reference/option/fields.entityDecode |
||
243 | */ |
||
244 | public function entityDecode($value) |
||
250 | |||
251 | /** |
||
252 | * Set field multiEditable value. |
||
253 | * |
||
254 | * @param mixed|bool $value |
||
255 | * @return $this |
||
256 | * @see https://editor.datatables.net/reference/option/fields.multiEditable |
||
257 | */ |
||
258 | public function multiEditable($value) |
||
264 | |||
265 | /** |
||
266 | * Set field id value. |
||
267 | * |
||
268 | * @param string $value |
||
269 | * @return $this |
||
270 | * @see https://editor.datatables.net/reference/option/fields.id |
||
271 | */ |
||
272 | public function id($value) |
||
278 | |||
279 | /** |
||
280 | * Set field submit value. |
||
281 | * |
||
282 | * @param bool $value |
||
283 | * @return $this |
||
284 | * @see https://editor.datatables.net/reference/option/fields.submit |
||
285 | */ |
||
286 | public function submit($value) |
||
292 | |||
293 | /** |
||
294 | * Set field compare value. |
||
295 | * |
||
296 | * @param bool $value |
||
297 | * @return $this |
||
298 | * @see https://editor.datatables.net/reference/option/fields.compare |
||
299 | */ |
||
300 | public function compare($value) |
||
306 | |||
307 | /** |
||
308 | * Set field opts value. |
||
309 | * |
||
310 | * @param bool $value |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function opts(array $value) |
||
319 | |||
320 | /** |
||
321 | * Set field attr option. |
||
322 | * |
||
323 | * @param string $attribute |
||
324 | * @param string $value |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function attr($attribute, $value) |
||
333 | } |
||
334 |
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.