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 | * Max number of selected items allowed. -1 for no limit |
||
| 26 | * |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | protected $max = -1; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Allow items to be added multiple times |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $duplicates_allowed = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Default field value |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $default_value = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Types of entries to associate with. |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $types = array( |
||
| 50 | array( |
||
| 51 | 'type' => 'post', |
||
| 52 | 'post_type' => 'post', |
||
| 53 | ), |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create a field from a certain type with the specified label. |
||
| 58 | * |
||
| 59 | * @param string $type Field type |
||
| 60 | * @param string $name Field name |
||
| 61 | * @param string $label Field label |
||
| 62 | */ |
||
| 63 | public function __construct( $type, $name, $label ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Load the field value from an input array based on it's name |
||
| 71 | * |
||
| 72 | * @param array $input Array of field names and values. |
||
| 73 | */ |
||
| 74 | public function set_value_from_input( $input ) { |
||
| 75 | $value = array(); |
||
| 76 | View Code Duplication | if ( isset( $input[ $this->get_name() ] ) ) { |
|
| 77 | $value = stripslashes_deep( $input[ $this->get_name() ] ); |
||
| 78 | if ( is_array( $value ) ) { |
||
| 79 | $value = array_values( $value ); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | $this->set_value( $value ); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Alias for $this->get_value_set()->set( $value ); |
||
| 87 | */ |
||
| 88 | public function set_value( $value ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get value string for legacy value |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | protected function get_value_string_for_legacy_value( $legacy_value ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Convert a colo:separated:string into it's expected components |
||
| 115 | * Used for backwards compatibility to CF 1.5 |
||
| 116 | * |
||
| 117 | * @param string $value_string |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | protected function value_string_to_property_array( $value_string ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Convert a colon:separated:string into it's expected components |
||
| 142 | * Used for backwards compatibility to CF 1.5 |
||
| 143 | * |
||
| 144 | * @param array $value_string_array |
||
| 145 | * @return array<array> |
||
| 146 | */ |
||
| 147 | protected function value_string_array_to_value_set( $value_string_array ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Used to get the title of an item. |
||
| 173 | * |
||
| 174 | * Can be overriden or extended by the `carbon_association_title` filter. |
||
| 175 | * |
||
| 176 | * @param int $id The database ID of the item. |
||
| 177 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 178 | * @param string $subtype The subtype - "page", "post", "category", etc. |
||
| 179 | * @return string $title The title of the item. |
||
| 180 | */ |
||
| 181 | protected function get_title_by_type( $id, $type, $subtype = '' ) { |
||
| 182 | $title = ''; |
||
| 183 | |||
| 184 | $method = 'get_' . $type . '_title'; |
||
| 185 | $callable = array( $this->wp_toolset, $method ); |
||
| 186 | if ( is_callable( $callable ) ) { |
||
| 187 | $title = call_user_func( $callable, $id, $subtype ); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ( $type === 'comment' ) { |
||
| 191 | $max = apply_filters( 'carbon_fields_association_field_comment_length', 30, $this->get_base_name() ); |
||
| 192 | if ( strlen( $title ) > $max ) { |
||
| 193 | $title = substr( $title, 0, $max ) . '...'; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Filter the title of the association item. |
||
| 199 | * |
||
| 200 | * @param string $title The unfiltered item title. |
||
| 201 | * @param string $name Name of the association field. |
||
| 202 | * @param int $id The database ID of the item. |
||
| 203 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 204 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
| 205 | */ |
||
| 206 | $title = apply_filters( 'carbon_fields_association_field_title', $title, $this->get_base_name(), $id, $type, $subtype ); |
||
| 207 | |||
| 208 | if ( ! $title ) { |
||
| 209 | $title = '(no title) - ID: ' . $id; |
||
| 210 | } |
||
| 211 | |||
| 212 | return $title; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Used to get the label of an item. |
||
| 217 | * |
||
| 218 | * Can be overriden or extended by the `carbon_association_item_label` filter. |
||
| 219 | * |
||
| 220 | * @param int $id The database ID of the item. |
||
| 221 | * @param string $type Item type (post, term, user, comment, or a custom one). |
||
| 222 | * @param string $subtype Subtype - "page", "post", "category", etc. |
||
| 223 | * @return string $label The label of the item. |
||
| 224 | */ |
||
| 225 | protected function get_item_label( $id, $type, $subtype = '' ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get post options |
||
| 250 | * |
||
| 251 | * @return array $options |
||
| 252 | */ |
||
| 253 | protected function get_post_options( $type ) { |
||
| 254 | /** |
||
| 255 | * Filter the default query when fetching posts for a particular field. |
||
| 256 | * |
||
| 257 | * @param array $args The parameters, passed to get_posts(). |
||
| 258 | */ |
||
| 259 | $filter_name = 'carbon_fields_association_field_options_' . $this->get_base_name() . '_' . $type['type'] . '_' . $type['post_type']; |
||
| 260 | $args = apply_filters( $filter_name, array( |
||
| 261 | 'post_type' => $type['post_type'], |
||
| 262 | 'posts_per_page' => -1, |
||
| 263 | 'fields' => 'ids', |
||
| 264 | 'suppress_filters' => false, |
||
| 265 | ) ); |
||
| 266 | |||
| 267 | // fetch and prepare posts as association items |
||
| 268 | $posts = get_posts( $args ); |
||
| 269 | foreach ( $posts as &$p ) { |
||
| 270 | $p = array( |
||
| 271 | 'id' => intval( $p ), |
||
| 272 | 'title' => $this->get_title_by_type( $p, $type['type'], $type['post_type'] ), |
||
| 273 | 'type' => $type['type'], |
||
| 274 | 'subtype' => $type['post_type'], |
||
| 275 | 'label' => $this->get_item_label( $p, $type['type'], $type['post_type'] ), |
||
| 276 | 'is_trashed' => ( get_post_status( $p ) == 'trash' ), |
||
| 277 | 'edit_link' => $this->get_object_edit_link( $type, $p ), |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | return $posts; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get term options |
||
| 285 | * |
||
| 286 | * @return array $options |
||
| 287 | */ |
||
| 288 | protected function get_term_options( $type ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get user options |
||
| 318 | * |
||
| 319 | * @return array $options |
||
| 320 | */ |
||
| 321 | View Code Duplication | protected function get_user_options( $type ) { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get comment options |
||
| 350 | * |
||
| 351 | * @return array $options |
||
| 352 | */ |
||
| 353 | View Code Duplication | protected function get_comment_options( $type ) { |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Generate the item options. |
||
| 382 | * |
||
| 383 | * @return array $options The selectable options of the association field. |
||
| 384 | */ |
||
| 385 | public function get_options() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Retrieve the edit link of a particular object. |
||
| 409 | * |
||
| 410 | * @param string $type Object type. |
||
| 411 | * @param int $id ID of the object. |
||
| 412 | * @return string URL of the edit link. |
||
| 413 | */ |
||
| 414 | protected function get_object_edit_link( $type, $id ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Modify the types. |
||
| 443 | * @param array $types New types |
||
| 444 | */ |
||
| 445 | public function set_types( $types ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Set the maximum allowed number of selected entries. |
||
| 452 | * |
||
| 453 | * @param int $max |
||
| 454 | */ |
||
| 455 | public function set_max( $max ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get whether entry duplicates are allowed. |
||
| 462 | * |
||
| 463 | * @return boolean |
||
| 464 | */ |
||
| 465 | public function get_duplicates_allowed() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Set whether entry duplicates are allowed. |
||
| 471 | * |
||
| 472 | * @param boolean $allowed |
||
| 473 | */ |
||
| 474 | public function set_duplicates_allowed( $allowed ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Specify whether to allow each entry to be selected multiple times. |
||
| 481 | * Backwards-compatibility alias. |
||
| 482 | * |
||
| 483 | * @param boolean $allow |
||
| 484 | */ |
||
| 485 | public function allow_duplicates( $allow = true ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Converts the field values into a usable associative array. |
||
| 491 | * |
||
| 492 | * The association data is saved in the database in the following format: |
||
| 493 | * array ( |
||
| 494 | * 0 => 'post:page:4', |
||
| 495 | * 1 => 'term:category:2', |
||
| 496 | * 2 => 'user:user:1', |
||
| 497 | * ) |
||
| 498 | * where the value of each array item contains: |
||
| 499 | * - Type of data (post, term, user or comment) |
||
| 500 | * - Subtype of data (the particular post type or taxonomy) |
||
| 501 | * - ID of the item (the database ID of the item) |
||
| 502 | */ |
||
| 503 | protected function value_to_json() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Convert the field data into JSON representation. |
||
| 522 | * @param bool $load Whether to load data from the datastore. |
||
| 523 | * @return mixed The JSON field data. |
||
| 524 | */ |
||
| 525 | public function to_json( $load ) { |
||
| 537 | } |
||
| 538 |
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.