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 Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Helper { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Get a field from a specific container type or id |
||
| 15 | * |
||
| 16 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
| 17 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
| 18 | * @param string $field_name Field name to search for |
||
| 19 | * @return boolean |
||
| 20 | */ |
||
| 21 | public static function get_field( $container_type, $container_id, $field_name ) { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get a clone of a field with a value loaded |
||
| 31 | * |
||
| 32 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 33 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
| 34 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
| 35 | * @param string $field_name Field name to search for |
||
| 36 | * @return mixed |
||
| 37 | */ |
||
| 38 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get a value formatted for end-users |
||
| 54 | * |
||
| 55 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 56 | * @param string $container_type Container type to search in |
||
| 57 | * @param string $container_id |
||
| 58 | * @param string $field_name Field name |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set value for a field |
||
| 74 | * |
||
| 75 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 76 | * @param string $container_type Container type to search in |
||
| 77 | * @param string $container_id |
||
| 78 | * @param string $field_name Field name |
||
| 79 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 80 | */ |
||
| 81 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Shorthand for get_post_meta(). |
||
| 95 | * Uses the ID of the current post in the loop. |
||
| 96 | * |
||
| 97 | * @param string $name Custom field name. |
||
| 98 | * @param string $container_id |
||
| 99 | * @return mixed Meta value. |
||
| 100 | */ |
||
| 101 | public static function get_the_post_meta( $name, $container_id = '' ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get post meta field for a post. |
||
| 107 | * |
||
| 108 | * @param int $id Post ID. |
||
| 109 | * @param string $name Custom field name. |
||
| 110 | * @param string $container_id |
||
| 111 | * @return mixed Meta value. |
||
| 112 | */ |
||
| 113 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set post meta field for a post. |
||
| 119 | * |
||
| 120 | * @param int $id Post ID |
||
| 121 | * @param string $name Custom field name |
||
| 122 | * @param array $value |
||
| 123 | * @param string $container_id |
||
| 124 | */ |
||
| 125 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get theme option field value. |
||
| 131 | * |
||
| 132 | * @param string $name Custom field name |
||
| 133 | * @param string $container_id |
||
| 134 | * @return mixed Option value |
||
| 135 | */ |
||
| 136 | public static function get_theme_option( $name, $container_id = '' ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set theme option field value. |
||
| 142 | * |
||
| 143 | * @param string $name Field name |
||
| 144 | * @param array $value |
||
| 145 | * @param string $container_id |
||
| 146 | */ |
||
| 147 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get term meta field for a term. |
||
| 153 | * |
||
| 154 | * @param int $id Term ID. |
||
| 155 | * @param string $name Custom field name. |
||
| 156 | * @param string $container_id |
||
| 157 | * @return mixed Meta value. |
||
| 158 | */ |
||
| 159 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set term meta field for a term. |
||
| 165 | * |
||
| 166 | * @param int $id Term ID |
||
| 167 | * @param string $name Field name |
||
| 168 | * @param array $value |
||
| 169 | * @param string $container_id |
||
| 170 | */ |
||
| 171 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get user meta field for a user. |
||
| 177 | * |
||
| 178 | * @param int $id User ID. |
||
| 179 | * @param string $name Custom field name. |
||
| 180 | * @param string $container_id |
||
| 181 | * @return mixed Meta value. |
||
| 182 | */ |
||
| 183 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set user meta field for a user. |
||
| 189 | * |
||
| 190 | * @param int $id User ID |
||
| 191 | * @param string $name Field name |
||
| 192 | * @param array $value |
||
| 193 | * @param string $container_id |
||
| 194 | */ |
||
| 195 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get comment meta field for a comment. |
||
| 201 | * |
||
| 202 | * @param int $id Comment ID. |
||
| 203 | * @param string $name Custom field name. |
||
| 204 | * @param string $container_id |
||
| 205 | * @return mixed Meta value. |
||
| 206 | */ |
||
| 207 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set comment meta field for a comment. |
||
| 213 | * |
||
| 214 | * @param int $id Comment ID |
||
| 215 | * @param string $name Field name |
||
| 216 | * @param array $value |
||
| 217 | * @param string $container_id |
||
| 218 | */ |
||
| 219 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get nav menu item meta field for a nav menu item. |
||
| 225 | * |
||
| 226 | * @param int $id Nav menu item ID. |
||
| 227 | * @param string $name Custom field name. |
||
| 228 | * @param string $container_id |
||
| 229 | * @return mixed Meta value. |
||
| 230 | */ |
||
| 231 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set nav menu item meta field for a nav menu item. |
||
| 237 | * |
||
| 238 | * @param int $id Nav menu item ID |
||
| 239 | * @param string $name Field name |
||
| 240 | * @param array $value |
||
| 241 | * @param string $container_id |
||
| 242 | */ |
||
| 243 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Recursive sorting function by array key. |
||
| 249 | * |
||
| 250 | * @param array &$array The input array. |
||
| 251 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 267 | * |
||
| 268 | * @param array $array |
||
| 269 | * @param array<string> $allowed_relations |
||
| 270 | * @param string $relation_key |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Normalize a label by updating case, stripping common prefixes etc. |
||
| 292 | * |
||
| 293 | * @param string $label |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function normalize_label( $label ) { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Normalize a type string representing an object type |
||
| 311 | * |
||
| 312 | * @param string $type |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | View Code Duplication | public static function normalize_type( $type ) { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Convert a string representing an object type to a fully qualified class name |
||
| 325 | * |
||
| 326 | * @param string $type |
||
| 327 | * @param string $namespace |
||
| 328 | * @param string $class_suffix |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Convert a string representing an object type to a fully qualified class name |
||
| 347 | * |
||
| 348 | * @param string $class |
||
| 349 | * @param string $class_suffix |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get an array of sanitized html classes |
||
| 367 | * |
||
| 368 | * @param string|array<string> $classes |
||
| 369 | * @return array<string> |
||
| 370 | */ |
||
| 371 | public static function sanitize_classes( $classes ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Check if an id or name for containers and fields is valid |
||
| 381 | * |
||
| 382 | * @param string $id |
||
| 383 | * @return boolean |
||
| 384 | */ |
||
| 385 | public static function is_valid_entity_id( $id ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return a partial regex pettern matching allowed field name characters |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public static function get_field_name_characters_pattern() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns attachment metadata from an ID. |
||
| 400 | * |
||
| 401 | * @param string $id |
||
| 402 | * @param string $type Value Type. Can be either id or url |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public static function get_attachment_metadata( $id, $type ) { |
||
| 471 | } |
||
| 472 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.