Complex classes like View_Table_Template 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 View_Table_Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class View_Table_Template extends View_Template { |
||
15 | /** |
||
16 | * @var string The template slug to be loaded (like "table", "list") |
||
17 | */ |
||
18 | public static $slug = 'table'; |
||
19 | |||
20 | |||
21 | /** |
||
22 | * Constructor. Add filters to modify output. |
||
23 | * |
||
24 | * @since 2.0.4 |
||
25 | * |
||
26 | * @param View $view |
||
27 | * @param Entry_Collection $entries |
||
28 | * @param Request $request |
||
29 | */ |
||
30 | 19 | public function __construct( View $view, Entry_Collection $entries, Request $request ) { |
|
36 | |||
37 | /** |
||
38 | * Add sorting links to HTML columns that support sorting |
||
39 | * |
||
40 | * @since 2.0.4 |
||
41 | * @since 2.0.5 Made static |
||
42 | * |
||
43 | * @static |
||
44 | * |
||
45 | * @param string $column_label Label for the table column |
||
46 | * @param \GV\Template_Context $context |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 29 | static public function add_columns_sort_links( $column_label, $context = null ) { |
|
51 | |||
52 | 29 | $sort_columns = $context->view->settings->get( 'sort_columns' ); |
|
53 | |||
54 | 29 | if ( empty( $sort_columns ) ) { |
|
55 | 28 | return $column_label; |
|
56 | } |
||
57 | |||
58 | 1 | if ( ! \GravityView_frontend::getInstance()->is_field_sortable( $context->field->ID, $context->view->form->form ) ) { |
|
59 | return $column_label; |
||
60 | } |
||
61 | |||
62 | 1 | $sorting = array(); |
|
63 | |||
64 | 1 | $directions = $context->view->settings->get( 'sort_direction' ); |
|
65 | |||
66 | 1 | $sorts = Utils::_GET( 'sort' ); |
|
67 | |||
68 | 1 | if ( $sorts ) { |
|
69 | 1 | if ( is_array( $sorts ) ) { |
|
70 | 1 | foreach ( (array)$sorts as $key => $direction ) { |
|
71 | 1 | if ( $key == $context->field->ID ) { |
|
72 | 1 | $sorting['key'] = $context->field->ID; |
|
73 | 1 | $sorting['direction'] = strtolower( $direction ); |
|
74 | 1 | break; |
|
75 | } |
||
76 | } |
||
77 | } else { |
||
78 | if ( $sorts == $context->field->ID ) { |
||
79 | $sorting['key'] = $context->field->ID; |
||
80 | 1 | $sorting['direction'] = strtolower( Utils::_GET( 'dir', '' ) ); |
|
81 | } |
||
82 | } |
||
83 | } else { |
||
84 | foreach ( (array)$context->view->settings->get( 'sort_field', array() ) as $i => $sort_field ) { |
||
85 | if ( $sort_field == $context->field->ID ) { |
||
86 | $sorting['key'] = $sort_field; |
||
87 | $sorting['direction'] = strtolower( Utils::get( $directions, $i, '' ) ); |
||
88 | break; // Only get the first sort |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | 1 | $class = 'gv-sort'; |
|
94 | |||
95 | 1 | $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $context->field->ID, $context->view->form->ID ); |
|
96 | |||
97 | $sort_args = array( |
||
98 | 1 | sprintf( 'sort[%s]', $context->field->ID ), |
|
99 | 1 | 'asc' |
|
100 | ); |
||
101 | |||
102 | // If we are already sorting by the current field... |
||
103 | 1 | if ( ! empty( $sorting['key'] ) && (string) $sort_field_id === (string) $sorting['key'] ) { |
|
104 | |||
105 | 1 | switch( $sorting['direction'] ) { |
|
106 | // No sort |
||
107 | 1 | case '': |
|
108 | 1 | $sort_args[1] = 'asc'; |
|
109 | 1 | $class .= ' gv-icon-caret-up-down'; |
|
110 | 1 | break; |
|
111 | 1 | case 'desc': |
|
112 | 1 | $sort_args[1] = ''; |
|
113 | 1 | $class .= ' gv-icon-sort-asc'; |
|
114 | 1 | break; |
|
115 | 1 | case 'asc': |
|
116 | default: |
||
117 | 1 | $sort_args[1] = 'desc'; |
|
118 | 1 | $class .= ' gv-icon-sort-desc'; |
|
119 | 1 | break; |
|
120 | } |
||
121 | |||
122 | } else { |
||
123 | $class .= ' gv-icon-caret-up-down'; |
||
124 | } |
||
125 | |||
126 | 1 | $url = remove_query_arg( array( 'pagenum' ) ); |
|
127 | 1 | $url = remove_query_arg( 'sort', $url ); |
|
128 | 1 | $multisort_url = self::_get_multisort_url( $url, $sort_args, $context->field->ID ); |
|
129 | |||
130 | 1 | $url = add_query_arg( $sort_args[0], $sort_args[1], $url ); |
|
131 | |||
132 | 1 | $return = '<a href="'. esc_url_raw( $url ) .'"'; |
|
133 | |||
134 | 1 | if ( ! empty( $multisort_url ) ) { |
|
135 | 1 | $return .= ' data-multisort-href="'. esc_url_raw( $multisort_url ) . '"'; |
|
136 | } |
||
137 | |||
138 | 1 | $return .= ' class="'. $class .'" ></a> '. $column_label; |
|
139 | |||
140 | 1 | return $return; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the multi-sort URL used in the sorting links |
||
145 | * |
||
146 | * @todo Consider moving to Utils? |
||
147 | * |
||
148 | * @since 2.3 |
||
149 | * |
||
150 | * @see add_columns_sort_links |
||
151 | * @param string $url Single-sort URL |
||
152 | * @param array $sort_args Single sorting for rules, in [ field_id, dir ] format |
||
153 | * @param string|int $field_id ID of the current field being displayed |
||
154 | * |
||
155 | * @return string Multisort URL, if there are multiple sorts. Otherwise, existing $url |
||
156 | */ |
||
157 | 2 | static public function _get_multisort_url( $url, $sort_args, $field_id ) { |
|
158 | |||
159 | 2 | $sorts = Utils::_GET( 'sort' ); |
|
160 | |||
161 | 2 | if ( ! is_array( $sorts ) ) { |
|
162 | 1 | return $url; |
|
163 | } |
||
164 | |||
165 | 2 | $multisort_url = $url; |
|
166 | |||
167 | // If the field has already been sorted by, add the field to the URL |
||
168 | 2 | if ( ! in_array( $field_id, $keys = array_keys( $sorts ) ) ) { |
|
169 | 1 | if ( count( $keys ) ) { |
|
170 | 1 | $multisort_url = add_query_arg( sprintf( 'sort[%s]', end( $keys ) ), $sorts[ end( $keys ) ], $multisort_url ); |
|
171 | 1 | $multisort_url = add_query_arg( $sort_args[0], $sort_args[1], $multisort_url ); |
|
172 | } else { |
||
173 | 1 | $multisort_url = add_query_arg( $sort_args[0], $sort_args[1], $multisort_url ); |
|
174 | } |
||
175 | } |
||
176 | // Otherwise, we are just updating the sort order |
||
177 | else { |
||
178 | |||
179 | // Pass empty value to unset |
||
180 | 2 | if( '' === $sort_args[1] ) { |
|
181 | 1 | unset( $sorts[ $field_id ] ); |
|
182 | } else { |
||
183 | 2 | $sorts[ $field_id ] = $sort_args[1]; |
|
184 | } |
||
185 | |||
186 | 2 | $multisort_url = add_query_arg( array( 'sort' => $sorts ), $multisort_url ); |
|
187 | } |
||
188 | |||
189 | 2 | return $multisort_url; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Output the table column names. |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | 20 | public function the_columns() { |
|
215 | |||
216 | /** |
||
217 | * Returns the label for a column, with support for all deprecated filters |
||
218 | * |
||
219 | * @since 2.1 |
||
220 | * |
||
221 | * @param \GV\Field $field |
||
222 | * @param \GV\Template_Context $context |
||
223 | */ |
||
224 | 19 | protected static function get_field_column_label( $field, $context = null ) { |
|
244 | |||
245 | /** |
||
246 | * Output the entry row. |
||
247 | * |
||
248 | * @param \GV\Entry $entry The entry to be rendered. |
||
249 | * @param array $attributes The attributes for the <tr> tag |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | 17 | public function the_entry( \GV\Entry $entry, $attributes ) { |
|
352 | |||
353 | /** |
||
354 | * Output a field cell. |
||
355 | * |
||
356 | * @param \GV\Field $field The field to be ouput. |
||
357 | * @param \GV\Field $entry The entry this field is for. |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | 16 | public function the_field( \GV\Field $field, \GV\Entry $entry ) { |
|
402 | |||
403 | /** |
||
404 | * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions. |
||
405 | * |
||
406 | * Output inside the `tbody` of the table. |
||
407 | * |
||
408 | * @param $context \GV\Template_Context The 2.0 context. |
||
409 | * |
||
410 | * @return void |
||
411 | */ |
||
412 | 19 | public static function body_before( $context ) { |
|
428 | |||
429 | /** |
||
430 | * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions. |
||
431 | * |
||
432 | * Output inside the `tbody` of the table. |
||
433 | * |
||
434 | * @param $context \GV\Template_Context The 2.0 context. |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | 19 | public static function body_after( $context ) { |
|
454 | |||
455 | /** |
||
456 | * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions. |
||
457 | * |
||
458 | * Output inside the `tr` of the table. |
||
459 | * |
||
460 | * @param $context \GV\Template_Context The 2.0 context. |
||
461 | * |
||
462 | * @return void |
||
463 | */ |
||
464 | 6 | public static function tr_before( $context ) { |
|
480 | |||
481 | /** |
||
482 | * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions. |
||
483 | * |
||
484 | * Output inside the `tr` of the table. |
||
485 | * |
||
486 | * @param $context \GV\Template_Context The 2.0 context. |
||
487 | * |
||
488 | * @return void |
||
489 | */ |
||
490 | 6 | public static function tr_after( $context ) { |
|
506 | |||
507 | /** |
||
508 | * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters. |
||
509 | * |
||
510 | * Modify of the class of a row. |
||
511 | * |
||
512 | * @param string $class The class. |
||
513 | * @param \GV\Entry $entry The entry. |
||
514 | * @param \GV\Template_Context The context. |
||
515 | * |
||
516 | * @return string The classes. |
||
517 | */ |
||
518 | 16 | public static function entry_class( $class, $entry, $context ) { |
|
537 | } |
||
538 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.