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 |
||
12 | class Field implements Renderable |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $view = 'admin::show.field'; |
||
18 | |||
19 | /** |
||
20 | * Name of column. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $name; |
||
25 | |||
26 | /** |
||
27 | * Label of column. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $label; |
||
32 | |||
33 | /** |
||
34 | * Field value. |
||
35 | * |
||
36 | * @var mixed |
||
37 | */ |
||
38 | protected $value; |
||
39 | |||
40 | /** |
||
41 | * @var Collection |
||
42 | */ |
||
43 | protected $showAs = []; |
||
44 | |||
45 | /** |
||
46 | * Parent show instance. |
||
47 | * |
||
48 | * @var Show |
||
49 | */ |
||
50 | protected $parent; |
||
51 | |||
52 | /** |
||
53 | * Relation name. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $relation; |
||
58 | |||
59 | /** |
||
60 | * Field constructor. |
||
61 | * |
||
62 | * @param string $name |
||
63 | * @param string $label |
||
64 | */ |
||
65 | public function __construct($name = '', $label = '') |
||
73 | |||
74 | /** |
||
75 | * Set parent show instance. |
||
76 | * |
||
77 | * @param Show $show |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function setParent(Show $show) |
||
87 | |||
88 | /** |
||
89 | * Get name of this column. |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function getName() |
||
97 | |||
98 | /** |
||
99 | * Format label. |
||
100 | * |
||
101 | * @param $label |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | protected function formatLabel($label) |
||
111 | |||
112 | /** |
||
113 | * Get label of the column. |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function getLabel() |
||
121 | |||
122 | /** |
||
123 | * Field display callback. |
||
124 | * |
||
125 | * @param callable $callable |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function as(callable $callable) |
||
135 | |||
136 | /** |
||
137 | * Set value mapping. |
||
138 | * |
||
139 | * @param array $values |
||
140 | * @param null $default |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function values(array $values, $default = null) |
||
150 | |||
151 | /** |
||
152 | * Show field as a image. |
||
153 | * |
||
154 | * @param string $server |
||
155 | * @param int $width |
||
156 | * @param int $height |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function image($server = '', $width = 200, $height = 200) |
||
174 | |||
175 | /** |
||
176 | * Show field as a link. |
||
177 | * |
||
178 | * @param string $href |
||
179 | * @param string $target |
||
180 | * |
||
181 | * @return Field |
||
182 | */ |
||
183 | public function link($href = '', $target = '_blank') |
||
191 | |||
192 | /** |
||
193 | * Show field as labels. |
||
194 | * |
||
195 | * @param string $style |
||
196 | * |
||
197 | * @return Field |
||
198 | */ |
||
199 | View Code Duplication | public function label($style = 'success') |
|
211 | |||
212 | /** |
||
213 | * Show field as badges. |
||
214 | * |
||
215 | * @param string $style |
||
216 | * |
||
217 | * @return Field |
||
218 | */ |
||
219 | View Code Duplication | public function badge($style = 'blue') |
|
231 | |||
232 | /** |
||
233 | * Set value for this field. |
||
234 | * |
||
235 | * @param Model $model |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function setValue(Model $model) |
||
253 | |||
254 | /** |
||
255 | * Set relation name for this field. |
||
256 | * |
||
257 | * @param string $relation |
||
258 | * |
||
259 | * @return $this |
||
260 | */ |
||
261 | public function setRelation($relation) |
||
267 | |||
268 | /** |
||
269 | * @param string $method |
||
270 | * @param array $arguments |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function __call($method, $arguments = []) |
||
283 | |||
284 | /** |
||
285 | * Render this field. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function render() |
||
305 | } |
||
306 |
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.