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 |
||
| 16 | class Association_Field extends Relationship_Field { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * WP_Toolset instance |
||
| 20 | * @var Carbon_Fields\Toolset\WP_Toolset |
||
| 21 | */ |
||
| 22 | protected $wp_toolset; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Types of entries to associate with. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $types = array( |
||
| 29 | array( |
||
| 30 | 'type' => 'post', |
||
| 31 | 'post_type' => 'post', |
||
| 32 | ), |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Create a field from a certain type with the specified label. |
||
| 37 | * |
||
| 38 | * @param string $type Field type |
||
| 39 | * @param string $name Field name |
||
| 40 | * @param string $label Field label |
||
| 41 | */ |
||
| 42 | protected function __construct( $type, $name, $label ) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Alias for $this->get_value_set()->set( $value ); |
||
| 50 | **/ |
||
| 51 | public function set_value( $value ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Convert a colo:separated:string into it's expected components |
||
| 58 | * Used for backwards compatibility to CF 1.5 |
||
| 59 | * |
||
| 60 | * @param string $value_string |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | protected function value_string_to_property_array( $value_string ) { |
||
| 64 | $value_pieces = explode( ':', $value_string ); |
||
| 65 | $type = isset( $value_pieces[0] ) ? $value_pieces[0] : 'post'; |
||
| 66 | $subtype = isset( $value_pieces[1] ) ? $value_pieces[1] : 'post'; |
||
| 67 | $object_id = isset( $value_pieces[2] ) ? $value_pieces[2] : 0; |
||
| 68 | |||
| 69 | $property_array = array( |
||
| 70 | Value_Set::VALUE_PROPERTY => $value_string, |
||
| 71 | 'type' => $type, |
||
| 72 | 'subtype' => $subtype, |
||
| 73 | 'object_id' => intval( $object_id ), |
||
| 74 | ); |
||
| 75 | return $property_array; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Convert a colon:separated:string into it's expected components |
||
| 80 | * Used for backwards compatibility to CF 1.5 |
||
| 81 | * |
||
| 82 | * @param array $value_string_array |
||
| 83 | * @return array<array> |
||
| 84 | */ |
||
| 85 | protected function value_string_array_to_value_set( $value_string_array ) { |
||
| 86 | $value_set = array(); |
||
| 87 | foreach ( $value_string_array as $raw_value_entry ) { |
||
| 88 | $value_string = $raw_value_entry; |
||
| 89 | |||
| 90 | if ( is_array( $raw_value_entry ) ) { |
||
| 91 | if ( isset( $raw_value_entry['type'] ) ) { |
||
| 92 | // array is already in suitable format |
||
| 93 | $value_set[] = $raw_value_entry; |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | $value_string = $raw_value_entry[ Value_Set::VALUE_PROPERTY ]; |
||
| 97 | } else if ( empty( trim( $value_string ) ) ) { |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | |||
| 101 | $property_array = $this->value_string_to_property_array( $value_string ); |
||
| 102 | $value_set[] = $property_array; |
||
| 103 | } |
||
| 104 | return $value_set; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Used to get the title of an item. |
||
| 109 | * |
||
| 110 | * Can be overriden or extended by the `carbon_association_title` filter. |
||
| 111 | * |
||
| 112 | * @param int $id The database ID of the item. |
||
| 113 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 114 | * @param string $subtype The subtype - "page", "post", "category", etc. |
||
| 115 | * @return string $title The title of the item. |
||
| 116 | */ |
||
| 117 | protected function get_title_by_type( $id, $type, $subtype = '' ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Used to get the label of an item. |
||
| 153 | * |
||
| 154 | * Can be overriden or extended by the `carbon_association_item_label` filter. |
||
| 155 | * |
||
| 156 | * @param int $id The database ID of the item. |
||
| 157 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 158 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
| 159 | * @return string $label The label of the item. |
||
| 160 | */ |
||
| 161 | protected function get_item_label( $id, $type, $subtype = '' ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get post options |
||
| 186 | * |
||
| 187 | * @return array $options |
||
| 188 | */ |
||
| 189 | protected function get_post_options( $type ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get term options |
||
| 221 | * |
||
| 222 | * @return array $options |
||
| 223 | */ |
||
| 224 | protected function get_term_options( $type ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get user options |
||
| 254 | * |
||
| 255 | * @return array $options |
||
| 256 | */ |
||
| 257 | View Code Duplication | protected function get_user_options( $type ) { |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Get comment options |
||
| 286 | * |
||
| 287 | * @return array $options |
||
| 288 | */ |
||
| 289 | View Code Duplication | protected function get_comment_options( $type ) { |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Generate the item options. |
||
| 318 | * |
||
| 319 | * @return array $options The selectable options of the association field. |
||
| 320 | */ |
||
| 321 | public function get_options() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Retrieve the edit link of a particular object. |
||
| 345 | * |
||
| 346 | * @param string $type Object type. |
||
| 347 | * @param int $id ID of the object. |
||
| 348 | * @return string URL of the edit link. |
||
| 349 | */ |
||
| 350 | protected function get_object_edit_link( $type, $id ) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Modify the types. |
||
| 379 | * @param array $types New types |
||
| 380 | */ |
||
| 381 | public function set_types( $types ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Converts the field values into a usable associative array. |
||
| 388 | * |
||
| 389 | * The association data is saved in the database in the following format: |
||
| 390 | * array ( |
||
| 391 | * 0 => 'post:page:4', |
||
| 392 | * 1 => 'term:category:2', |
||
| 393 | * 2 => 'user:user:1', |
||
| 394 | * ) |
||
| 395 | * where the value of each array item contains: |
||
| 396 | * - Type of data (post, term, user or comment) |
||
| 397 | * - Subtype of data (the particular post type or taxonomy) |
||
| 398 | * - ID of the item (the database ID of the item) |
||
| 399 | */ |
||
| 400 | protected function value_to_json() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Convert the field data into JSON representation. |
||
| 419 | * @param bool $load Whether to load data from the datastore. |
||
| 420 | * @return mixed The JSON field data. |
||
| 421 | */ |
||
| 422 | public function to_json( $load ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Serves as a backbone template for the association items. |
||
| 444 | * Used for both the selected and the selectable options. |
||
| 445 | * |
||
| 446 | * @param bool $display_input Whether to display the selected item input field. |
||
| 447 | */ |
||
| 448 | public function item_template( $display_input = true ) { |
||
| 469 | } |
||
| 470 |