Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmFormAction 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 FrmFormAction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmFormAction { |
||
| 4 | |||
| 5 | public $id_base; // Root id for all actions of this type. |
||
| 6 | public $name; // Name for this action type. |
||
| 7 | public $option_name; |
||
| 8 | public $action_options; // Option array passed to wp_register_sidebar_widget() |
||
| 9 | public $control_options; // Option array passed to wp_register_widget_control() |
||
| 10 | |||
| 11 | public $form_id; // The ID of the form to evaluate |
||
| 12 | public $number = false; // Unique ID number of the current instance. |
||
| 13 | public $id = ''; // Unique ID string of the current instance (id_base-number) |
||
| 14 | public $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice. |
||
| 15 | |||
| 16 | // Member functions that you must over-ride. |
||
| 17 | |||
| 18 | /** |
||
| 19 | * This function should check that $new_instance is set correctly. |
||
| 20 | * The newly calculated value of $instance should be returned. |
||
| 21 | * If "false" is returned, the instance won't be saved/updated. |
||
| 22 | * |
||
| 23 | * @param array $new_instance New settings for this instance as input by the user via form() |
||
| 24 | * @param array $old_instance Old settings for this instance |
||
| 25 | * @return array Settings to save or bool false to cancel saving |
||
| 26 | */ |
||
| 27 | public function update( $new_instance, $old_instance ) { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Echo the settings update form |
||
| 33 | * |
||
| 34 | * @param array $instance Current settings |
||
| 35 | */ |
||
| 36 | public function form( $instance, $args = array() ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return array of the default options |
||
| 43 | */ |
||
| 44 | public function get_defaults() { |
||
| 47 | |||
| 48 | public function get_switch_fields() { |
||
| 51 | |||
| 52 | public function migrate_values( $action, $form ) { |
||
| 55 | |||
| 56 | // Functions you'll need to call. |
||
| 57 | |||
| 58 | /** |
||
| 59 | * PHP5 constructor |
||
| 60 | * |
||
| 61 | * @param string $id_base Optional Base ID for the widget, lower case, |
||
| 62 | * if left empty a portion of the widget's class name will be used. Has to be unique. |
||
| 63 | * @param string $name Name for the widget displayed on the configuration page. |
||
| 64 | * @param array $action_options Optional Passed to wp_register_sidebar_widget() |
||
| 65 | * - description: shown on the configuration page |
||
| 66 | * - classname |
||
| 67 | * @param array $control_options Optional Passed to wp_register_widget_control() |
||
| 68 | * - width: required if more than 250px |
||
| 69 | * - height: currently not used but may be needed in the future |
||
| 70 | */ |
||
| 71 | public function __construct( $id_base, $name, $action_options = array(), $control_options = array() ) { |
||
| 72 | if ( ! defined('ABSPATH') ) { |
||
| 73 | die('You are not allowed to call this page directly.'); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->id_base = strtolower($id_base); |
||
| 77 | $this->name = $name; |
||
| 78 | $this->option_name = 'frm_' . $this->id_base . '_action'; |
||
| 79 | |||
| 80 | $default_options = array( |
||
| 81 | 'classes' => '', |
||
| 82 | 'active' => true, |
||
| 83 | 'event' => array( 'create' ), |
||
| 84 | 'limit' => 1, |
||
| 85 | 'force_event' => false, |
||
| 86 | 'priority' => 20, |
||
| 87 | 'ajax_load' => true, |
||
| 88 | 'tooltip' => $name, |
||
| 89 | ); |
||
| 90 | |||
| 91 | $action_options = apply_filters( 'frm_' . $id_base . '_action_options', $action_options ); |
||
| 92 | $this->action_options = wp_parse_args( $action_options, $default_options ); |
||
| 93 | $this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $id_base |
||
| 98 | */ |
||
| 99 | public function FrmFormAction( $id_base, $name, $action_options = array(), $control_options = array() ) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Constructs name attributes for use in form() fields |
||
| 105 | * |
||
| 106 | * This function should be used in form() methods to create name attributes for fields to be saved by update() |
||
| 107 | * |
||
| 108 | * @param string $field_name Field name |
||
| 109 | * @return string Name attribute for $field_name |
||
| 110 | */ |
||
| 111 | public function get_field_name( $field_name, $post_field = 'post_content' ) { |
||
| 112 | $name = $this->option_name . '[' . $this->number . ']'; |
||
| 113 | $name .= ( empty( $post_field ) ? '' : '[' . $post_field . ']' ); |
||
| 114 | $name .= '[' . $field_name . ']'; |
||
| 115 | return $name; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Constructs id attributes for use in form() fields |
||
| 120 | * |
||
| 121 | * This function should be used in form() methods to create id attributes for fields to be saved by update() |
||
| 122 | * |
||
| 123 | * @param string $field_name Field name |
||
| 124 | * @return string ID attribute for $field_name |
||
| 125 | */ |
||
| 126 | public function get_field_id( $field_name ) { |
||
| 127 | return $field_name . '_' . $this->number; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Private Function. Don't worry about this. |
||
| 131 | |||
| 132 | public function _set( $number ) { |
||
| 136 | |||
| 137 | public function prepare_new( $form_id = false ) { |
||
| 166 | |||
| 167 | public function create( $form_id ) { |
||
| 174 | |||
| 175 | public function duplicate_form_actions( $form_id, $old_id ) { |
||
| 190 | |||
| 191 | /* Check if imported action should be created or updated |
||
| 192 | * |
||
| 193 | * Since 2.0 |
||
| 194 | * |
||
| 195 | * @param array $action |
||
| 196 | * @return integer $post_id |
||
| 197 | */ |
||
| 198 | public function maybe_create_action( $action, $forms ) { |
||
| 210 | |||
| 211 | public function duplicate_one( $action, $form_id ) { |
||
| 238 | |||
| 239 | private function duplicate_array_walk( $action, $subkey, $val ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Deal with changed settings. |
||
| 267 | * |
||
| 268 | * Do NOT over-ride this function |
||
| 269 | * |
||
| 270 | */ |
||
| 271 | public function update_callback( $form_id ) { |
||
| 272 | $this->form_id = $form_id; |
||
| 273 | |||
| 274 | $all_instances = $this->get_settings(); |
||
| 275 | |||
| 276 | // We need to update the data |
||
| 277 | if ( $this->updated ) { |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | if ( isset( $_POST[ $this->option_name ] ) && is_array( $_POST[ $this->option_name ] ) ) { |
||
| 282 | $settings = $_POST[ $this->option_name ]; |
||
| 283 | } else { |
||
| 284 | return; |
||
| 285 | } |
||
| 286 | |||
| 287 | $action_ids = array(); |
||
| 288 | |||
| 289 | foreach ( $settings as $number => $new_instance ) { |
||
| 290 | $this->_set($number); |
||
| 291 | |||
| 292 | if ( ! isset($new_instance['post_title']) ) { |
||
| 293 | // settings were never opened, so don't update |
||
| 294 | $action_ids[] = $new_instance['ID']; |
||
| 295 | $this->updated = true; |
||
| 296 | continue; |
||
| 297 | } |
||
| 298 | |||
| 299 | $old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array(); |
||
| 300 | |||
| 301 | $new_instance['post_type'] = FrmFormActionsController::$action_post_type; |
||
| 302 | $new_instance['post_name'] = $this->form_id . '_' . $this->id_base . '_' . $this->number; |
||
| 303 | $new_instance['menu_order'] = $this->form_id; |
||
| 304 | $new_instance['post_status'] = 'publish'; |
||
| 305 | $new_instance['post_date'] = isset( $old_instance->post_date ) ? $old_instance->post_date : ''; |
||
| 306 | |||
| 307 | $instance = $this->update( $new_instance, $old_instance ); |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Filter an action's settings before saving. |
||
| 311 | * |
||
| 312 | * Returning false will effectively short-circuit the widget's ability |
||
| 313 | * to update settings. |
||
| 314 | * |
||
| 315 | * @since 2.0 |
||
| 316 | * |
||
| 317 | * @param array $instance The current widget instance's settings. |
||
| 318 | * @param array $new_instance Array of new widget settings. |
||
| 319 | * @param array $old_instance Array of old widget settings. |
||
| 320 | * @param WP_Widget $this The current widget instance. |
||
| 321 | */ |
||
| 322 | $instance = apply_filters( 'frm_action_update_callback', $instance, $new_instance, $old_instance, $this ); |
||
| 323 | |||
| 324 | $instance['post_content'] = apply_filters('frm_before_save_action', $instance['post_content'], $instance, $new_instance, $old_instance, $this); |
||
| 325 | $instance['post_content'] = apply_filters( 'frm_before_save_' . $this->id_base . '_action', $new_instance['post_content'], $instance, $new_instance, $old_instance, $this ); |
||
| 326 | |||
| 327 | if ( false !== $instance ) { |
||
| 328 | $all_instances[ $number ] = $instance; |
||
| 329 | } |
||
| 330 | |||
| 331 | $action_ids[] = $this->save_settings($instance); |
||
| 332 | |||
| 333 | $this->updated = true; |
||
| 334 | } |
||
| 335 | |||
| 336 | return $action_ids; |
||
| 337 | } |
||
| 338 | |||
| 339 | public function save_settings( $settings ) { |
||
| 343 | |||
| 344 | public function get_single_action( $id ) { |
||
| 352 | |||
| 353 | public function get_one( $form_id ) { |
||
| 356 | |||
| 357 | public static function get_action_for_form( $form_id, $type = 'all', $limit = 99 ) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param int $action_id |
||
| 401 | */ |
||
| 402 | public static function get_single_action_type( $action_id, $type ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param int $form_id |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public static function form_has_action_type( $form_id, $type ) { |
||
| 415 | |||
| 416 | public function get_all( $form_id = false, $limit = 99 ) { |
||
| 457 | |||
| 458 | public static function action_args( $form_id = 0, $limit = 99 ) { |
||
| 473 | |||
| 474 | public function prepare_action( $action ) { |
||
| 498 | |||
| 499 | public function destroy( $form_id = false, $type = 'default' ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Delete the action cache when a form action is created, deleted, or updated |
||
| 522 | * |
||
| 523 | * @since 2.0.5 |
||
| 524 | */ |
||
| 525 | public static function clear_cache() { |
||
| 528 | |||
| 529 | public function get_settings() { |
||
| 532 | |||
| 533 | public function get_global_defaults() { |
||
| 549 | |||
| 550 | public function get_global_switch_fields() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Migrate settings from form->options into new action. |
||
| 558 | */ |
||
| 559 | public function migrate_to_2( $form, $update = 'update' ) { |
||
| 597 | |||
| 598 | public static function action_conditions_met( $action, $entry ) { |
||
| 599 | $notification = $action->post_content; |
||
| 600 | $stop = false; |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Prepare the logic value for comparison against the entered value |
||
| 640 | * |
||
| 641 | * @since 2.01.02 |
||
| 642 | * @param array|string $logic_value |
||
| 643 | */ |
||
| 644 | private static function prepare_logic_value( &$logic_value ) { |
||
| 655 | |||
| 656 | |||
| 657 | /** |
||
| 658 | * Get the value from a specific field and entry |
||
| 659 | * |
||
| 660 | * @since 2.01.02 |
||
| 661 | * @param object $entry |
||
| 662 | * @param int $field_id |
||
| 663 | * @return array|bool|mixed|string |
||
| 664 | */ |
||
| 665 | private static function get_value_from_entry( $entry, $field_id ) { |
||
| 677 | |||
| 678 | public static function default_action_opts( $class = '' ) { |
||
| 685 | |||
| 686 | public static function trigger_labels() { |
||
| 695 | } |
||
| 696 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.