Complex classes like FrmMigrate 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 FrmMigrate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmMigrate { |
||
| 4 | public $fields; |
||
| 5 | public $forms; |
||
| 6 | public $entries; |
||
| 7 | public $entry_metas; |
||
| 8 | |||
| 9 | public function __construct() { |
||
| 10 | if ( ! defined('ABSPATH') ) { |
||
| 11 | die('You are not allowed to call this page directly.'); |
||
| 12 | } |
||
| 13 | |||
| 14 | global $wpdb; |
||
| 15 | $this->fields = $wpdb->prefix . 'frm_fields'; |
||
| 16 | $this->forms = $wpdb->prefix . 'frm_forms'; |
||
| 17 | $this->entries = $wpdb->prefix . 'frm_items'; |
||
| 18 | $this->entry_metas = $wpdb->prefix . 'frm_item_metas'; |
||
| 19 | } |
||
| 20 | |||
| 21 | public function upgrade( $old_db_version = false ) { |
||
| 66 | |||
| 67 | public function collation() { |
||
| 84 | |||
| 85 | private function create_tables() { |
||
| 171 | |||
| 172 | private function maybe_create_contact_form() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param int|string $old_db_version |
||
| 188 | */ |
||
| 189 | private function migrate_data( $old_db_version ) { |
||
| 211 | |||
| 212 | public function uninstall() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Reverse migration 17 -- Divide by 9 |
||
| 266 | */ |
||
| 267 | private function migrate_to_86() { |
||
| 268 | |||
| 269 | $fields = $this->get_fields_with_size(); |
||
| 270 | |||
| 271 | foreach ( (array) $fields as $f ) { |
||
| 272 | $f->field_options = maybe_unserialize( $f->field_options ); |
||
| 273 | $size = $f->field_options['size']; |
||
| 274 | $this->maybe_convert_migrated_size( $size ); |
||
| 275 | |||
| 276 | if ( $size === $f->field_options['size'] ) { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | |||
| 280 | $f->field_options['size'] = $size; |
||
| 281 | FrmField::update( $f->id, array( 'field_options' => $f->field_options ) ); |
||
| 282 | unset( $f ); |
||
| 283 | } |
||
| 284 | |||
| 285 | // reverse the extra size changes in widgets |
||
| 286 | $widgets = get_option( 'widget_frm_show_form' ); |
||
| 287 | if ( empty( $widgets ) ) { |
||
| 288 | return; |
||
| 289 | } |
||
| 290 | |||
| 291 | $this->revert_widget_field_size(); |
||
| 292 | } |
||
| 293 | |||
| 294 | private function get_fields_with_size() { |
||
| 295 | $field_types = array( 'textarea', 'text', 'number', 'email', 'url', 'rte', 'date', 'phone', 'password', 'image', 'tag', 'file' ); |
||
| 296 | $query = array( |
||
| 297 | 'type' => $field_types, |
||
| 298 | 'field_options like' => 's:4:"size";', |
||
| 299 | 'field_options not like' => 's:4:"size";s:0:', |
||
| 300 | ); |
||
| 301 | |||
| 302 | return FrmDb::get_results( $this->fields, $query, 'id, field_options' ); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * reverse the extra size changes in widgets |
||
| 307 | */ |
||
| 308 | private function revert_widget_field_size() { |
||
| 309 | $widgets = get_option( 'widget_frm_show_form' ); |
||
| 310 | if ( empty( $widgets ) ) { |
||
| 311 | return; |
||
| 312 | } |
||
| 313 | |||
| 314 | $widgets = maybe_unserialize( $widgets ); |
||
| 315 | foreach ( $widgets as $k => $widget ) { |
||
| 316 | if ( ! is_array( $widget ) || ! isset( $widget['size'] ) ) { |
||
| 317 | continue; |
||
| 318 | } |
||
| 319 | |||
| 320 | $size = $widget['size']; |
||
| 321 | $this->maybe_convert_migrated_size( $size ); |
||
| 322 | if ( $size === $widget['size'] ) { |
||
| 323 | continue; |
||
| 324 | } |
||
| 325 | |||
| 326 | $widgets[ $k ]['size'] = $size; |
||
| 327 | } |
||
| 328 | update_option( 'widget_frm_show_form', $widgets ); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Divide by 9 to reverse the multiplication |
||
| 333 | * @since 3.0.05 |
||
| 334 | */ |
||
| 335 | private function maybe_convert_migrated_size( &$size ) { |
||
| 336 | $has_px_size = ! empty( $size ) && strpos( $size, 'px' ); |
||
| 337 | if ( ! $has_px_size ) { |
||
| 338 | return; |
||
| 339 | } |
||
| 340 | |||
| 341 | $int_size = str_replace( 'px', '', $size ); |
||
| 342 | if ( ! is_numeric( $int_size ) || (int) $int_size < 900 ) { |
||
| 343 | return; |
||
| 344 | } |
||
| 345 | |||
| 346 | $pixel_conversion = 9; |
||
| 347 | $size = round( (int) $int_size / $pixel_conversion ); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Migrate old styling settings. If sites are using the old |
||
| 352 | * default 400px field width, switch it to 100% |
||
| 353 | * |
||
| 354 | * @since 2.0.4 |
||
| 355 | */ |
||
| 356 | private function migrate_to_25() { |
||
| 357 | // get the style that was created with the style migration |
||
| 358 | $frm_style = new FrmStyle(); |
||
| 359 | $styles = $frm_style->get_all( 'post_date', 'ASC', 1 ); |
||
| 360 | if ( empty( $styles ) ) { |
||
| 361 | return; |
||
| 362 | } |
||
| 363 | |||
| 364 | foreach ( $styles as $style ) { |
||
| 365 | if ( $style->post_content['field_width'] == '400px' ) { |
||
| 366 | $style->post_content['field_width'] = '100%'; |
||
| 367 | $frm_style->save( (array) $style ); |
||
| 368 | return; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Check if the parent_form_id columns exists. |
||
| 375 | * If not, try and add it again |
||
| 376 | * |
||
| 377 | * @since 2.0.2 |
||
| 378 | */ |
||
| 379 | private function migrate_to_23() { |
||
| 380 | global $wpdb; |
||
| 381 | $exists = $wpdb->get_row( 'SHOW COLUMNS FROM ' . $this->forms . ' LIKE "parent_form_id"' ); |
||
| 382 | if ( empty( $exists ) ) { |
||
| 383 | $wpdb->query( 'ALTER TABLE ' . $this->forms . ' ADD parent_form_id int(11) default 0' ); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Change field size from character to pixel -- Multiply by 9 |
||
| 389 | */ |
||
| 390 | private function migrate_to_17() { |
||
| 391 | $pixel_conversion = 9; |
||
| 392 | |||
| 393 | $fields = $this->get_fields_with_size(); |
||
| 394 | |||
| 395 | foreach ( $fields as $f ) { |
||
|
|
|||
| 396 | $f->field_options = maybe_unserialize($f->field_options); |
||
| 397 | if ( empty($f->field_options['size']) || ! is_numeric($f->field_options['size']) ) { |
||
| 398 | continue; |
||
| 399 | } |
||
| 400 | |||
| 401 | $f->field_options['size'] = round( $pixel_conversion * (int) $f->field_options['size'] ); |
||
| 402 | $f->field_options['size'] .= 'px'; |
||
| 403 | FrmField::update( $f->id, array( 'field_options' => $f->field_options ) ); |
||
| 404 | unset($f); |
||
| 405 | } |
||
| 406 | |||
| 407 | // Change the characters in widgets to pixels |
||
| 408 | $widgets = get_option('widget_frm_show_form'); |
||
| 409 | if ( empty($widgets) ) { |
||
| 410 | return; |
||
| 411 | } |
||
| 412 | |||
| 413 | $widgets = maybe_unserialize($widgets); |
||
| 414 | foreach ( $widgets as $k => $widget ) { |
||
| 415 | if ( ! is_array($widget) || ! isset($widget['size']) ) { |
||
| 416 | continue; |
||
| 417 | } |
||
| 418 | $size = round( $pixel_conversion * (int) $widget['size'] ); |
||
| 419 | $size .= 'px'; |
||
| 420 | $widgets[ $k ]['size'] = $size; |
||
| 421 | } |
||
| 422 | update_option('widget_frm_show_form', $widgets); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Migrate post and email notification settings into actions |
||
| 427 | */ |
||
| 428 | private function migrate_to_16() { |
||
| 475 | |||
| 476 | private function migrate_to_11() { |
||
| 511 | |||
| 512 | private function migrate_to_6() { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Adds user id to the entry |
||
| 553 | */ |
||
| 554 | private function migrate_to_4() { |
||
| 561 | } |
||
| 562 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.