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:
Complex classes like Button often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Button, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Button extends Fluent implements Arrayable |
||
9 | { |
||
10 | use HasAuthorizations; |
||
11 | |||
12 | /** |
||
13 | * Make a new button instance. |
||
14 | * |
||
15 | * @param string|array $options |
||
16 | * @return static |
||
17 | */ |
||
18 | public static function make($options = []) |
||
26 | |||
27 | /** |
||
28 | * Make a raw button that does not extend anything. |
||
29 | * |
||
30 | * @param array $options |
||
31 | * @return static |
||
32 | */ |
||
33 | public static function raw($options = []) |
||
41 | |||
42 | /** |
||
43 | * Set attr option value. |
||
44 | * |
||
45 | * @param array $value |
||
46 | * @return $this |
||
47 | * @see https://datatables.net/reference/option/buttons.buttons.attr |
||
48 | */ |
||
49 | public function attr(array $value) |
||
55 | |||
56 | /** |
||
57 | * Set available option value. |
||
58 | * |
||
59 | * @param string $value |
||
60 | * @return $this |
||
61 | * @see https://datatables.net/reference/option/buttons.buttons.available |
||
62 | */ |
||
63 | public function available($value) |
||
73 | |||
74 | /** |
||
75 | * Check if a given value is a function. |
||
76 | * |
||
77 | * @param string $value |
||
78 | * @return bool |
||
79 | */ |
||
80 | protected function isFunction($value) |
||
84 | |||
85 | /** |
||
86 | * Set enabled option value. |
||
87 | * |
||
88 | * @param bool $value |
||
89 | * @return $this |
||
90 | * @see https://datatables.net/reference/option/buttons.buttons.enabled |
||
91 | */ |
||
92 | public function enabled($value = true) |
||
98 | |||
99 | /** |
||
100 | * Set init option value. |
||
101 | * |
||
102 | * @param string $value |
||
103 | * @return $this |
||
104 | * @see https://datatables.net/reference/option/buttons.buttons.init |
||
105 | */ |
||
106 | View Code Duplication | public function init($value) |
|
116 | |||
117 | /** |
||
118 | * Set key option value. |
||
119 | * |
||
120 | * @param string|array $value |
||
121 | * @return $this |
||
122 | * @see https://datatables.net/reference/option/buttons.buttons.key |
||
123 | */ |
||
124 | public function key($value) |
||
130 | |||
131 | /** |
||
132 | * Set extend option value. |
||
133 | * |
||
134 | * @param string $value |
||
135 | * @return $this |
||
136 | * @see https://datatables.net/reference/option/buttons.buttons.extend |
||
137 | */ |
||
138 | public function extend($value) |
||
144 | |||
145 | /** |
||
146 | * Set editor option value. |
||
147 | * |
||
148 | * @param string $value |
||
149 | * @return $this |
||
150 | * @see https://editor.datatables.net/reference/button |
||
151 | */ |
||
152 | public function editor($value) |
||
158 | |||
159 | /** |
||
160 | * Set buttons option value. |
||
161 | * |
||
162 | * @param array $buttons |
||
163 | * @return $this |
||
164 | * @see https://datatables.net/reference/option/buttons.buttons |
||
165 | */ |
||
166 | View Code Duplication | public function buttons(array $buttons) |
|
178 | |||
179 | /** |
||
180 | * @param array $buttons |
||
181 | * @return $this |
||
182 | * @see https://editor.datatables.net/examples/api/cancelButton |
||
183 | */ |
||
184 | View Code Duplication | public function formButtons(array $buttons) |
|
196 | |||
197 | /** |
||
198 | * @param mixed $message |
||
199 | * @return $this |
||
200 | * @see https://editor.datatables.net/examples/api/removeMessage |
||
201 | * @see https://editor.datatables.net/reference/button/create |
||
202 | * @see https://editor.datatables.net/reference/button/edit |
||
203 | * @see https://editor.datatables.net/reference/button/remove |
||
204 | */ |
||
205 | public function formMessage($message) |
||
211 | |||
212 | /** |
||
213 | * @param mixed $title |
||
214 | * @return $this |
||
215 | * @see https://editor.datatables.net/reference/button/create |
||
216 | * @see https://editor.datatables.net/reference/button/edit |
||
217 | * @see https://editor.datatables.net/reference/button/remove |
||
218 | */ |
||
219 | public function formTitle($title) |
||
225 | |||
226 | /** |
||
227 | * Set className option value. |
||
228 | * |
||
229 | * @param string $value |
||
230 | * @return $this |
||
231 | * @see https://datatables.net/reference/option/buttons.buttons.className |
||
232 | */ |
||
233 | public function className($value) |
||
239 | |||
240 | /** |
||
241 | * Set destroy option value. |
||
242 | * |
||
243 | * @param string $value |
||
244 | * @return $this |
||
245 | * @see https://datatables.net/reference/option/buttons.buttons.destroy |
||
246 | */ |
||
247 | View Code Duplication | public function destroy($value) |
|
257 | |||
258 | /** |
||
259 | * Set customize option value. |
||
260 | * |
||
261 | * @param string $value |
||
262 | * @return $this |
||
263 | * @see https://datatables.net/reference/button/excelHtml5 |
||
264 | */ |
||
265 | public function customize($value) |
||
271 | |||
272 | /** |
||
273 | * Append a class name to column. |
||
274 | * |
||
275 | * @param string $class |
||
276 | * @return $this |
||
277 | */ |
||
278 | View Code Duplication | public function addClass($class) |
|
288 | |||
289 | /** |
||
290 | * Set text option value. |
||
291 | * |
||
292 | * @param string $value |
||
293 | * @return $this |
||
294 | * @see https://datatables.net/reference/option/buttons.buttons.text |
||
295 | */ |
||
296 | public function text($value) |
||
302 | |||
303 | /** |
||
304 | * Set titleAttr option value. |
||
305 | * |
||
306 | * @param string $value |
||
307 | * @return $this |
||
308 | * @see https://datatables.net/reference/option/buttons.buttons.titleAttr |
||
309 | */ |
||
310 | public function titleAttr($value) |
||
316 | |||
317 | /** |
||
318 | * Set name option value. |
||
319 | * |
||
320 | * @param string $value |
||
321 | * @return $this |
||
322 | * @see https://datatables.net/reference/option/buttons.buttons.name |
||
323 | */ |
||
324 | public function name($value) |
||
330 | |||
331 | /** |
||
332 | * Set namespace option value. |
||
333 | * |
||
334 | * @param string $value |
||
335 | * @return $this |
||
336 | * @see https://datatables.net/reference/option/buttons.buttons.namespace |
||
337 | */ |
||
338 | public function namespace($value) |
||
344 | |||
345 | /** |
||
346 | * Set tag option value. |
||
347 | * |
||
348 | * @param string $value |
||
349 | * @return $this |
||
350 | * @see https://datatables.net/reference/option/buttons.buttons.tag |
||
351 | */ |
||
352 | public function tag($value) |
||
358 | |||
359 | /** |
||
360 | * Set columns option value. |
||
361 | * |
||
362 | * @param mixed $value |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function columns($value) |
||
371 | |||
372 | /** |
||
373 | * Set exportOptions option value. |
||
374 | * |
||
375 | * @param mixed $value |
||
376 | * @return $this |
||
377 | */ |
||
378 | public function exportOptions($value) |
||
384 | |||
385 | /** |
||
386 | * Set action to submit the form. |
||
387 | * |
||
388 | * @return \Yajra\DataTables\Html\Button |
||
389 | */ |
||
390 | public function actionSubmit() |
||
396 | |||
397 | /** |
||
398 | * Set action option value. |
||
399 | * |
||
400 | * @param string $value |
||
401 | * @return $this |
||
402 | */ |
||
403 | public function action($value) |
||
413 | |||
414 | /** |
||
415 | * Set editor class action handler. |
||
416 | * |
||
417 | * @param string $action |
||
418 | * @return \Yajra\DataTables\Html\Button |
||
419 | */ |
||
420 | public function actionHandler($action) |
||
426 | |||
427 | /** |
||
428 | * Set action to close the form. |
||
429 | * |
||
430 | * @return \Yajra\DataTables\Html\Button |
||
431 | */ |
||
432 | public function actionClose() |
||
438 | |||
439 | /** |
||
440 | * Set button alignment. |
||
441 | * |
||
442 | * @param string $align |
||
443 | * @return \Yajra\DataTables\Html\Button |
||
444 | */ |
||
445 | public function align($align = 'button-left') |
||
451 | } |
||
452 |
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.