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 = '' ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the associative array results for the given columns, table, and where query |
||
| 362 | * |
||
| 363 | * @since 2.02.05 |
||
| 364 | * @param string $columns |
||
| 365 | * @param string $table |
||
| 366 | * @param array $where |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | public static function get_associative_array_results( $columns, $table, $where ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Combine the pieces of a query to form a full, prepared query |
||
| 383 | * |
||
| 384 | * @since 2.02.05 |
||
| 385 | * |
||
| 386 | * @param string $columns |
||
| 387 | * @param string $table |
||
| 388 | * @param mixed $where |
||
| 389 | * @param array $args |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @since 2.05.07 |
||
| 415 | */ |
||
| 416 | private static function esc_query_args( &$args ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Added for < WP 4.0 compatability |
||
| 432 | * |
||
| 433 | * @since 2.05.06 |
||
| 434 | * |
||
| 435 | * @param string $term The value to escape |
||
| 436 | * @return string The escaped value |
||
| 437 | */ |
||
| 438 | public static function esc_like( $term ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @since 2.05.06 |
||
| 452 | * @param string $order_query |
||
| 453 | */ |
||
| 454 | public static function esc_order( $order_query ) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Make sure this is ordering by either ASC or DESC |
||
| 489 | * @since 2.05.06 |
||
| 490 | */ |
||
| 491 | public static function esc_order_by( &$order_by ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $limit |
||
| 500 | * @since 2.05.06 |
||
| 501 | */ |
||
| 502 | public static function esc_limit( $limit ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get an array of values ready to go through $wpdb->prepare |
||
| 525 | * @since 2.05.06 |
||
| 526 | */ |
||
| 527 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @since 2.05.06 |
||
| 534 | */ |
||
| 535 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Prepare and save settings in styles and actions |
||
| 553 | * |
||
| 554 | * @param array $settings |
||
| 555 | * @param string $group |
||
| 556 | * |
||
| 557 | * @since 2.05.06 |
||
| 558 | */ |
||
| 559 | public static function save_settings( $settings, $group ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 575 | * Remove the filters and then add them back in case any posts or views are |
||
| 576 | * also being imported. |
||
| 577 | * |
||
| 578 | * Used when saving form actions and styles |
||
| 579 | * |
||
| 580 | * @since 2.05.06 |
||
| 581 | */ |
||
| 582 | public static function save_json_post( $settings ) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Check cache before fetching values and saving to cache |
||
| 599 | * |
||
| 600 | * @since 2.05.06 |
||
| 601 | * |
||
| 602 | * @param string $cache_key The unique name for this cache |
||
| 603 | * @param string $group The name of the cache group |
||
| 604 | * @param string $query If blank, don't run a db call |
||
| 605 | * @param string $type The wpdb function to use with this query |
||
| 606 | * @return mixed $results The cache or query results |
||
| 607 | */ |
||
| 608 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @since 2.05.06 |
||
| 631 | */ |
||
| 632 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Keep track of the keys cached in each group so they can be deleted |
||
| 641 | * in Redis and Memcache |
||
| 642 | * @since 2.05.06 |
||
| 643 | */ |
||
| 644 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @since 2.05.06 |
||
| 652 | */ |
||
| 653 | public static function get_group_cached_keys( $group ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @since 2.05.06 |
||
| 664 | * @param string $cache_key |
||
| 665 | */ |
||
| 666 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Delete all caching in a single group |
||
| 673 | * |
||
| 674 | * @since 2.05.06 |
||
| 675 | * |
||
| 676 | * @param string $group The name of the cache group |
||
| 677 | */ |
||
| 678 | public static function cache_delete_group( $group ) { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * @deprecated 2.05.06 |
||
| 692 | */ |
||
| 693 | public function upgrade( $old_db_version = false ) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @deprecated 2.05.06 |
||
| 702 | */ |
||
| 703 | public function collation() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @deprecated 2.05.06 |
||
| 712 | */ |
||
| 713 | public function uninstall() { |
||
| 719 | } |
||
| 720 |