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 Association_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 Association_Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Association_Field extends Field { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * WP_Toolset instance for WP data loading |
||
| 19 | * |
||
| 20 | * @var Carbon_Fields\Toolset\WP_Toolset |
||
| 21 | */ |
||
| 22 | protected $wp_toolset; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Min number of selected items allowed. -1 for no limit |
||
| 26 | * |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | protected $min = -1; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Max number of selected items allowed. -1 for no limit |
||
| 33 | * |
||
| 34 | * @var integer |
||
| 35 | */ |
||
| 36 | protected $max = -1; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Allow items to be added multiple times |
||
| 40 | * |
||
| 41 | * @var boolean |
||
| 42 | */ |
||
| 43 | protected $duplicates_allowed = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Default field value |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $default_value = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Types of entries to associate with. |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $types = array( |
||
| 57 | array( |
||
| 58 | 'type' => 'post', |
||
| 59 | 'post_type' => 'post', |
||
| 60 | ), |
||
| 61 | ); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Create a field from a certain type with the specified label. |
||
| 65 | * |
||
| 66 | * @param string $type Field type |
||
| 67 | * @param string $name Field name |
||
| 68 | * @param string $label Field label |
||
| 69 | */ |
||
| 70 | public function __construct( $type, $name, $label ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritDoc} |
||
| 78 | */ |
||
| 79 | public function set_value_from_input( $input ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritDoc} |
||
| 93 | */ |
||
| 94 | public function set_value( $value ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get value string for legacy value |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | protected function get_value_string_for_legacy_value( $legacy_value ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Convert a colon:separated:string into its expected components |
||
| 121 | * Used for backwards compatibility to CF 1.5 |
||
| 122 | * |
||
| 123 | * @param string $value_string |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | protected function value_string_to_property_array( $value_string ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Convert a colon:separated:string into its expected components |
||
| 148 | * Used for backwards compatibility to CF 1.5 |
||
| 149 | * |
||
| 150 | * @param array $value_string_array |
||
| 151 | * @return array<array> |
||
| 152 | */ |
||
| 153 | protected function value_string_array_to_value_set( $value_string_array ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Used to get the title of an item. |
||
| 179 | * |
||
| 180 | * Can be overriden or extended by the `carbon_association_title` filter. |
||
| 181 | * |
||
| 182 | * @param int $id The database ID of the item. |
||
| 183 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 184 | * @param string $subtype The subtype - "page", "post", "category", etc. |
||
| 185 | * @return string $title The title of the item. |
||
| 186 | */ |
||
| 187 | protected function get_title_by_type( $id, $type, $subtype = '' ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Used to get the label of an item. |
||
| 223 | * |
||
| 224 | * Can be overriden or extended by the `carbon_association_item_label` filter. |
||
| 225 | * |
||
| 226 | * @param int $id The database ID of the item. |
||
| 227 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 228 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
| 229 | * @return string $label The label of the item. |
||
| 230 | */ |
||
| 231 | protected function get_item_label( $id, $type, $subtype = '' ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get post options |
||
| 256 | * |
||
| 257 | * @return array $options |
||
| 258 | */ |
||
| 259 | protected function get_post_options( $type ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get term options |
||
| 291 | * |
||
| 292 | * @return array $options |
||
| 293 | */ |
||
| 294 | protected function get_term_options( $type ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get user options |
||
| 324 | * |
||
| 325 | * @return array $options |
||
| 326 | */ |
||
| 327 | View Code Duplication | protected function get_user_options( $type ) { |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Get comment options |
||
| 356 | * |
||
| 357 | * @return array $options |
||
| 358 | */ |
||
| 359 | View Code Duplication | protected function get_comment_options( $type ) { |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Generate the item options. |
||
| 388 | * |
||
| 389 | * @return array $options The selectable options of the association field. |
||
| 390 | */ |
||
| 391 | public function get_options() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Retrieve the edit link of a particular object. |
||
| 415 | * |
||
| 416 | * @param string $type Object type. |
||
| 417 | * @param int $id ID of the object. |
||
| 418 | * @return string URL of the edit link. |
||
| 419 | */ |
||
| 420 | protected function get_object_edit_link( $type, $id ) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Modify the types. |
||
| 449 | * |
||
| 450 | * @param array $types New types |
||
| 451 | * @return self $this |
||
| 452 | */ |
||
| 453 | public function set_types( $types ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Get the minimum allowed number of selected entries. |
||
| 460 | * |
||
| 461 | * @return int |
||
| 462 | */ |
||
| 463 | public function get_min() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Set the minimum allowed number of selected entries. |
||
| 469 | * |
||
| 470 | * @param int $min |
||
| 471 | * @return self $this |
||
| 472 | */ |
||
| 473 | public function set_min( $min ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the maximum allowed number of selected entries. |
||
| 480 | * |
||
| 481 | * @return int |
||
| 482 | */ |
||
| 483 | public function get_max() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set the maximum allowed number of selected entries. |
||
| 489 | * |
||
| 490 | * @param int $max |
||
| 491 | * @return self $this |
||
| 492 | */ |
||
| 493 | public function set_max( $max ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get whether entry duplicates are allowed. |
||
| 500 | * |
||
| 501 | * @return boolean |
||
| 502 | */ |
||
| 503 | public function get_duplicates_allowed() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set whether entry duplicates are allowed. |
||
| 509 | * |
||
| 510 | * @param boolean $allowed |
||
| 511 | * @return self $this |
||
| 512 | */ |
||
| 513 | public function set_duplicates_allowed( $allowed ) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Specify whether to allow each entry to be selected multiple times. |
||
| 520 | * Backwards-compatibility alias. |
||
| 521 | * |
||
| 522 | * @param boolean $allow |
||
| 523 | * @return self $this |
||
| 524 | */ |
||
| 525 | public function allow_duplicates( $allow = true ) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Converts the field values into a usable associative array. |
||
| 531 | * |
||
| 532 | * The association data is saved in the database in the following format: |
||
| 533 | * array ( |
||
| 534 | * 0 => 'post:page:4', |
||
| 535 | * 1 => 'term:category:2', |
||
| 536 | * 2 => 'user:user:1', |
||
| 537 | * ) |
||
| 538 | * where the value of each array item contains: |
||
| 539 | * - Type of data (post, term, user or comment) |
||
| 540 | * - Subtype of data (the particular post type or taxonomy) |
||
| 541 | * - ID of the item (the database ID of the item) |
||
| 542 | */ |
||
| 543 | protected function value_to_json() { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Convert the field data into JSON representation. |
||
| 562 | * @param bool $load Whether to load data from the datastore. |
||
| 563 | * @return mixed The JSON field data. |
||
| 564 | */ |
||
| 565 | public function to_json( $load ) { |
||
| 578 | } |
||
| 579 |