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 |
||
11 | class DropdownActions extends Actions |
||
12 | { |
||
13 | protected $view = 'admin::grid.actions.dropdown'; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $custom = []; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $default = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $defaultClass = [Edit::class, Show::class, Delete::class]; |
||
29 | |||
30 | /** |
||
31 | * @param RowAction $action |
||
32 | * |
||
33 | * @return $this |
||
34 | */ |
||
35 | public function add(RowAction $action) |
||
43 | |||
44 | /** |
||
45 | * Prepend default `edit` `view` `delete` actions. |
||
46 | */ |
||
47 | protected function prependDefaultActions() |
||
58 | |||
59 | /** |
||
60 | * @param RowAction $action |
||
61 | */ |
||
62 | protected function prepareAction(RowAction $action) |
||
68 | |||
69 | /** |
||
70 | * Disable view action. |
||
71 | * |
||
72 | * @param bool $disable |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | View Code Duplication | public function disableView(bool $disable = true) |
|
86 | |||
87 | /** |
||
88 | * Disable delete. |
||
89 | * |
||
90 | * @param bool $disable |
||
91 | * |
||
92 | * @return $this. |
||
93 | */ |
||
94 | View Code Duplication | public function disableDelete(bool $disable = true) |
|
104 | |||
105 | /** |
||
106 | * Disable edit. |
||
107 | * |
||
108 | * @param bool $disable |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | View Code Duplication | public function disableEdit(bool $disable = true) |
|
122 | |||
123 | /** |
||
124 | * @param null|\Closure $callback |
||
125 | * |
||
126 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
||
127 | */ |
||
128 | public function display($callback = null) |
||
151 | } |
||
152 |
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.