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 |
||
9 | class Button extends Fluent implements Arrayable |
||
10 | { |
||
11 | /** |
||
12 | * Flag to check if user is authorized to use the button. |
||
13 | * |
||
14 | * @var bool |
||
15 | */ |
||
16 | protected $authorized = true; |
||
17 | |||
18 | /** |
||
19 | * Make a button if condition is true. |
||
20 | * |
||
21 | * @param bool|callable $condition |
||
22 | * @param string|array $options |
||
23 | * @return Button |
||
24 | */ |
||
25 | public static function makeIf($condition, $options) |
||
33 | |||
34 | /** |
||
35 | * Make a new button instance. |
||
36 | * |
||
37 | * @param string|array $options |
||
38 | * @return Button |
||
39 | */ |
||
40 | public static function make($options = []) |
||
48 | |||
49 | /** |
||
50 | * Set authotization status of the button. |
||
51 | * |
||
52 | * @param bool|callable $bool |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function authorized($bool) |
||
61 | |||
62 | /** |
||
63 | * Make a raw button that does not extend anything. |
||
64 | * |
||
65 | * @param array $options |
||
66 | * @return \Yajra\DataTables\Html\Button |
||
67 | */ |
||
68 | public static function raw($options = []) |
||
76 | |||
77 | /** |
||
78 | * Make a button if condition is true. |
||
79 | * |
||
80 | * @param string $permission |
||
81 | * @param string|array $options |
||
82 | * @param Authorizable|null $user |
||
83 | * @return Button |
||
84 | */ |
||
85 | public static function makeIfCan($permission, $options, Authorizable $user = null) |
||
97 | |||
98 | /** |
||
99 | * Set extend option value. |
||
100 | * |
||
101 | * @param string $value |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function extend($value) |
||
110 | |||
111 | /** |
||
112 | * Set editor option value. |
||
113 | * |
||
114 | * @param string $value |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function editor($value) |
||
123 | |||
124 | /** |
||
125 | * @param array $buttons |
||
126 | * @return $this |
||
127 | */ |
||
128 | View Code Duplication | public function buttons(array $buttons) |
|
140 | |||
141 | /** |
||
142 | * @param array $buttons |
||
143 | * @return $this |
||
144 | * @see https://editor.datatables.net/examples/api/cancelButton |
||
145 | */ |
||
146 | View Code Duplication | public function formButtons(array $buttons) |
|
158 | |||
159 | /** |
||
160 | * Set className option value. |
||
161 | * |
||
162 | * @param string $value |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function className($value) |
||
171 | |||
172 | /** |
||
173 | * Set text option value. |
||
174 | * |
||
175 | * @param string $value |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function text($value) |
||
184 | |||
185 | /** |
||
186 | * Set columns option value. |
||
187 | * |
||
188 | * @param mixed $value |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function columns($value) |
||
197 | |||
198 | /** |
||
199 | * Set exportOptions option value. |
||
200 | * |
||
201 | * @param mixed $value |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function exportOptions($value) |
||
210 | |||
211 | /** |
||
212 | * Convert the Fluent instance to an array. |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | public function toArray() |
||
224 | |||
225 | /** |
||
226 | * Set action to submit the form. |
||
227 | * |
||
228 | * @return \Yajra\DataTables\Html\Button |
||
229 | */ |
||
230 | public function actionSubmit() |
||
234 | |||
235 | /** |
||
236 | * Set action option value. |
||
237 | * |
||
238 | * @param string $value |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function action($value) |
||
247 | |||
248 | /** |
||
249 | * Set editor class action handler. |
||
250 | * |
||
251 | * @param string $action |
||
252 | * @return \Yajra\DataTables\Html\Button |
||
253 | */ |
||
254 | public function actionHandler($action) |
||
258 | |||
259 | /** |
||
260 | * Set action to close the form. |
||
261 | * |
||
262 | * @return \Yajra\DataTables\Html\Button |
||
263 | */ |
||
264 | public function actionClose() |
||
268 | } |
||
269 |
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.