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 |
||
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 | * Field value. |
||
41 | * |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $value; |
||
45 | |||
46 | /** |
||
47 | * @var Collection |
||
48 | */ |
||
49 | protected $showAs = []; |
||
50 | |||
51 | /** |
||
52 | * Parent show instance. |
||
53 | * |
||
54 | * @var Show |
||
55 | */ |
||
56 | protected $parent; |
||
57 | |||
58 | /** |
||
59 | * Relation name. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $relation; |
||
64 | |||
65 | /** |
||
66 | * If show contents in box. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | public $wrapped = true; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $fileTypes = [ |
||
76 | 'image' => 'png|jpg|jpeg|tmp|gif', |
||
77 | 'word' => 'doc|docx', |
||
78 | 'excel' => 'xls|xlsx|cvs', |
||
79 | 'powerpoint' => 'ppt|pptx', |
||
80 | 'pdf' => 'pdf', |
||
81 | 'code' => 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx', |
||
82 | 'archive' => 'zip|tar\.gz|rar|rpm', |
||
83 | 'txt' => 'txt|pac|log|md', |
||
84 | 'audio' => 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg', |
||
85 | 'video' => 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg' |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * Field constructor. |
||
90 | * |
||
91 | * @param string $name |
||
92 | * @param string $label |
||
93 | */ |
||
94 | public function __construct($name = '', $label = '') |
||
102 | |||
103 | /** |
||
104 | * Set parent show instance. |
||
105 | * |
||
106 | * @param Show $show |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setParent(Show $show) |
||
116 | |||
117 | /** |
||
118 | * Get name of this column. |
||
119 | * |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function getName() |
||
126 | |||
127 | /** |
||
128 | * Format label. |
||
129 | * |
||
130 | * @param $label |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function formatLabel($label) |
||
140 | |||
141 | /** |
||
142 | * Get label of the column. |
||
143 | * |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function getLabel() |
||
150 | |||
151 | /** |
||
152 | * Field display callback. |
||
153 | * |
||
154 | * @param callable $callable |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function as(callable $callable) |
||
164 | |||
165 | /** |
||
166 | * Display field using array value map. |
||
167 | * |
||
168 | * @param array $values |
||
169 | * @param null $default |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | View Code Duplication | public function using(array $values, $default = null) |
|
183 | |||
184 | /** |
||
185 | * Show field as a image. |
||
186 | * |
||
187 | * @param string $server |
||
188 | * @param int $width |
||
189 | * @param int $height |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function image($server = '', $width = 200, $height = 200) |
||
207 | |||
208 | /** |
||
209 | * Show field as a file. |
||
210 | * |
||
211 | * @param string $server |
||
212 | * @param bool $download |
||
213 | * @return Field |
||
214 | */ |
||
215 | public function file($server = '', $download = true) |
||
258 | |||
259 | /** |
||
260 | * Show field as a link. |
||
261 | * |
||
262 | * @param string $href |
||
263 | * @param string $target |
||
264 | * |
||
265 | * @return Field |
||
266 | */ |
||
267 | public function link($href = '', $target = '_blank') |
||
275 | |||
276 | /** |
||
277 | * Show field as labels. |
||
278 | * |
||
279 | * @param string $style |
||
280 | * |
||
281 | * @return Field |
||
282 | */ |
||
283 | View Code Duplication | public function label($style = 'success') |
|
295 | |||
296 | /** |
||
297 | * Show field as badges. |
||
298 | * |
||
299 | * @param string $style |
||
300 | * |
||
301 | * @return Field |
||
302 | */ |
||
303 | View Code Duplication | public function badge($style = 'blue') |
|
315 | |||
316 | /** |
||
317 | * Show field as json code. |
||
318 | * |
||
319 | * @return Field |
||
320 | */ |
||
321 | public function json() |
||
339 | |||
340 | /** |
||
341 | * Get file icon |
||
342 | * |
||
343 | * @param string $file |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getFileIcon($file = '') |
||
358 | |||
359 | /** |
||
360 | * Set value for this field. |
||
361 | * |
||
362 | * @param Model $model |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setValue(Model $model) |
||
380 | |||
381 | /** |
||
382 | * Set relation name for this field. |
||
383 | * |
||
384 | * @param string $relation |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function setRelation($relation) |
||
394 | |||
395 | /** |
||
396 | * @param string $method |
||
397 | * @param array $arguments |
||
398 | * |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function __call($method, $arguments = []) |
||
414 | |||
415 | /** |
||
416 | * Get all variables passed to field view. |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | protected function variables() |
||
428 | |||
429 | /** |
||
430 | * Render this field. |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | public function render() |
||
447 | } |
||
448 |
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.