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_user 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_user, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class acf_field_user 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() { |
|
| 54 | |||
| 55 | |||
| 56 | /* |
||
| 57 | * get_choices |
||
| 58 | * |
||
| 59 | * This function will return an array of data formatted for use in a select2 AJAX response |
||
| 60 | * |
||
| 61 | * @type function |
||
| 62 | * @date 15/10/2014 |
||
| 63 | * @since 5.0.9 |
||
| 64 | * |
||
| 65 | * @param $options (array) |
||
| 66 | * @return (array) |
||
| 67 | */ |
||
| 68 | |||
| 69 | function get_choices( $options = array() ) { |
||
| 215 | |||
| 216 | |||
| 217 | /* |
||
| 218 | * ajax_query |
||
| 219 | * |
||
| 220 | * description |
||
| 221 | * |
||
| 222 | * @type function |
||
| 223 | * @date 24/10/13 |
||
| 224 | * @since 5.0.0 |
||
| 225 | * |
||
| 226 | * @param $post_id (int) |
||
| 227 | * @return $post_id (int) |
||
| 228 | */ |
||
| 229 | |||
| 230 | View Code Duplication | function ajax_query() { |
|
| 257 | |||
| 258 | |||
| 259 | /* |
||
| 260 | * get_result |
||
| 261 | * |
||
| 262 | * This function returns the HTML for a result |
||
| 263 | * |
||
| 264 | * @type function |
||
| 265 | * @date 1/11/2013 |
||
| 266 | * @since 5.0.0 |
||
| 267 | * |
||
| 268 | * @param $post (object) |
||
| 269 | * @param $field (array) |
||
| 270 | * @param $post_id (int) the post_id to which this value is saved to |
||
| 271 | * @return (string) |
||
| 272 | */ |
||
| 273 | |||
| 274 | function get_result( $user, $field, $post_id = 0 ) { |
||
| 314 | |||
| 315 | |||
| 316 | /* |
||
| 317 | * user_search_columns |
||
| 318 | * |
||
| 319 | * This function will modify the columns which the user AJAX search looks in |
||
| 320 | * |
||
| 321 | * @type function |
||
| 322 | * @date 17/06/2014 |
||
| 323 | * @since 5.0.0 |
||
| 324 | * |
||
| 325 | * @param $columns (array) |
||
| 326 | * @return $columns |
||
| 327 | */ |
||
| 328 | |||
| 329 | function user_search_columns( $columns, $search, $WP_User_Query ) { |
||
| 353 | |||
| 354 | /* |
||
| 355 | * render_field() |
||
| 356 | * |
||
| 357 | * Create the HTML interface for your field |
||
| 358 | * |
||
| 359 | * @type action |
||
| 360 | * @since 3.6 |
||
| 361 | * @date 23/01/13 |
||
| 362 | * |
||
| 363 | * @param $field - an array holding all the field's data |
||
| 364 | */ |
||
| 365 | |||
| 366 | function render_field( $field ) { |
||
| 408 | |||
| 409 | |||
| 410 | /* |
||
| 411 | * render_field_settings() |
||
| 412 | * |
||
| 413 | * Create extra options for your field. This is rendered when editing a field. |
||
| 414 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
||
| 415 | * |
||
| 416 | * @type action |
||
| 417 | * @since 3.6 |
||
| 418 | * @date 23/01/13 |
||
| 419 | * |
||
| 420 | * @param $field - an array holding all the field's data |
||
| 421 | */ |
||
| 422 | |||
| 423 | function render_field_settings( $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 | function update_value( $value, $post_id, $field ) { |
||
| 517 | |||
| 518 | |||
| 519 | /* |
||
| 520 | * load_value() |
||
| 521 | * |
||
| 522 | * This filter is applied to the $value after it is loaded from the db |
||
| 523 | * |
||
| 524 | * @type filter |
||
| 525 | * @since 3.6 |
||
| 526 | * @date 23/01/13 |
||
| 527 | * |
||
| 528 | * @param $value (mixed) the value found in the database |
||
| 529 | * @param $post_id (mixed) the $post_id from which the value was loaded |
||
| 530 | * @param $field (array) the field array holding all the field options |
||
| 531 | * @return $value |
||
| 532 | */ |
||
| 533 | |||
| 534 | function load_value( $value, $post_id, $field ) { |
||
| 547 | |||
| 548 | |||
| 549 | /* |
||
| 550 | * format_value() |
||
| 551 | * |
||
| 552 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
||
| 553 | * |
||
| 554 | * @type filter |
||
| 555 | * @since 3.6 |
||
| 556 | * @date 23/01/13 |
||
| 557 | * |
||
| 558 | * @param $value (mixed) the value which was loaded from the database |
||
| 559 | * @param $post_id (mixed) the $post_id from which the value was loaded |
||
| 560 | * @param $field (array) the field array holding all the field options |
||
| 561 | * |
||
| 562 | * @return $value (mixed) the modified value |
||
| 563 | */ |
||
| 564 | |||
| 565 | function format_value( $value, $post_id, $field ) { |
||
| 629 | |||
| 630 | } |
||
| 631 | |||
| 637 |
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.