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 MetaboxValues 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 MetaboxValues, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MetaboxValues{ |
||
12 | |||
13 | /** |
||
14 | * Assigns the proper testing data to a custom metabox. |
||
15 | * |
||
16 | * Swaps through the possible types of CMB2 supported fields and |
||
17 | * insert the appropriate data based on type & id. |
||
18 | * Some types are not yet supported due to low frequency of use. |
||
19 | * |
||
20 | * @see TestContent, add_post_meta |
||
21 | * |
||
22 | * @param int $post_id Single post ID. |
||
23 | * @param array $cmb custom metabox array from CMB2. |
||
24 | */ |
||
25 | public function get_values( $post_id, $cmb, $connected ){ |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Pulls a text string for CMB field. |
||
177 | * |
||
178 | * @see TestContent |
||
179 | * |
||
180 | * @param array $cmb Metabox data |
||
181 | * @return string cmb value |
||
182 | */ |
||
183 | private function text( $cmb ){ |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Pulls a URL value CMB field. |
||
217 | * |
||
218 | * @see TestContent |
||
219 | * |
||
220 | * @param array $cmb Metabox data |
||
221 | * @return string cmb value |
||
222 | */ |
||
223 | private function url( $cmb ){ |
||
228 | |||
229 | |||
230 | /** |
||
231 | * Pulls an email address for CMB field. |
||
232 | * |
||
233 | * @see TestContent |
||
234 | * |
||
235 | * @param array $cmb Metabox data |
||
236 | * @return string cmb value |
||
237 | */ |
||
238 | private function email( $cmb ){ |
||
243 | |||
244 | |||
245 | /** |
||
246 | * Pulls a random valnumberue for CMB field. |
||
247 | * |
||
248 | * @param array $cmb Metabox data |
||
249 | * @return int cmb value |
||
250 | */ |
||
251 | private function number( $cmb ){ |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Pulls a time of day for CMB field. |
||
271 | * |
||
272 | * @see TestContent |
||
273 | * |
||
274 | * @param array $cmb Metabox data |
||
275 | * @return string cmb value |
||
276 | */ |
||
277 | private function time( $cmb ){ |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Pulls a timezone for CMB field. |
||
286 | * |
||
287 | * @see TestContent |
||
288 | * |
||
289 | * @param array $cmb Metabox data |
||
290 | * @return string cmb value |
||
291 | */ |
||
292 | private function timezone( $cmb ){ |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Pulls a date for CMB field. |
||
301 | * |
||
302 | * @see TestContent |
||
303 | * |
||
304 | * @param array $cmb Metabox data |
||
305 | * @return string cmb value |
||
306 | */ |
||
307 | private function date( $cmb ){ |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Pulls a timestamp for CMB field. |
||
316 | * |
||
317 | * @see TestContent |
||
318 | * |
||
319 | * @param array $cmb Metabox data |
||
320 | * @return string cmb value |
||
321 | */ |
||
322 | private function timestamp( $cmb ){ |
||
327 | |||
328 | |||
329 | /** |
||
330 | * Pulls a random hexadecimal color code for CMB field. |
||
331 | * |
||
332 | * @param array $cmb Metabox data |
||
333 | * @return string cmb value |
||
334 | */ |
||
335 | private function color( $cmb ){ |
||
340 | |||
341 | |||
342 | /** |
||
343 | * Pulls a long text string for CMB field. |
||
344 | * |
||
345 | * @see TestContent |
||
346 | * |
||
347 | * @param array $cmb Metabox data |
||
348 | * @return string cmb value |
||
349 | */ |
||
350 | private function textarea( $cmb ){ |
||
361 | |||
362 | |||
363 | /** |
||
364 | * Pulls a random radio field value for CMB field. |
||
365 | * |
||
366 | * @see TestContent |
||
367 | * |
||
368 | * @param array $cmb Metabox data |
||
369 | * @return string cmb value |
||
370 | */ |
||
371 | private function radio( $cmb ){ |
||
380 | |||
381 | |||
382 | /** |
||
383 | * Pulls a random checkbox field value for CMB field. |
||
384 | * |
||
385 | * @see TestContent |
||
386 | * |
||
387 | * @param array $cmb Metabox data |
||
388 | * @return string cmb value |
||
389 | */ |
||
390 | private function checkbox( $cmb ){ |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Pulls a random multicheck field value for CMB field. |
||
405 | * |
||
406 | * @see TestContent |
||
407 | * |
||
408 | * @param array $cmb Metabox data |
||
409 | * @return array cmb value |
||
410 | */ |
||
411 | private function multicheck( $cmb ){ |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Pulls an HTML paragraph string for CMB field. |
||
432 | * |
||
433 | * @see TestContent |
||
434 | * |
||
435 | * @param array $cmb Metabox data |
||
436 | * @return string cmb value |
||
437 | */ |
||
438 | private function wysiwyg( $cmb ){ |
||
443 | |||
444 | |||
445 | /** |
||
446 | * Pulls an image URL for CMB field. |
||
447 | * |
||
448 | * @see TestContent |
||
449 | * |
||
450 | * @param array $cmb Metabox data |
||
451 | * @param int $post_id Post ID |
||
452 | * @param bool $connected Whether we're connected to the Internets or not |
||
453 | * @return mixed string|object cmb value or WP_Error object |
||
454 | */ |
||
455 | private function file( $cmb, $post_id, $connected ){ |
||
465 | |||
466 | |||
467 | /** |
||
468 | * Pulls an Oembed URL for CMB field. |
||
469 | * |
||
470 | * @see TestContent |
||
471 | * |
||
472 | * @param array $cmb Metabox data |
||
473 | * @return string cmb value |
||
474 | */ |
||
475 | private function oembed( $cmb ){ |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Update the metabox with new data. |
||
484 | * |
||
485 | * @access private |
||
486 | * |
||
487 | * @see add_post_meta |
||
488 | * |
||
489 | * @param int $post_id Post ID. |
||
490 | * @param string $value Value to add into the database. |
||
491 | * @param array $cmb SMB data. |
||
492 | */ |
||
493 | private function update_meta( $post_id, $value, $cmb ){ |
||
515 | |||
516 | } |
||
517 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.