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 acf_field_select 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 acf_field_select, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class acf_field_select extends acf_field { |
||
17 | |||
18 | |||
19 | /* |
||
20 | * __construct |
||
21 | * |
||
22 | * This function will setup the field type data |
||
23 | * |
||
24 | * @type function |
||
25 | * @date 5/03/2014 |
||
26 | * @since 5.0.0 |
||
27 | * |
||
28 | * @param n/a |
||
29 | * @return n/a |
||
30 | */ |
||
|
|||
31 | |||
32 | View Code Duplication | function __construct() { |
|
60 | |||
61 | |||
62 | /* |
||
63 | * query_posts |
||
64 | * |
||
65 | * description |
||
66 | * |
||
67 | * @type function |
||
68 | * @date 24/10/13 |
||
69 | * @since 5.0.0 |
||
70 | * |
||
71 | * @param n/a |
||
72 | * @return n/a |
||
73 | */ |
||
74 | |||
75 | function ajax_query() { |
||
143 | |||
144 | |||
145 | /* |
||
146 | * render_field() |
||
147 | * |
||
148 | * Create the HTML interface for your field |
||
149 | * |
||
150 | * @param $field - an array holding all the field's data |
||
151 | * |
||
152 | * @type action |
||
153 | * @since 3.6 |
||
154 | * @date 23/01/13 |
||
155 | */ |
||
156 | |||
157 | function render_field( $field ) { |
||
264 | |||
265 | |||
266 | /* |
||
267 | * walk |
||
268 | * |
||
269 | * description |
||
270 | * |
||
271 | * @type function |
||
272 | * @date 22/12/2015 |
||
273 | * @since 5.3.2 |
||
274 | * |
||
275 | * @param $post_id (int) |
||
276 | * @return $post_id (int) |
||
277 | */ |
||
278 | |||
279 | function walk( $choices, $values ) { |
||
330 | |||
331 | |||
332 | /* |
||
333 | * render_field_settings() |
||
334 | * |
||
335 | * Create extra options for your field. This is rendered when editing a field. |
||
336 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
||
337 | * |
||
338 | * @type action |
||
339 | * @since 3.6 |
||
340 | * @date 23/01/13 |
||
341 | * |
||
342 | * @param $field - an array holding all the field's data |
||
343 | */ |
||
344 | |||
345 | function render_field_settings( $field ) { |
||
426 | |||
427 | |||
428 | /* |
||
429 | * load_value() |
||
430 | * |
||
431 | * This filter is applied to the $value after it is loaded from the db |
||
432 | * |
||
433 | * @type filter |
||
434 | * @since 3.6 |
||
435 | * @date 23/01/13 |
||
436 | * |
||
437 | * @param $value (mixed) the value found in the database |
||
438 | * @param $post_id (mixed) the $post_id from which the value was loaded |
||
439 | * @param $field (array) the field array holding all the field options |
||
440 | * @return $value |
||
441 | */ |
||
442 | |||
443 | function load_value( $value, $post_id, $field ) { |
||
452 | |||
453 | |||
454 | /* |
||
455 | * update_field() |
||
456 | * |
||
457 | * This filter is appied to the $field before it is saved to the database |
||
458 | * |
||
459 | * @type filter |
||
460 | * @since 3.6 |
||
461 | * @date 23/01/13 |
||
462 | * |
||
463 | * @param $field - the field array holding all the field options |
||
464 | * @param $post_id - the field group ID (post_type = acf) |
||
465 | * |
||
466 | * @return $field - the modified field |
||
467 | */ |
||
468 | |||
469 | View Code Duplication | function update_field( $field ) { |
|
479 | |||
480 | |||
481 | /* |
||
482 | * update_value() |
||
483 | * |
||
484 | * This filter is appied to the $value before it is updated in the db |
||
485 | * |
||
486 | * @type filter |
||
487 | * @since 3.6 |
||
488 | * @date 23/01/13 |
||
489 | * |
||
490 | * @param $value - the value which will be saved in the database |
||
491 | * @param $post_id - the $post_id of which the value will be saved |
||
492 | * @param $field - the field array holding all the field options |
||
493 | * |
||
494 | * @return $value - the modified value |
||
495 | */ |
||
496 | |||
497 | View Code Duplication | function update_value( $value, $post_id, $field ) { |
|
519 | |||
520 | |||
521 | /* |
||
522 | * enqueue_assets |
||
523 | * |
||
524 | * This function will enqueue the Select2 JS library |
||
525 | * moved to the footer in an attempt to allow 3rd party to register Select2 paths |
||
526 | * |
||
527 | * @type function |
||
528 | * @date 16/12/2015 |
||
529 | * @since 5.3.3 |
||
530 | * |
||
531 | * @param n/a |
||
532 | * @return n/a |
||
533 | */ |
||
534 | |||
535 | function input_admin_enqueue_scripts() { |
||
540 | |||
541 | |||
542 | function field_group_admin_enqueue_scripts() { |
||
547 | |||
548 | function enqueue_assets() { |
||
601 | |||
602 | } |
||
603 | |||
609 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.