Complex classes like FrmDb 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 FrmDb, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmDb { |
||
| 4 | public $fields; |
||
| 5 | public $forms; |
||
| 6 | public $entries; |
||
| 7 | public $entry_metas; |
||
| 8 | |||
| 9 | public function __construct() { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Change array into format $wpdb->prepare can use |
||
| 24 | * |
||
| 25 | * @param array $args |
||
| 26 | * @param string $starts_with |
||
| 27 | */ |
||
| 28 | public static function get_where_clause_and_values( &$args, $starts_with = ' WHERE ' ) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param array $args |
||
| 52 | * @param string $base_where |
||
| 53 | * @param string $where |
||
| 54 | * @param array $values |
||
| 55 | */ |
||
| 56 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $key |
||
| 90 | * @param string|array $value |
||
| 91 | * @param string $where |
||
| 92 | * @param array $values |
||
| 93 | */ |
||
| 94 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Add %d, or %s to query |
||
| 165 | * |
||
| 166 | * @since 2.02.05 |
||
| 167 | * |
||
| 168 | * @param string $key |
||
| 169 | * @param int|string $value |
||
| 170 | * @param string $where |
||
| 171 | */ |
||
| 172 | private static function add_query_placeholder( $key, $value, &$where ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $table |
||
| 183 | * @param array $where |
||
| 184 | * @param array $args |
||
| 185 | * |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | public static function get_count( $table, $where = array(), $args = array() ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $table |
||
| 196 | * @param array $where |
||
| 197 | * @param string $field |
||
| 198 | * @param array $args |
||
| 199 | * @param string $limit |
||
| 200 | * @param string $type |
||
| 201 | * |
||
| 202 | * @return array|null|string|object |
||
| 203 | */ |
||
| 204 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Generate a cache key from the where query, field, type, and other arguments |
||
| 222 | * |
||
| 223 | * @since 2.03.07 |
||
| 224 | * |
||
| 225 | * @param array $where |
||
| 226 | * @param array $args |
||
| 227 | * @param string $field |
||
| 228 | * @param string $type |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | private static function generate_cache_key( $where, $args, $field, $type ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $table |
||
| 246 | * @param array $where |
||
| 247 | * @param string $field |
||
| 248 | * @param array $args |
||
| 249 | * @param string $limit |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @since 2.0 |
||
| 259 | * |
||
| 260 | * @param string $table |
||
| 261 | * @param array $where |
||
| 262 | * @param string $fields |
||
| 263 | * @param array $args |
||
| 264 | * |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Prepare a key/value array before DB call |
||
| 275 | * |
||
| 276 | * @since 2.0 |
||
| 277 | * |
||
| 278 | * @param string $table |
||
| 279 | * @param array $where |
||
| 280 | * @param string $fields |
||
| 281 | * @param array $args |
||
| 282 | * |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
||
| 291 | * Return a value to append to the where array key |
||
| 292 | * |
||
| 293 | * @param string $where_is |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public static function append_where_is( $where_is ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join |
||
| 327 | * Also add the wpdb->prefix to the table if it's missing |
||
| 328 | * |
||
| 329 | * @param string $table |
||
| 330 | * @param string $group |
||
| 331 | */ |
||
| 332 | private static function get_group_and_table_name( &$table, &$group ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Only remove the db prefix when at the beginning. |
||
| 352 | * |
||
| 353 | * @since 4.04.02 |
||
| 354 | */ |
||
| 355 | private static function maybe_remove_prefix( $prefix, &$name ) { |
||
| 360 | |||
| 361 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the associative array results for the given columns, table, and where query |
||
| 397 | * |
||
| 398 | * @since 2.02.05 |
||
| 399 | * |
||
| 400 | * @param string $columns |
||
| 401 | * @param string $table |
||
| 402 | * @param array $where |
||
| 403 | * |
||
| 404 | * @return mixed |
||
| 405 | */ |
||
| 406 | public static function get_associative_array_results( $columns, $table, $where ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Combine the pieces of a query to form a full, prepared query |
||
| 420 | * |
||
| 421 | * @since 2.02.05 |
||
| 422 | * |
||
| 423 | * @param string $columns |
||
| 424 | * @param string $table |
||
| 425 | * @param mixed $where |
||
| 426 | * @param array $args |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @since 2.05.07 |
||
| 453 | */ |
||
| 454 | private static function esc_query_args( &$args ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Added for < WP 4.0 compatability |
||
| 470 | * |
||
| 471 | * @since 2.05.06 |
||
| 472 | * |
||
| 473 | * @param string $term The value to escape |
||
| 474 | * |
||
| 475 | * @return string The escaped value |
||
| 476 | */ |
||
| 477 | public static function esc_like( $term ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @since 2.05.06 |
||
| 485 | * |
||
| 486 | * @param string $order_query |
||
| 487 | */ |
||
| 488 | public static function esc_order( $order_query ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Make sure this is ordering by either ASC or DESC |
||
| 518 | * |
||
| 519 | * @since 2.05.06 |
||
| 520 | */ |
||
| 521 | public static function esc_order_by( &$order_by ) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string $limit |
||
| 530 | * |
||
| 531 | * @since 2.05.06 |
||
| 532 | */ |
||
| 533 | public static function esc_limit( $limit ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get an array of values ready to go through $wpdb->prepare |
||
| 557 | * |
||
| 558 | * @since 2.05.06 |
||
| 559 | */ |
||
| 560 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @since 2.05.06 |
||
| 568 | */ |
||
| 569 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Prepare and save settings in styles and actions |
||
| 587 | * |
||
| 588 | * @param array $settings |
||
| 589 | * @param string $group |
||
| 590 | * |
||
| 591 | * @since 2.05.06 |
||
| 592 | */ |
||
| 593 | public static function save_settings( $settings, $group ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 609 | * Remove the filters and then add them back in case any posts or views are |
||
| 610 | * also being imported. |
||
| 611 | * |
||
| 612 | * Used when saving form actions and styles |
||
| 613 | * |
||
| 614 | * @since 2.05.06 |
||
| 615 | */ |
||
| 616 | public static function save_json_post( $settings ) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Check cache before fetching values and saving to cache |
||
| 633 | * |
||
| 634 | * @since 2.05.06 |
||
| 635 | * |
||
| 636 | * @param string $cache_key The unique name for this cache |
||
| 637 | * @param string $group The name of the cache group |
||
| 638 | * @param string $query If blank, don't run a db call |
||
| 639 | * @param string $type The wpdb function to use with this query |
||
| 640 | * |
||
| 641 | * @return mixed $results The cache or query results |
||
| 642 | */ |
||
| 643 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @since 2.05.06 |
||
| 666 | */ |
||
| 667 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Keep track of the keys cached in each group so they can be deleted |
||
| 676 | * in Redis and Memcache |
||
| 677 | * |
||
| 678 | * @since 2.05.06 |
||
| 679 | */ |
||
| 680 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @since 2.05.06 |
||
| 688 | */ |
||
| 689 | public static function get_group_cached_keys( $group ) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @since 2.05.06 |
||
| 700 | * |
||
| 701 | * @param string $cache_key |
||
| 702 | */ |
||
| 703 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Delete all caching in a single group |
||
| 710 | * |
||
| 711 | * @since 2.05.06 |
||
| 712 | * |
||
| 713 | * @param string $group The name of the cache group |
||
| 714 | */ |
||
| 715 | public static function cache_delete_group( $group ) { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @deprecated 2.05.06 |
||
| 729 | * @codeCoverageIgnore |
||
| 730 | */ |
||
| 731 | public function upgrade() { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @deprecated 2.05.06 |
||
| 737 | * @codeCoverageIgnore |
||
| 738 | */ |
||
| 739 | public function collation() { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @deprecated 2.05.06 |
||
| 745 | * @codeCoverageIgnore |
||
| 746 | */ |
||
| 747 | public function uninstall() { |
||
| 750 | } |
||
| 751 |