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 ) { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get a clone of a field with a value loaded |
||
| 34 | * |
||
| 35 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 36 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
| 37 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
| 38 | * @param string $field_name Field name to search for |
||
| 39 | * @return mixed |
||
| 40 | */ |
||
| 41 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get a value formatted for end-users |
||
| 57 | * |
||
| 58 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 59 | * @param string $container_type Container type to search in |
||
| 60 | * @param string $container_id |
||
| 61 | * @param string $field_name Field name |
||
| 62 | * @return mixed |
||
| 63 | */ |
||
| 64 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Set value for a field |
||
| 77 | * |
||
| 78 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 79 | * @param string $container_type Container type to search in |
||
| 80 | * @param string $container_id |
||
| 81 | * @param string $field_name Field name |
||
| 82 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 83 | */ |
||
| 84 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Shorthand for get_post_meta(). |
||
| 99 | * Uses the ID of the current post in the loop. |
||
| 100 | * |
||
| 101 | * @param string $name Field name |
||
| 102 | * @param string $container_id |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public static function get_the_post_meta( $name, $container_id = '' ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get post meta field for a post. |
||
| 111 | * |
||
| 112 | * @param int $id Post ID |
||
| 113 | * @param string $name Field name |
||
| 114 | * @param string $container_id |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set post meta field for a post. |
||
| 123 | * |
||
| 124 | * @param int $id Post ID |
||
| 125 | * @param string $name Field name |
||
| 126 | * @param array $value |
||
| 127 | * @param string $container_id |
||
| 128 | */ |
||
| 129 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get theme option field value. |
||
| 135 | * |
||
| 136 | * @param string $name Field name |
||
| 137 | * @param string $container_id |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | public static function get_theme_option( $name, $container_id = '' ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set theme option field value. |
||
| 146 | * |
||
| 147 | * @param string $name Field name |
||
| 148 | * @param array $value |
||
| 149 | * @param string $container_id |
||
| 150 | */ |
||
| 151 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get network option field value for the main site. |
||
| 157 | * |
||
| 158 | * @param string $name Field name |
||
| 159 | * @param string $container_id |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public static function get_the_network_option( $name, $container_id = '' ) { |
||
| 163 | $id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1; |
||
| 164 | return static::get_network_option( $id, $name, $container_id ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get network option field value for a site. |
||
| 169 | * |
||
| 170 | * @param string $id Site ID |
||
| 171 | * @param string $name Field name |
||
| 172 | * @param string $container_id |
||
| 173 | * @return mixed |
||
| 174 | */ |
||
| 175 | public static function get_network_option( $id, $name, $container_id = '' ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Set network option field value for a site. |
||
| 181 | * |
||
| 182 | * @param string $id Site ID |
||
| 183 | * @param string $name Field name |
||
| 184 | * @param string $container_id |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public static function set_network_option( $id, $name, $value, $container_id = '' ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get term meta field for a term. |
||
| 193 | * |
||
| 194 | * @param int $id Term ID |
||
| 195 | * @param string $name Field name |
||
| 196 | * @param string $container_id |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set term meta field for a term. |
||
| 205 | * |
||
| 206 | * @param int $id Term ID |
||
| 207 | * @param string $name Field name |
||
| 208 | * @param array $value |
||
| 209 | * @param string $container_id |
||
| 210 | */ |
||
| 211 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get user meta field for a user. |
||
| 217 | * |
||
| 218 | * @param int $id User ID |
||
| 219 | * @param string $name Field name |
||
| 220 | * @param string $container_id |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set user meta field for a user. |
||
| 229 | * |
||
| 230 | * @param int $id User ID |
||
| 231 | * @param string $name Field name |
||
| 232 | * @param array $value |
||
| 233 | * @param string $container_id |
||
| 234 | */ |
||
| 235 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get comment meta field for a comment. |
||
| 241 | * |
||
| 242 | * @param int $id Comment ID |
||
| 243 | * @param string $name Field name |
||
| 244 | * @param string $container_id |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set comment meta field for a comment. |
||
| 253 | * |
||
| 254 | * @param int $id Comment ID |
||
| 255 | * @param string $name Field name |
||
| 256 | * @param array $value |
||
| 257 | * @param string $container_id |
||
| 258 | */ |
||
| 259 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get nav menu item meta field for a nav menu item. |
||
| 265 | * |
||
| 266 | * @param int $id Nav menu item ID |
||
| 267 | * @param string $name Field name |
||
| 268 | * @param string $container_id |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
| 272 | return static::get_value( $id, 'nav_menu_item', $container_id, $name ); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set nav menu item meta field for a nav menu item. |
||
| 277 | * |
||
| 278 | * @param int $id Nav menu item ID |
||
| 279 | * @param string $name Field name |
||
| 280 | * @param array $value |
||
| 281 | * @param string $container_id |
||
| 282 | */ |
||
| 283 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Recursive sorting function by array key. |
||
| 289 | * |
||
| 290 | * @param array &$array The input array. |
||
| 291 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 292 | * @return boolean |
||
| 293 | */ |
||
| 294 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 295 | if ( ! is_array( $array ) ) { |
||
| 296 | return false; |
||
| 297 | } |
||
| 298 | ksort( $array, $sort_flags ); |
||
| 299 | foreach ( $array as $key => $value ) { |
||
| 300 | self::ksort_recursive( $array[ $key ], $sort_flags ); |
||
| 301 | } |
||
| 302 | return true; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 307 | * |
||
| 308 | * @param array $array |
||
| 309 | * @param array<string> $allowed_relations |
||
| 310 | * @param string $relation_key |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 314 | $allowed_relations = array_values( $allowed_relations ); |
||
| 315 | $allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
||
| 316 | $relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
||
| 317 | |||
| 318 | if ( isset( $array[ $relation_key ] ) ) { |
||
| 319 | $relation = strtoupper( $array[ $relation_key ] ); |
||
| 320 | } |
||
| 321 | |||
| 322 | View Code Duplication | if ( ! in_array( $relation, $allowed_relations ) ) { |
|
| 323 | Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
||
| 324 | 'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $relation; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Normalize a label by updating case, stripping common prefixes etc. |
||
| 332 | * |
||
| 333 | * @param string $label |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | View Code Duplication | public static function normalize_label( $label ) { |
|
| 337 | // remove the leading underscore(if it's there) |
||
| 338 | $label = preg_replace( '~^_~', '', $label ); |
||
| 339 | |||
| 340 | // remove the leading "crb_"(if it's there) |
||
| 341 | $label = preg_replace( '~^crb_~', '', $label ); |
||
| 342 | |||
| 343 | // split the name into words and make them capitalized |
||
| 344 | $label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
||
| 345 | |||
| 346 | return $label; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Normalize a type string representing an object type |
||
| 351 | * |
||
| 352 | * @param string $type |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | View Code Duplication | public static function normalize_type( $type ) { |
|
| 356 | $normalized_type = str_replace( ' ', '_', $type ); |
||
| 357 | $normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type ); |
||
| 358 | $normalized_type = preg_replace( '/^_|_$/', '', $normalized_type ); |
||
| 359 | $normalized_type = strtolower( $normalized_type ); |
||
| 360 | return $normalized_type; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Convert a string representing an object type to a fully qualified class name |
||
| 365 | * |
||
| 366 | * @param string $type |
||
| 367 | * @param string $namespace |
||
| 368 | * @param string $class_suffix |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 372 | $classlike_type = static::normalize_type( $type ); |
||
| 373 | $classlike_type = str_replace( '_', ' ', $classlike_type ); |
||
| 374 | $classlike_type = ucwords( $classlike_type ); |
||
| 375 | $classlike_type = str_replace( ' ', '_', $classlike_type ); |
||
| 376 | |||
| 377 | $class = $classlike_type . $class_suffix; |
||
| 378 | if ( $namespace ) { |
||
| 379 | $class = $namespace . '\\' . $class; |
||
| 380 | } |
||
| 381 | |||
| 382 | return $class; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Convert a string representing an object type to a fully qualified class name |
||
| 387 | * |
||
| 388 | * @param string $class |
||
| 389 | * @param string $class_suffix |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 393 | $reflection = new \ReflectionClass( $class ); |
||
| 394 | $type = $reflection->getShortName(); |
||
| 395 | |||
| 396 | if ( $class_suffix ) { |
||
| 397 | $type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
||
| 398 | } |
||
| 399 | |||
| 400 | $type = static::normalize_type( $type ); |
||
| 401 | |||
| 402 | return $type; |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get an array of sanitized html classes |
||
| 407 | * |
||
| 408 | * @param string|array<string> $classes |
||
| 409 | * @return array<string> |
||
| 410 | */ |
||
| 411 | public static function sanitize_classes( $classes ) { |
||
| 412 | if ( ! is_array( $classes ) ) { |
||
| 413 | $classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
||
| 414 | } |
||
| 415 | $classes = array_map( 'sanitize_html_class', $classes ); |
||
| 416 | return $classes; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Check if an id or name for containers and fields is valid |
||
| 421 | * |
||
| 422 | * @param string $id |
||
| 423 | * @return boolean |
||
| 424 | */ |
||
| 425 | public static function is_valid_entity_id( $id ) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Return a partial regex pettern matching allowed field name characters |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public static function get_field_name_characters_pattern() { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Get an attachment ID given a file URL |
||
| 440 | * Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
||
| 441 | * |
||
| 442 | * @param string $url |
||
| 443 | * @return integet |
||
| 444 | */ |
||
| 445 | public static function get_attachment_id( $url ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns attachment metadata from an ID. |
||
| 484 | * |
||
| 485 | * @param string $id |
||
| 486 | * @param string $type Value Type. Can be either id or url |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | public static function get_attachment_metadata( $id, $type ) { |
||
| 490 | $attachment_meta = array( |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get the current $_POST or $_GET input array with compacted input values merged in |
||
| 574 | * |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | public static function input() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Get a copy of the passed array with compacted input values merged in |
||
| 590 | * |
||
| 591 | * @param array $input |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | public static function expand_compacted_input( $input ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get valid input from an input array compared to predefined options |
||
| 604 | * |
||
| 605 | * @param array $input |
||
| 606 | * @param array $options |
||
| 607 | * @return array |
||
| 608 | */ |
||
| 609 | public static function get_valid_options( $input, $options ) { |
||
| 627 | } |
||
| 628 |
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.