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 |
||
20 | abstract class Field { |
||
21 | |||
22 | /** |
||
23 | * Field type. |
||
24 | * |
||
25 | * @access public |
||
26 | * @var string |
||
27 | */ |
||
28 | public $type = ''; |
||
29 | |||
30 | /** |
||
31 | * Field name. |
||
32 | * |
||
33 | * @access public |
||
34 | * @var string |
||
35 | */ |
||
36 | public $name = ''; |
||
37 | |||
38 | /** |
||
39 | * Field Id. |
||
40 | * |
||
41 | * @access public |
||
42 | * @var string |
||
43 | */ |
||
44 | public $id = ''; |
||
45 | |||
46 | /** |
||
47 | * Field title (label). |
||
48 | * |
||
49 | * @access public |
||
50 | * @var string |
||
51 | */ |
||
52 | public $title = ''; |
||
53 | |||
54 | /** |
||
55 | * Field type class. |
||
56 | * |
||
57 | * @access protected |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $type_class = ''; |
||
61 | |||
62 | /** |
||
63 | * CSS classes. |
||
64 | * |
||
65 | * @access public |
||
66 | * @var string |
||
67 | */ |
||
68 | public $class = ''; |
||
69 | |||
70 | /** |
||
71 | * CSS styles. |
||
72 | * |
||
73 | * @access public |
||
74 | * @var string |
||
75 | */ |
||
76 | public $style = ''; |
||
77 | |||
78 | /** |
||
79 | * Description. |
||
80 | * |
||
81 | * @access public |
||
82 | * @var string |
||
83 | */ |
||
84 | public $description = ''; |
||
85 | |||
86 | /** |
||
87 | * Tooltip text. |
||
88 | * |
||
89 | * @access public |
||
90 | * @var string |
||
91 | */ |
||
92 | public $tooltip = ''; |
||
93 | |||
94 | /** |
||
95 | * Attributes. |
||
96 | * |
||
97 | * @access public |
||
98 | * @var string |
||
99 | */ |
||
100 | public $attributes = ''; |
||
101 | |||
102 | /** |
||
103 | * Placeholder. |
||
104 | * |
||
105 | * @access public |
||
106 | * @var string |
||
107 | */ |
||
108 | public $placeholder = ''; |
||
109 | |||
110 | /** |
||
111 | * Options. |
||
112 | * |
||
113 | * @access public |
||
114 | * @var array |
||
115 | */ |
||
116 | public $options = array(); |
||
117 | |||
118 | /** |
||
119 | * Value. |
||
120 | * |
||
121 | * @access public |
||
122 | * @var array|string |
||
123 | */ |
||
124 | public $value = ''; |
||
125 | |||
126 | /** |
||
127 | * Default value. |
||
128 | * |
||
129 | * @access public |
||
130 | * @var array|string |
||
131 | */ |
||
132 | public $default = ''; |
||
133 | |||
134 | public $text = ''; |
||
135 | |||
136 | /** |
||
137 | * Validation result. |
||
138 | * |
||
139 | * @access public |
||
140 | * @var string|true |
||
141 | */ |
||
142 | public $validation = true; |
||
143 | |||
144 | /** |
||
145 | * Construct the field. |
||
146 | * |
||
147 | * Escapes and sets every field property. |
||
148 | * |
||
149 | * @since 3.0.0 |
||
150 | * |
||
151 | * @param array $field Field data. |
||
152 | */ |
||
153 | public function __construct( $field ) { |
||
154 | |||
155 | // Field properties. |
||
156 | if ( isset( $field['title'] ) ) { |
||
157 | $this->title = esc_attr( $field['title'] ); |
||
158 | } |
||
159 | if ( isset( $field['description'] ) ) { |
||
160 | $this->description = wp_kses_post( $field['description'] ); |
||
161 | } |
||
162 | if ( isset( $field['type'] ) ) { |
||
163 | $this->type = esc_attr( $field['type'] ); |
||
164 | } |
||
165 | if ( isset( $field['name'] ) ) { |
||
166 | $this->name = esc_attr( $field['name'] ); |
||
167 | } |
||
168 | if ( isset( $field['id'] ) ) { |
||
169 | $this->id = esc_attr( $field['id'] ); |
||
170 | } |
||
171 | if ( isset( $field['placeholder'] ) ) { |
||
172 | $this->placeholder = esc_attr( $field['placeholder'] ); |
||
173 | } |
||
174 | if ( isset( $field['options'] ) && is_array( $field['options'] ) ) { |
||
175 | $this->options = array_map( 'esc_attr', $field['options'] ); |
||
176 | } |
||
177 | if ( isset( $field['text'] ) ) { |
||
178 | $this->text = $field['text']; |
||
179 | } |
||
180 | |||
181 | // Escaping. |
||
182 | if ( ! empty( $field['escaping'] ) && ( is_string( $field['escaping'] ) || is_array( $field['escaping'] ) ) ) { |
||
183 | View Code Duplication | if ( isset( $field['default'] ) ) { |
|
|
|||
184 | $this->default = $this->escape_callback( $field['escaping'], $field['default'] ); |
||
185 | } |
||
186 | View Code Duplication | if ( isset( $field['value'] ) ) { |
|
187 | $this->value = $this->escape_callback( $field['escaping'], $field['value'] ); |
||
188 | } |
||
189 | } else { |
||
190 | if ( isset( $field['default'] ) ) { |
||
191 | $this->default = $this->escape( $field['default'] ); |
||
192 | } |
||
193 | if ( isset( $field['value'] ) ) { |
||
194 | $this->value = $this->escape( $field['value'] ); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // Validation. |
||
199 | if ( ! empty( $field['validation'] ) ) { |
||
200 | $this->validation = $this->validate( $field['validation'], $this->value ); |
||
201 | } |
||
202 | |||
203 | // CSS classes and styles. |
||
204 | $classes = isset( $field['class'] ) ? $field['class'] : ''; |
||
205 | $this->set_class( $classes ); |
||
206 | if ( isset( $field['style'] ) ) { |
||
207 | $this->set_style( $field['style'] ); |
||
208 | } |
||
209 | |||
210 | // Custom attributes. |
||
211 | if ( isset( $field['attributes'] ) ) { |
||
212 | $this->set_attributes( $field['attributes'] ); |
||
213 | } |
||
214 | |||
215 | // Tooltip markup. |
||
216 | if ( isset( $field['tooltip'] ) ) { |
||
217 | $this->tooltip = ' <i class="simcal-icon-help simcal-help-tip" data-tip="' . esc_attr( $field['tooltip'] ) . '"></i> ' ; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Set custom HTML attributes. |
||
223 | * |
||
224 | * @since 3.0.0 |
||
225 | * |
||
226 | * @param array $attributes |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | View Code Duplication | public function set_attributes( $attributes ) { |
|
231 | |||
232 | $attr = ''; |
||
233 | |||
234 | if ( ! empty( $attributes ) && is_array( $attributes ) ) { |
||
235 | foreach ( $attributes as $k => $v ) { |
||
236 | $attr .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" '; |
||
237 | } |
||
238 | } |
||
239 | |||
240 | $this->attributes = $attr; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Set CSS styles. |
||
245 | * |
||
246 | * @since 3.0.0 |
||
247 | * |
||
248 | * @param array $css |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | View Code Duplication | public function set_style( $css ) { |
|
253 | |||
254 | $styles = ''; |
||
255 | |||
256 | if ( ! empty( $css ) && is_array( $css ) ) { |
||
257 | foreach ( $css as $k => $v ) { |
||
258 | $styles .= esc_attr( $k ) . ': ' . esc_attr( $v ) . '; '; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | $this->style = $styles; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Set classes. |
||
267 | * |
||
268 | * @since 3.0.0 |
||
269 | * |
||
270 | * @param array $class |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | public function set_class( $class ) { |
||
292 | |||
293 | /** |
||
294 | * Escape field value. |
||
295 | * |
||
296 | * Default escape function, a wrapper for esc_attr(). |
||
297 | * |
||
298 | * @since 3.0.0 |
||
299 | * @access protected |
||
300 | * |
||
301 | * @param array|string|int $value |
||
302 | * |
||
303 | * @return array|string |
||
304 | */ |
||
305 | protected function escape( $value ) { |
||
308 | |||
309 | /** |
||
310 | * Escape callback. |
||
311 | * |
||
312 | * Custom escaping function set in field args. |
||
313 | * |
||
314 | * @since 3.0.0 |
||
315 | * @access protected |
||
316 | * |
||
317 | * @param array|string $callback |
||
318 | * @param mixed $value |
||
319 | * |
||
320 | * @return mixed |
||
321 | */ |
||
322 | protected function escape_callback( $callback, $value ) { |
||
328 | |||
329 | /** |
||
330 | * Validation callback. |
||
331 | * |
||
332 | * Custom field validation callback set in field args. |
||
333 | * |
||
334 | * @since 3.0.0 |
||
335 | * @access protected |
||
336 | * |
||
337 | * @param array|string $callback |
||
338 | * @param string $value |
||
339 | * |
||
340 | * @return true|string Expected to return bool (true) if passes, message string if not. |
||
341 | */ |
||
342 | protected function validate( $callback, $value ) { |
||
349 | |||
350 | /** |
||
351 | * Outputs the field markup. |
||
352 | * |
||
353 | * @since 3.0.0 |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | abstract public function html(); |
||
358 | |||
359 | } |
||
360 |
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.