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 FrmEntriesListHelper 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 FrmEntriesListHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmEntriesListHelper extends FrmListHelper { |
||
| 4 | protected $column_name; |
||
| 5 | protected $item; |
||
| 6 | protected $field; |
||
| 7 | |||
| 8 | public function prepare_items() { |
||
| 9 | global $per_page; |
||
| 10 | |||
| 11 | $per_page = $this->get_items_per_page( 'formidable_page_formidable_entries_per_page' ); |
||
|
|
|||
| 12 | $form_id = $this->params['form']; |
||
| 13 | |||
| 14 | $s_query = array(); |
||
| 15 | |||
| 16 | if ( $form_id ) { |
||
| 17 | $s_query['it.form_id'] = $form_id; |
||
| 18 | $join_form_in_query = false; |
||
| 19 | } else { |
||
| 20 | $s_query[] = array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 1 ); |
||
| 21 | $join_form_in_query = true; |
||
| 22 | } |
||
| 23 | |||
| 24 | $s = self::get_param( array( 'param' => 's', 'sanitize' => 'sanitize_text_field' ) ); |
||
| 25 | |||
| 26 | if ( $s != '' && FrmAppHelper::pro_is_installed() ) { |
||
| 27 | $fid = self::get_param( array( 'param' => 'fid' ) ); |
||
| 28 | $s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid ); |
||
| 29 | } |
||
| 30 | |||
| 31 | $s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) ); |
||
| 32 | |||
| 33 | $orderby = self::get_param( array( 'param' => 'orderby', 'default' => 'id' ) ); |
||
| 34 | |||
| 35 | if ( strpos( $orderby, 'meta' ) !== false ) { |
||
| 36 | $order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) ); |
||
| 37 | $orderby .= in_array( $order_field_type, array( 'number', 'scale' ) ) ? ' +0 ' : ''; |
||
| 38 | } |
||
| 39 | |||
| 40 | $order = self::get_param( array( 'param' => 'order', 'default' => 'DESC' ) ); |
||
| 41 | $order = FrmDb::esc_order( $orderby . ' ' . $order ); |
||
| 42 | |||
| 43 | $page = $this->get_pagenum(); |
||
| 44 | $start = (int) self::get_param( array( 'param' => 'start', 'default' => ( ( $page - 1 ) * $per_page ) ) ); |
||
| 45 | |||
| 46 | $limit = FrmDb::esc_limit( $start . ',' . $per_page ); |
||
| 47 | $this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query ); |
||
| 48 | $total_items = FrmEntry::getRecordCount($s_query); |
||
| 49 | |||
| 50 | $this->set_pagination_args( array( |
||
| 51 | 'total_items' => $total_items, |
||
| 52 | 'per_page' => $per_page, |
||
| 53 | ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function no_items() { |
||
| 57 | $s = self::get_param( array( 'param' => 's', 'sanitize' => 'sanitize_text_field' ) ); |
||
| 58 | if ( ! empty($s) ) { |
||
| 59 | _e( 'No Entries Found', 'formidable' ); |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $form_id = $this->params['form']; |
||
| 64 | $form = $this->params['form']; |
||
| 65 | |||
| 66 | if ( $form_id ) { |
||
| 67 | $form = FrmForm::getOne($form_id); |
||
| 68 | } |
||
| 69 | $colspan = $this->get_column_count(); |
||
| 70 | |||
| 71 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' ); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function search_box( $text, $input_id ) { |
||
| 77 | |||
| 78 | protected function extra_tablenav( $which ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Gets the name of the primary column in the Entries screen |
||
| 90 | * |
||
| 91 | * @since 2.0.14 |
||
| 92 | * |
||
| 93 | * @return string $primary_column |
||
| 94 | */ |
||
| 95 | protected function get_primary_column_name() { |
||
| 110 | |||
| 111 | public function single_row( $item, $style = '' ) { |
||
| 173 | |||
| 174 | private function column_value( $item ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $view_link |
||
| 220 | */ |
||
| 221 | private function get_actions( &$actions, $item, $view_link ) { |
||
| 231 | |||
| 232 | private function get_column_value( $item, &$val ) { |
||
| 268 | } |
||
| 269 |