| Total Complexity | 104 |
| Total Lines | 718 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 ' ) { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $args |
||
| 51 | * @param string $base_where |
||
| 52 | * @param string $where |
||
| 53 | * @param array $values |
||
| 54 | */ |
||
| 55 | public static function parse_where_from_array( $args, $base_where, &$where, &$values ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $key |
||
| 89 | * @param string|array $value |
||
| 90 | * @param string $where |
||
| 91 | * @param array $values |
||
| 92 | */ |
||
| 93 | private static function interpret_array_to_sql( $key, $value, &$where, &$values ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Add %d, or %s to query |
||
| 164 | * |
||
| 165 | * @since 2.02.05 |
||
| 166 | * @param string $key |
||
| 167 | * @param int|string $value |
||
| 168 | * @param string $where |
||
| 169 | */ |
||
| 170 | private static function add_query_placeholder( $key, $value, &$where ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $table |
||
| 181 | * @param array $where |
||
| 182 | * @param array $args |
||
| 183 | * @return int |
||
| 184 | */ |
||
| 185 | public static function get_count( $table, $where = array(), $args = array() ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $table |
||
| 192 | * @param array $where |
||
| 193 | * @param string $field |
||
| 194 | * @param array $args |
||
| 195 | * @param string $limit |
||
| 196 | * @param string $type |
||
| 197 | * @return array|null|string|object |
||
| 198 | */ |
||
| 199 | public static function get_var( $table, $where = array(), $field = 'id', $args = array(), $limit = '', $type = 'var' ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Generate a cache key from the where query, field, type, and other arguments |
||
| 213 | * |
||
| 214 | * @since 2.03.07 |
||
| 215 | * |
||
| 216 | * @param array $where |
||
| 217 | * @param array $args |
||
| 218 | * @param string $field |
||
| 219 | * @param string $type |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | private static function generate_cache_key( $where, $args, $field, $type ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $table |
||
| 237 | * @param array $where |
||
| 238 | * @param string $field |
||
| 239 | * @param array $args |
||
| 240 | * @param string $limit |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | public static function get_col( $table, $where = array(), $field = 'id', $args = array(), $limit = '' ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @since 2.0 |
||
| 249 | * @param string $table |
||
| 250 | * @param array $where |
||
| 251 | * @param string $fields |
||
| 252 | * @param array $args |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public static function get_row( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Prepare a key/value array before DB call |
||
| 262 | * |
||
| 263 | * @since 2.0 |
||
| 264 | * @param string $table |
||
| 265 | * @param array $where |
||
| 266 | * @param string $fields |
||
| 267 | * @param array $args |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public static function get_results( $table, $where = array(), $fields = '*', $args = array() ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Check for like, not like, in, not in, =, !=, >, <, <=, >= |
||
| 276 | * Return a value to append to the where array key |
||
| 277 | * |
||
| 278 | * @param string $where_is |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function append_where_is( $where_is ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get 'frm_forms' from wp_frm_forms or a longer table param that includes a join |
||
| 311 | * Also add the wpdb->prefix to the table if it's missing |
||
| 312 | * |
||
| 313 | * @param string $table |
||
| 314 | * @param string $group |
||
| 315 | */ |
||
| 316 | private static function get_group_and_table_name( &$table, &$group ) { |
||
| 333 | |||
| 334 | private static function convert_options_to_array( &$args, $order_by = '', $limit = '' ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get the associative array results for the given columns, table, and where query |
||
| 370 | * |
||
| 371 | * @since 2.02.05 |
||
| 372 | * @param string $columns |
||
| 373 | * @param string $table |
||
| 374 | * @param array $where |
||
| 375 | * @return mixed |
||
| 376 | */ |
||
| 377 | public static function get_associative_array_results( $columns, $table, $where ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Combine the pieces of a query to form a full, prepared query |
||
| 391 | * |
||
| 392 | * @since 2.02.05 |
||
| 393 | * |
||
| 394 | * @param string $columns |
||
| 395 | * @param string $table |
||
| 396 | * @param mixed $where |
||
| 397 | * @param array $args |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | private static function generate_query_string_from_pieces( $columns, $table, $where, $args = array() ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @since 2.05.07 |
||
| 423 | */ |
||
| 424 | private static function esc_query_args( &$args ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Added for < WP 4.0 compatability |
||
| 440 | * |
||
| 441 | * @since 2.05.06 |
||
| 442 | * |
||
| 443 | * @param string $term The value to escape |
||
| 444 | * @return string The escaped value |
||
| 445 | */ |
||
| 446 | public static function esc_like( $term ) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @since 2.05.06 |
||
| 460 | * @param string $order_query |
||
| 461 | */ |
||
| 462 | public static function esc_order( $order_query ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Make sure this is ordering by either ASC or DESC |
||
| 492 | * @since 2.05.06 |
||
| 493 | */ |
||
| 494 | public static function esc_order_by( &$order_by ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $limit |
||
| 503 | * @since 2.05.06 |
||
| 504 | */ |
||
| 505 | public static function esc_limit( $limit ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get an array of values ready to go through $wpdb->prepare |
||
| 528 | * @since 2.05.06 |
||
| 529 | */ |
||
| 530 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @since 2.05.06 |
||
| 537 | */ |
||
| 538 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Prepare and save settings in styles and actions |
||
| 556 | * |
||
| 557 | * @param array $settings |
||
| 558 | * @param string $group |
||
| 559 | * |
||
| 560 | * @since 2.05.06 |
||
| 561 | */ |
||
| 562 | public static function save_settings( $settings, $group ) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 578 | * Remove the filters and then add them back in case any posts or views are |
||
| 579 | * also being imported. |
||
| 580 | * |
||
| 581 | * Used when saving form actions and styles |
||
| 582 | * |
||
| 583 | * @since 2.05.06 |
||
| 584 | */ |
||
| 585 | public static function save_json_post( $settings ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Check cache before fetching values and saving to cache |
||
| 602 | * |
||
| 603 | * @since 2.05.06 |
||
| 604 | * |
||
| 605 | * @param string $cache_key The unique name for this cache |
||
| 606 | * @param string $group The name of the cache group |
||
| 607 | * @param string $query If blank, don't run a db call |
||
| 608 | * @param string $type The wpdb function to use with this query |
||
| 609 | * @return mixed $results The cache or query results |
||
| 610 | */ |
||
| 611 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @since 2.05.06 |
||
| 634 | */ |
||
| 635 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Keep track of the keys cached in each group so they can be deleted |
||
| 644 | * in Redis and Memcache |
||
| 645 | * @since 2.05.06 |
||
| 646 | */ |
||
| 647 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @since 2.05.06 |
||
| 655 | */ |
||
| 656 | public static function get_group_cached_keys( $group ) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @since 2.05.06 |
||
| 667 | * @param string $cache_key |
||
| 668 | */ |
||
| 669 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Delete all caching in a single group |
||
| 676 | * |
||
| 677 | * @since 2.05.06 |
||
| 678 | * |
||
| 679 | * @param string $group The name of the cache group |
||
| 680 | */ |
||
| 681 | public static function cache_delete_group( $group ) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @deprecated 2.05.06 |
||
| 695 | */ |
||
| 696 | public function upgrade( $old_db_version = false ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @deprecated 2.05.06 |
||
| 705 | */ |
||
| 706 | public function collation() { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @deprecated 2.05.06 |
||
| 715 | */ |
||
| 716 | public function uninstall() { |
||
| 717 | _deprecated_function( __METHOD__, '2.05.06', 'FrmMigrate::uninstall' ); |
||
| 718 | |||
| 719 | $db = new FrmMigrate(); |
||
| 720 | $db->uninstall(); |
||
| 721 | } |
||
| 723 |