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 Field { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * WP_Toolset instance for WP data loading |
||
| 20 | * |
||
| 21 | * @var Carbon_Fields\Toolset\WP_Toolset |
||
| 22 | */ |
||
| 23 | protected $wp_toolset; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Max number of selected items allowed. -1 for no limit |
||
| 27 | * |
||
| 28 | * @var integer |
||
| 29 | */ |
||
| 30 | protected $max = -1; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Allow items to be added multiple times |
||
| 34 | * |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | protected $allow_duplicates = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Default field value |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $default_value = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Types of entries to associate with. |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $types = array( |
||
| 51 | array( |
||
| 52 | 'type' => 'post', |
||
| 53 | 'post_type' => 'post', |
||
| 54 | ), |
||
| 55 | ); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a field from a certain type with the specified label. |
||
| 59 | * |
||
| 60 | * @param string $type Field type |
||
| 61 | * @param string $name Field name |
||
| 62 | * @param string $label Field label |
||
| 63 | */ |
||
| 64 | protected function __construct( $type, $name, $label ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Load the field value from an input array based on it's name |
||
| 72 | * |
||
| 73 | * @param array $input Array of field names and values. |
||
| 74 | **/ |
||
| 75 | public function set_value_from_input( $input ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Alias for $this->get_value_set()->set( $value ); |
||
| 88 | */ |
||
| 89 | public function set_value( $value ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get value string for legacy value |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | protected function get_value_string_for_legacy_value( $legacy_value ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Convert a colo:separated:string into it's expected components |
||
| 116 | * Used for backwards compatibility to CF 1.5 |
||
| 117 | * |
||
| 118 | * @param string $value_string |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | protected function value_string_to_property_array( $value_string ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Convert a colon:separated:string into it's expected components |
||
| 143 | * Used for backwards compatibility to CF 1.5 |
||
| 144 | * |
||
| 145 | * @param array $value_string_array |
||
| 146 | * @return array<array> |
||
| 147 | */ |
||
| 148 | protected function value_string_array_to_value_set( $value_string_array ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Used to get the title of an item. |
||
| 174 | * |
||
| 175 | * Can be overriden or extended by the `carbon_association_title` filter. |
||
| 176 | * |
||
| 177 | * @param int $id The database ID of the item. |
||
| 178 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 179 | * @param string $subtype The subtype - "page", "post", "category", etc. |
||
| 180 | * @return string $title The title of the item. |
||
| 181 | */ |
||
| 182 | protected function get_title_by_type( $id, $type, $subtype = '' ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Used to get the label of an item. |
||
| 218 | * |
||
| 219 | * Can be overriden or extended by the `carbon_association_item_label` filter. |
||
| 220 | * |
||
| 221 | * @param int $id The database ID of the item. |
||
| 222 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 223 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
| 224 | * @return string $label The label of the item. |
||
| 225 | */ |
||
| 226 | protected function get_item_label( $id, $type, $subtype = '' ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get post options |
||
| 251 | * |
||
| 252 | * @return array $options |
||
| 253 | */ |
||
| 254 | protected function get_post_options( $type ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get term options |
||
| 286 | * |
||
| 287 | * @return array $options |
||
| 288 | */ |
||
| 289 | protected function get_term_options( $type ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get user options |
||
| 319 | * |
||
| 320 | * @return array $options |
||
| 321 | */ |
||
| 322 | View Code Duplication | protected function get_user_options( $type ) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Get comment options |
||
| 351 | * |
||
| 352 | * @return array $options |
||
| 353 | */ |
||
| 354 | View Code Duplication | protected function get_comment_options( $type ) { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Generate the item options. |
||
| 383 | * |
||
| 384 | * @return array $options The selectable options of the association field. |
||
| 385 | */ |
||
| 386 | public function get_options() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Retrieve the edit link of a particular object. |
||
| 410 | * |
||
| 411 | * @param string $type Object type. |
||
| 412 | * @param int $id ID of the object. |
||
| 413 | * @return string URL of the edit link. |
||
| 414 | */ |
||
| 415 | protected function get_object_edit_link( $type, $id ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Modify the types. |
||
| 444 | * @param array $types New types |
||
| 445 | */ |
||
| 446 | public function set_types( $types ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Set the maximum allowed number of selected entries. |
||
| 453 | * |
||
| 454 | * @param int $max |
||
| 455 | */ |
||
| 456 | public function set_max( $max ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Specify whether to allow each entry to be selected multiple times. |
||
| 463 | * |
||
| 464 | * @param boolean $allow |
||
| 465 | */ |
||
| 466 | public function allow_duplicates( $allow = true ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Converts the field values into a usable associative array. |
||
| 473 | * |
||
| 474 | * The association data is saved in the database in the following format: |
||
| 475 | * array ( |
||
| 476 | * 0 => 'post:page:4', |
||
| 477 | * 1 => 'term:category:2', |
||
| 478 | * 2 => 'user:user:1', |
||
| 479 | * ) |
||
| 480 | * where the value of each array item contains: |
||
| 481 | * - Type of data (post, term, user or comment) |
||
| 482 | * - Subtype of data (the particular post type or taxonomy) |
||
| 483 | * - ID of the item (the database ID of the item) |
||
| 484 | */ |
||
| 485 | protected function value_to_json() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Convert the field data into JSON representation. |
||
| 504 | * @param bool $load Whether to load data from the datastore. |
||
| 505 | * @return mixed The JSON field data. |
||
| 506 | */ |
||
| 507 | public function to_json( $load ) { |
||
| 526 | } |
||
| 527 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.