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 |
||
| 12 | class Helper { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Get a field from a specific container type or id |
||
| 16 | * |
||
| 17 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
| 18 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
| 19 | * @param string $field_name Field name to search for |
||
| 20 | * @return boolean |
||
| 21 | */ |
||
| 22 | public static function get_field( $container_type, $container_id, $field_name ) { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Get a clone of a field with a value loaded |
||
| 32 | * |
||
| 33 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 34 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
| 35 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
| 36 | * @param string $field_name Field name to search for |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get a value formatted for end-users |
||
| 55 | * |
||
| 56 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 57 | * @param string $container_type Container type to search in |
||
| 58 | * @param string $container_id |
||
| 59 | * @param string $field_name Field name |
||
| 60 | * @return mixed |
||
| 61 | */ |
||
| 62 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set value for a field |
||
| 75 | * |
||
| 76 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 77 | * @param string $container_type Container type to search in |
||
| 78 | * @param string $container_id |
||
| 79 | * @param string $field_name Field name |
||
| 80 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 81 | */ |
||
| 82 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Shorthand for get_post_meta(). |
||
| 96 | * Uses the ID of the current post in the loop. |
||
| 97 | * |
||
| 98 | * @param string $name Custom field name. |
||
| 99 | * @param string $container_id |
||
| 100 | * @return mixed Meta value. |
||
| 101 | */ |
||
| 102 | public static function get_the_post_meta( $name, $container_id = '' ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get post meta field for a post. |
||
| 108 | * |
||
| 109 | * @param int $id Post ID. |
||
| 110 | * @param string $name Custom field name. |
||
| 111 | * @param string $container_id |
||
| 112 | * @return mixed Meta value. |
||
| 113 | */ |
||
| 114 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set post meta field for a post. |
||
| 120 | * |
||
| 121 | * @param int $id Post ID |
||
| 122 | * @param string $name Custom field name |
||
| 123 | * @param array $value |
||
| 124 | * @param string $container_id |
||
| 125 | */ |
||
| 126 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get theme option field value. |
||
| 132 | * |
||
| 133 | * @param string $name Custom field name |
||
| 134 | * @param string $container_id |
||
| 135 | * @return mixed Option value |
||
| 136 | */ |
||
| 137 | public static function get_theme_option( $name, $container_id = '' ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set theme option field value. |
||
| 143 | * |
||
| 144 | * @param string $name Field name |
||
| 145 | * @param array $value |
||
| 146 | * @param string $container_id |
||
| 147 | */ |
||
| 148 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get term meta field for a term. |
||
| 154 | * |
||
| 155 | * @param int $id Term ID. |
||
| 156 | * @param string $name Custom field name. |
||
| 157 | * @param string $container_id |
||
| 158 | * @return mixed Meta value. |
||
| 159 | */ |
||
| 160 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Set term meta field for a term. |
||
| 166 | * |
||
| 167 | * @param int $id Term ID |
||
| 168 | * @param string $name Field name |
||
| 169 | * @param array $value |
||
| 170 | * @param string $container_id |
||
| 171 | */ |
||
| 172 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get user meta field for a user. |
||
| 178 | * |
||
| 179 | * @param int $id User ID. |
||
| 180 | * @param string $name Custom field name. |
||
| 181 | * @param string $container_id |
||
| 182 | * @return mixed Meta value. |
||
| 183 | */ |
||
| 184 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set user meta field for a user. |
||
| 190 | * |
||
| 191 | * @param int $id User ID |
||
| 192 | * @param string $name Field name |
||
| 193 | * @param array $value |
||
| 194 | * @param string $container_id |
||
| 195 | */ |
||
| 196 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get comment meta field for a comment. |
||
| 202 | * |
||
| 203 | * @param int $id Comment ID. |
||
| 204 | * @param string $name Custom field name. |
||
| 205 | * @param string $container_id |
||
| 206 | * @return mixed Meta value. |
||
| 207 | */ |
||
| 208 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set comment meta field for a comment. |
||
| 214 | * |
||
| 215 | * @param int $id Comment ID |
||
| 216 | * @param string $name Field name |
||
| 217 | * @param array $value |
||
| 218 | * @param string $container_id |
||
| 219 | */ |
||
| 220 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get nav menu item meta field for a nav menu item. |
||
| 226 | * |
||
| 227 | * @param int $id Nav menu item ID. |
||
| 228 | * @param string $name Custom field name. |
||
| 229 | * @param string $container_id |
||
| 230 | * @return mixed Meta value. |
||
| 231 | */ |
||
| 232 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set nav menu item meta field for a nav menu item. |
||
| 238 | * |
||
| 239 | * @param int $id Nav menu item ID |
||
| 240 | * @param string $name Field name |
||
| 241 | * @param array $value |
||
| 242 | * @param string $container_id |
||
| 243 | */ |
||
| 244 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Recursive sorting function by array key. |
||
| 250 | * |
||
| 251 | * @param array &$array The input array. |
||
| 252 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 253 | * @return boolean |
||
| 254 | */ |
||
| 255 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 268 | * |
||
| 269 | * @param array $array |
||
| 270 | * @param array<string> $allowed_relations |
||
| 271 | * @param string $relation_key |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Normalize a label by updating case, stripping common prefixes etc. |
||
| 293 | * |
||
| 294 | * @param string $label |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | View Code Duplication | public static function normalize_label( $label ) { |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Normalize a type string representing an object type |
||
| 312 | * |
||
| 313 | * @param string $type |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | View Code Duplication | public static function normalize_type( $type ) { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Convert a string representing an object type to a fully qualified class name |
||
| 326 | * |
||
| 327 | * @param string $type |
||
| 328 | * @param string $namespace |
||
| 329 | * @param string $class_suffix |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Convert a string representing an object type to a fully qualified class name |
||
| 348 | * |
||
| 349 | * @param string $class |
||
| 350 | * @param string $class_suffix |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get an array of sanitized html classes |
||
| 368 | * |
||
| 369 | * @param string|array<string> $classes |
||
| 370 | * @return array<string> |
||
| 371 | */ |
||
| 372 | public static function sanitize_classes( $classes ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Check if an id or name for containers and fields is valid |
||
| 382 | * |
||
| 383 | * @param string $id |
||
| 384 | * @return boolean |
||
| 385 | */ |
||
| 386 | public static function is_valid_entity_id( $id ) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Return a partial regex pettern matching allowed field name characters |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public static function get_field_name_characters_pattern() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get an attachment ID given a file URL |
||
| 401 | * Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
||
| 402 | * |
||
| 403 | * @param string $url |
||
| 404 | * @return integet |
||
| 405 | */ |
||
| 406 | function get_attachment_id( $url ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns attachment metadata from an ID. |
||
| 445 | * |
||
| 446 | * @param string $id |
||
| 447 | * @param string $type Value Type. Can be either id or url |
||
| 448 | * @return boolean |
||
| 449 | */ |
||
| 450 | public static function get_attachment_metadata( $id, $type ) { |
||
| 531 | } |
||
| 532 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.