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 |
||
14 | class Field implements Renderable |
||
15 | { |
||
16 | use Macroable { |
||
17 | __call as macroCall; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $view = 'admin::show.field'; |
||
24 | |||
25 | /** |
||
26 | * Name of column. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $name; |
||
31 | |||
32 | /** |
||
33 | * Label of column. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $label; |
||
38 | |||
39 | /** |
||
40 | * Escape field value or not. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $escape = true; |
||
45 | |||
46 | /** |
||
47 | * Field value. |
||
48 | * |
||
49 | * @var mixed |
||
50 | */ |
||
51 | protected $value; |
||
52 | |||
53 | /** |
||
54 | * @var Collection |
||
55 | */ |
||
56 | protected $showAs = []; |
||
57 | |||
58 | /** |
||
59 | * Parent show instance. |
||
60 | * |
||
61 | * @var Show |
||
62 | */ |
||
63 | protected $parent; |
||
64 | |||
65 | /** |
||
66 | * Relation name. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $relation; |
||
71 | |||
72 | /** |
||
73 | * If show contents in box. |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | public $wrapped = true; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $fileTypes = [ |
||
83 | 'image' => 'png|jpg|jpeg|tmp|gif', |
||
84 | 'word' => 'doc|docx', |
||
85 | 'excel' => 'xls|xlsx|csv', |
||
86 | 'powerpoint' => 'ppt|pptx', |
||
87 | 'pdf' => 'pdf', |
||
88 | 'code' => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx', |
||
89 | 'archive' => 'zip|tar\.gz|rar|rpm', |
||
90 | 'txt' => 'txt|pac|log|md', |
||
91 | 'audio' => 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg', |
||
92 | 'video' => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * Field constructor. |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @param string $label |
||
100 | */ |
||
101 | public function __construct($name = '', $label = '') |
||
109 | |||
110 | /** |
||
111 | * Set parent show instance. |
||
112 | * |
||
113 | * @param Show $show |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function setParent(Show $show) |
||
123 | |||
124 | /** |
||
125 | * Get name of this column. |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | public function getName() |
||
133 | |||
134 | /** |
||
135 | * Format label. |
||
136 | * |
||
137 | * @param $label |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | protected function formatLabel($label) |
||
147 | |||
148 | /** |
||
149 | * Get label of the column. |
||
150 | * |
||
151 | * @return mixed |
||
152 | */ |
||
153 | public function getLabel() |
||
157 | |||
158 | /** |
||
159 | * Field display callback. |
||
160 | * |
||
161 | * @param callable $callable |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function as(callable $callable) |
||
171 | |||
172 | /** |
||
173 | * Display field using array value map. |
||
174 | * |
||
175 | * @param array $values |
||
176 | * @param null $default |
||
177 | * |
||
178 | * @return $this |
||
179 | */ |
||
180 | View Code Duplication | public function using(array $values, $default = null) |
|
190 | |||
191 | /** |
||
192 | * Show field as a image. |
||
193 | * |
||
194 | * @param string $server |
||
195 | * @param int $width |
||
196 | * @param int $height |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function image($server = '', $width = 200, $height = 200) |
||
224 | |||
225 | /** |
||
226 | * Show field as a file. |
||
227 | * |
||
228 | * @param string $server |
||
229 | * @param bool $download |
||
230 | * |
||
231 | * @return Field |
||
232 | */ |
||
233 | public function file($server = '', $download = true) |
||
276 | |||
277 | /** |
||
278 | * Show field as a link. |
||
279 | * |
||
280 | * @param string $href |
||
281 | * @param string $target |
||
282 | * |
||
283 | * @return Field |
||
284 | */ |
||
285 | public function link($href = '', $target = '_blank') |
||
293 | |||
294 | /** |
||
295 | * Show field as labels. |
||
296 | * |
||
297 | * @param string $style |
||
298 | * |
||
299 | * @return Field |
||
300 | */ |
||
301 | View Code Duplication | public function label($style = 'success') |
|
313 | |||
314 | /** |
||
315 | * Show field as badges. |
||
316 | * |
||
317 | * @param string $style |
||
318 | * |
||
319 | * @return Field |
||
320 | */ |
||
321 | View Code Duplication | public function badge($style = 'blue') |
|
333 | |||
334 | /** |
||
335 | * Show field as json code. |
||
336 | * |
||
337 | * @return Field |
||
338 | */ |
||
339 | public function json() |
||
355 | |||
356 | /** |
||
357 | * Get file icon. |
||
358 | * |
||
359 | * @param string $file |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | public function getFileIcon($file = '') |
||
375 | |||
376 | /** |
||
377 | * Set escape or not for this field. |
||
378 | * |
||
379 | * @param bool $escape |
||
380 | * |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function setEscape($escape = true) |
||
389 | |||
390 | /** |
||
391 | * Unescape for this field. |
||
392 | * |
||
393 | * @return Field |
||
394 | */ |
||
395 | public function unescape() |
||
399 | |||
400 | /** |
||
401 | * Set value for this field. |
||
402 | * |
||
403 | * @param Model $model |
||
404 | * |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function setValue(Model $model) |
||
421 | |||
422 | /** |
||
423 | * Set relation name for this field. |
||
424 | * |
||
425 | * @param string $relation |
||
426 | * |
||
427 | * @return $this |
||
428 | */ |
||
429 | public function setRelation($relation) |
||
435 | |||
436 | /** |
||
437 | * @param string $method |
||
438 | * @param array $arguments |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function __call($method, $arguments = []) |
||
455 | |||
456 | /** |
||
457 | * Get all variables passed to field view. |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | protected function variables() |
||
470 | |||
471 | /** |
||
472 | * Render this field. |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | public function render() |
||
489 | } |
||
490 |
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.