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 ' ) { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param array $args |
||
| 48 | * @param string $base_where |
||
| 49 | * @param string $where |
||
| 50 | * @param array $values |
||
| 51 | */ |
||
| 52 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $key |
||
| 82 | * @param string|array $value |
||
| 83 | * @param string $where |
||
| 84 | * @param array $values |
||
| 85 | */ |
||
| 86 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Add %d, or %s to query |
||
| 157 | * |
||
| 158 | * @since 2.02.05 |
||
| 159 | * @param string $key |
||
| 160 | * @param int|string $value |
||
| 161 | * @param string $where |
||
| 162 | */ |
||
| 163 | private static function add_query_placeholder( $key, $value, &$where ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $table |
||
| 173 | * @param array $where |
||
| 174 | * @param array $args |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public static function get_count( $table, $where = array(), $args = array() ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $table |
||
| 184 | * @param array $where |
||
| 185 | * @param string $field |
||
| 186 | * @param array $args |
||
| 187 | * @param string $limit |
||
| 188 | * @param string $type |
||
| 189 | * @return array|null|string|object |
||
| 190 | */ |
||
| 191 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Generate a cache key from the where query, field, type, and other arguments |
||
| 205 | * |
||
| 206 | * @since 2.03.07 |
||
| 207 | * |
||
| 208 | * @param array $where |
||
| 209 | * @param array $args |
||
| 210 | * @param string $field |
||
| 211 | * @param string $type |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | private static function generate_cache_key( $where, $args, $field, $type ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $table |
||
| 229 | * @param array $where |
||
| 230 | * @param string $field |
||
| 231 | * @param array $args |
||
| 232 | * @param string $limit |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @since 2.0 |
||
| 241 | * @param string $table |
||
| 242 | * @param array $where |
||
| 243 | * @param string $fields |
||
| 244 | * @param array $args |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Prepare a key/value array before DB call |
||
| 254 | * |
||
| 255 | * @since 2.0 |
||
| 256 | * @param string $table |
||
| 257 | * @param array $where |
||
| 258 | * @param string $fields |
||
| 259 | * @param array $args |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
||
| 268 | * Return a value to append to the where array key |
||
| 269 | * |
||
| 270 | * @param string $where_is |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public static function append_where_is( $where_is ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join |
||
| 303 | * Also add the wpdb->prefix to the table if it's missing |
||
| 304 | * |
||
| 305 | * @param string $table |
||
| 306 | * @param string $group |
||
| 307 | */ |
||
| 308 | private static function get_group_and_table_name( &$table, &$group ) { |
||
| 325 | |||
| 326 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the associative array results for the given columns, table, and where query |
||
| 365 | * |
||
| 366 | * @since 2.02.05 |
||
| 367 | * @param string $columns |
||
| 368 | * @param string $table |
||
| 369 | * @param array $where |
||
| 370 | * @return mixed |
||
| 371 | */ |
||
| 372 | public static function get_associative_array_results( $columns, $table, $where ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Combine the pieces of a query to form a full, prepared query |
||
| 386 | * |
||
| 387 | * @since 2.02.05 |
||
| 388 | * |
||
| 389 | * @param string $columns |
||
| 390 | * @param string $table |
||
| 391 | * @param mixed $where |
||
| 392 | * @param array $args |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Added for < WP 4.0 compatability |
||
| 416 | * |
||
| 417 | * @since 2.06 |
||
| 418 | * |
||
| 419 | * @param string $term The value to escape |
||
| 420 | * @return string The escaped value |
||
| 421 | */ |
||
| 422 | public static function esc_like( $term ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @since 2.06 |
||
| 436 | * @param string $order_query |
||
| 437 | */ |
||
| 438 | public static function esc_order( $order_query ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Make sure this is ordering by either ASC or DESC |
||
| 473 | * @since 2.06 |
||
| 474 | */ |
||
| 475 | public static function esc_order_by( &$order_by ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $limit |
||
| 484 | * @since 2.06 |
||
| 485 | */ |
||
| 486 | public static function esc_limit( $limit ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get an array of values ready to go through $wpdb->prepare |
||
| 509 | * @since 2.06 |
||
| 510 | */ |
||
| 511 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @since 2.06 |
||
| 518 | */ |
||
| 519 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Prepare and save settings in styles and actions |
||
| 537 | * |
||
| 538 | * @param array $settings |
||
| 539 | * @param string $group |
||
| 540 | * |
||
| 541 | * @since 2.06 |
||
| 542 | */ |
||
| 543 | public static function save_settings( $settings, $group ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 559 | * Remove the filters and then add them back in case any posts or views are |
||
| 560 | * also being imported. |
||
| 561 | * |
||
| 562 | * Used when saving form actions and styles |
||
| 563 | * |
||
| 564 | * @since 2.06 |
||
| 565 | */ |
||
| 566 | public static function save_json_post( $settings ) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Check cache before fetching values and saving to cache |
||
| 583 | * |
||
| 584 | * @since 2.06 |
||
| 585 | * |
||
| 586 | * @param string $cache_key The unique name for this cache |
||
| 587 | * @param string $group The name of the cache group |
||
| 588 | * @param string $query If blank, don't run a db call |
||
| 589 | * @param string $type The wpdb function to use with this query |
||
| 590 | * @return mixed $results The cache or query results |
||
| 591 | */ |
||
| 592 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @since 2.06 |
||
| 615 | */ |
||
| 616 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Keep track of the keys cached in each group so they can be deleted |
||
| 625 | * in Redis and Memcache |
||
| 626 | * @since 2.06 |
||
| 627 | */ |
||
| 628 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @since 2.06 |
||
| 636 | */ |
||
| 637 | public static function get_group_cached_keys( $group ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @since 2.06 |
||
| 648 | * @param string $cache_key |
||
| 649 | */ |
||
| 650 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Delete all caching in a single group |
||
| 657 | * |
||
| 658 | * @since 2.06 |
||
| 659 | * |
||
| 660 | * @param string $group The name of the cache group |
||
| 661 | */ |
||
| 662 | public static function cache_delete_group( $group ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @deprecated 2.06 |
||
| 676 | */ |
||
| 677 | public function upgrade( $old_db_version = false ) { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @deprecated 2.06 |
||
| 686 | */ |
||
| 687 | public function collation() { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @deprecated 2.06 |
||
| 696 | */ |
||
| 697 | public function uninstall() { |
||
| 703 | } |
||
| 704 |