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 Field 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 Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Field |
||
16 | { |
||
17 | /** |
||
18 | * Element id. |
||
19 | * |
||
20 | * @var array|string |
||
21 | */ |
||
22 | protected $id; |
||
23 | |||
24 | /** |
||
25 | * Element value. |
||
26 | * |
||
27 | * @var mixed |
||
28 | */ |
||
29 | protected $value; |
||
30 | |||
31 | /** |
||
32 | * Field original value. |
||
33 | * |
||
34 | * @var mixed |
||
35 | */ |
||
36 | protected $original; |
||
37 | |||
38 | /** |
||
39 | * Field default value. |
||
40 | * |
||
41 | * @var mixed |
||
42 | */ |
||
43 | protected $default; |
||
44 | |||
45 | /** |
||
46 | * Element label. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $label = ''; |
||
51 | |||
52 | /** |
||
53 | * Column name. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $column = ''; |
||
58 | |||
59 | /** |
||
60 | * Variables of elements. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $variables = []; |
||
65 | |||
66 | /** |
||
67 | * Options for specify elements. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $options = []; |
||
72 | |||
73 | /** |
||
74 | * Validation rules. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $rules = ''; |
||
79 | |||
80 | /** |
||
81 | * Css required by this field. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected static $css = []; |
||
86 | |||
87 | /** |
||
88 | * Js required by this field. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected static $js = []; |
||
93 | |||
94 | /** |
||
95 | * Script for field. |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $script = ''; |
||
100 | |||
101 | /** |
||
102 | * Element attributes. |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | protected $attributes = []; |
||
107 | |||
108 | /** |
||
109 | * Parent form. |
||
110 | * |
||
111 | * @var Form |
||
112 | */ |
||
113 | protected $form = null; |
||
114 | |||
115 | /** |
||
116 | * View for field to render. |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | protected $view = ''; |
||
121 | |||
122 | /** |
||
123 | * Help block. |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | protected $help = []; |
||
128 | |||
129 | /** |
||
130 | * Field constructor. |
||
131 | * |
||
132 | * @param $column |
||
133 | * @param array $arguments |
||
134 | */ |
||
135 | public function __construct($column, $arguments = []) |
||
141 | |||
142 | /** |
||
143 | * Get assets required by this field. |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public static function getAssets() |
||
154 | |||
155 | /** |
||
156 | * Format the field element id. |
||
157 | * |
||
158 | * @param string|array $column |
||
159 | * |
||
160 | * @return string|array |
||
161 | */ |
||
162 | protected function formatId($column) |
||
166 | |||
167 | /** |
||
168 | * Format the label value. |
||
169 | * |
||
170 | * @param array $arguments |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function formatLabel($arguments = []) |
||
182 | |||
183 | /** |
||
184 | * Format the name of the field. |
||
185 | * |
||
186 | * @param string $column |
||
187 | * |
||
188 | * @return array|mixed|string |
||
189 | */ |
||
190 | protected function formatName($column) |
||
218 | |||
219 | /** |
||
220 | * Fill data to the field. |
||
221 | * |
||
222 | * @param $data |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | View Code Duplication | public function fill($data) |
|
238 | |||
239 | /** |
||
240 | * Set original value to the field. |
||
241 | * |
||
242 | * @param array $data |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | View Code Duplication | public function setOriginal($data) |
|
258 | |||
259 | /** |
||
260 | * @param Form $form |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setForm(Form $form) |
||
270 | |||
271 | /** |
||
272 | * Set the field options. |
||
273 | * |
||
274 | * @param array $options |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function options($options = []) |
||
288 | |||
289 | /** |
||
290 | * Get or set rules. |
||
291 | * |
||
292 | * @param null $rules |
||
293 | * |
||
294 | * @return $this|array |
||
295 | */ |
||
296 | public function rules($rules = null) |
||
308 | |||
309 | |||
310 | protected function getRules() |
||
314 | |||
315 | /** |
||
316 | * Set or get value of the field. |
||
317 | * |
||
318 | * @param null $value |
||
319 | * |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function value($value = null) |
||
332 | |||
333 | /** |
||
334 | * Set help block for current field. |
||
335 | * |
||
336 | * @param string $text |
||
337 | * @param string $icon |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function help($text = '', $icon = 'fa-info-circle') |
||
347 | |||
348 | /** |
||
349 | * Get column of the field. |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public function column() |
||
357 | |||
358 | /** |
||
359 | * Get label of the field. |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | public function label() |
||
367 | |||
368 | /** |
||
369 | * Get original value of the field. |
||
370 | * |
||
371 | * @return mixed |
||
372 | */ |
||
373 | public function original() |
||
377 | |||
378 | /** |
||
379 | * Validate input field data. |
||
380 | * |
||
381 | * @param array $input |
||
382 | * |
||
383 | * @return bool|Validator |
||
384 | */ |
||
385 | public function validate(array $input) |
||
421 | |||
422 | /** |
||
423 | * Add html attributes to elements. |
||
424 | * |
||
425 | * @param array|string $attribute |
||
426 | * @param mixed $value |
||
427 | * |
||
428 | * @return $this |
||
429 | */ |
||
430 | public function attribute($attribute, $value = null) |
||
440 | |||
441 | /** |
||
442 | * Set the field as readonly mode. |
||
443 | * |
||
444 | * @return Field |
||
445 | */ |
||
446 | public function readOnly() |
||
450 | |||
451 | /** |
||
452 | * Format the field attributes. |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | protected function formatAttributes() |
||
466 | |||
467 | /** |
||
468 | * Get the view variables of this field. |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | protected function variables() |
||
484 | |||
485 | /** |
||
486 | * Get view of this field. |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public function getView() |
||
500 | |||
501 | /** |
||
502 | * Render this filed. |
||
503 | * |
||
504 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
505 | */ |
||
506 | public function render() |
||
512 | |||
513 | /** |
||
514 | * @param $method |
||
515 | * @param $arguments |
||
516 | * |
||
517 | * @return $this |
||
518 | */ |
||
519 | public function __call($method, $arguments) |
||
527 | } |
||
528 |
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.