Complex classes like FrmFormsListHelper 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 FrmFormsListHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmFormsListHelper extends FrmListHelper { |
||
| 7 | public $status = ''; |
||
| 8 | |||
| 9 | public $total_items = 0; |
||
| 10 | |||
| 11 | public function __construct( $args ) { |
||
| 12 | $this->status = self::get_param( array( 'param' => 'form_type' ) ); |
||
|
|
|||
| 13 | |||
| 14 | parent::__construct( $args ); |
||
| 15 | } |
||
| 16 | |||
| 17 | public function prepare_items() { |
||
| 18 | global $wpdb, $per_page, $mode; |
||
| 19 | |||
| 20 | $page = $this->get_pagenum(); |
||
| 21 | $per_page = $this->get_items_per_page( 'formidable_page_formidable_per_page' ); |
||
| 22 | |||
| 23 | $mode = self::get_param( |
||
| 24 | array( |
||
| 25 | 'param' => 'mode', |
||
| 26 | 'default' => 'list', |
||
| 27 | ) |
||
| 28 | ); |
||
| 29 | $orderby = self::get_param( |
||
| 30 | array( |
||
| 31 | 'param' => 'orderby', |
||
| 32 | 'default' => 'name', |
||
| 33 | ) |
||
| 34 | ); |
||
| 35 | $order = self::get_param( |
||
| 36 | array( |
||
| 37 | 'param' => 'order', |
||
| 38 | 'default' => 'ASC', |
||
| 39 | ) |
||
| 40 | ); |
||
| 41 | $start = self::get_param( |
||
| 42 | array( |
||
| 43 | 'param' => 'start', |
||
| 44 | 'default' => ( $page - 1 ) * $per_page, |
||
| 45 | ) |
||
| 46 | ); |
||
| 47 | |||
| 48 | $s_query = array( |
||
| 49 | array( |
||
| 50 | 'or' => 1, |
||
| 51 | 'parent_form_id' => null, |
||
| 52 | 'parent_form_id <' => 1, |
||
| 53 | ), |
||
| 54 | ); |
||
| 55 | switch ( $this->status ) { |
||
| 56 | case 'draft': |
||
| 57 | $s_query['is_template'] = 0; |
||
| 58 | $s_query['status'] = 'draft'; |
||
| 59 | break; |
||
| 60 | case 'trash': |
||
| 61 | $s_query['status'] = 'trash'; |
||
| 62 | break; |
||
| 63 | default: |
||
| 64 | $s_query['is_template'] = 0; |
||
| 65 | $s_query['status !'] = 'trash'; |
||
| 66 | break; |
||
| 67 | } |
||
| 68 | |||
| 69 | $s = self::get_param( |
||
| 70 | array( |
||
| 71 | 'param' => 's', |
||
| 72 | 'sanitize' => 'sanitize_text_field', |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | if ( $s != '' ) { |
||
| 76 | preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches ); |
||
| 77 | $search_terms = array_map( 'trim', $matches[0] ); |
||
| 78 | foreach ( (array) $search_terms as $term ) { |
||
| 79 | $s_query[] = array( |
||
| 80 | 'or' => true, |
||
| 81 | 'name LIKE' => $term, |
||
| 82 | 'description LIKE' => $term, |
||
| 83 | 'created_at LIKE' => $term, |
||
| 84 | 'form_key LIKE' => $term, |
||
| 85 | 'id' => $term, |
||
| 86 | ); |
||
| 87 | unset( $term ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->items = FrmForm::getAll( $s_query, $orderby . ' ' . $order, $start . ',' . $per_page ); |
||
| 92 | $total_items = FrmDb::get_count( 'frm_forms', $s_query ); |
||
| 93 | $this->total_items = $total_items; |
||
| 94 | |||
| 95 | $this->set_pagination_args( |
||
| 96 | array( |
||
| 97 | 'total_items' => $total_items, |
||
| 98 | 'per_page' => $per_page, |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function no_items() { |
||
| 104 | if ( $this->status === 'trash' ) { |
||
| 105 | echo '<p>'; |
||
| 106 | esc_html_e( 'No forms found in the trash.', 'formidable' ); |
||
| 107 | ?> |
||
| 108 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable' ) ); ?>"> |
||
| 109 | <?php esc_html_e( 'See all forms.', 'formidable' ); ?> |
||
| 110 | </a> |
||
| 111 | <?php |
||
| 112 | echo '</p>'; |
||
| 113 | } else { |
||
| 114 | $title = __( 'No Forms Found', 'formidable' ); |
||
| 115 | include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_no_forms.php'; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public function get_bulk_actions() { |
||
| 138 | |||
| 139 | public function extra_tablenav( $which ) { |
||
| 152 | |||
| 153 | public function get_views() { |
||
| 188 | |||
| 189 | public function pagination( $which ) { |
||
| 198 | |||
| 199 | public function single_row( $item, $style = '' ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $edit_link |
||
| 287 | */ |
||
| 288 | private function get_actions( &$actions, $item, $edit_link ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $edit_link |
||
| 311 | */ |
||
| 312 | private function get_form_name( $item, $actions, $edit_link, $mode = 'list' ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $val |
||
| 339 | */ |
||
| 340 | private function add_draft_label( $item, &$val ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $val |
||
| 348 | */ |
||
| 349 | private function add_form_description( $item, &$val ) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | protected function confirm_bulk_delete() { |
||
| 362 | } |
||
| 363 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.