Complex classes like Give_Sales_Log_Table 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 Give_Sales_Log_Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Give_Sales_Log_Table extends WP_List_Table { |
||
| 29 | /** |
||
| 30 | * Number of results to show per page |
||
| 31 | * |
||
| 32 | * @since 1.0 |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | public $per_page = 30; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get things started |
||
| 39 | * |
||
| 40 | * @since 1.0 |
||
| 41 | * @see WP_List_Table::__construct() |
||
| 42 | */ |
||
| 43 | public function __construct() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This function renders most of the columns in the list table. |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * @since 1.0 |
||
| 61 | * |
||
| 62 | * @param array $item Contains all the data of the discount code |
||
| 63 | * @param string $column_name The name of the column |
||
| 64 | * |
||
| 65 | * @return string Column Name |
||
| 66 | */ |
||
| 67 | public function column_default( $item, $column_name ) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Retrieve the table columns |
||
| 119 | * |
||
| 120 | * @access public |
||
| 121 | * @since 1.0 |
||
| 122 | * @return array $columns Array of all the list table columns |
||
| 123 | */ |
||
| 124 | public function get_columns() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve the current page number |
||
| 140 | * |
||
| 141 | * @access public |
||
| 142 | * @since 1.0 |
||
| 143 | * @return int Current page number |
||
| 144 | */ |
||
| 145 | public function get_paged() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Retrieves the user we are filtering logs by, if any |
||
| 151 | * |
||
| 152 | * @access public |
||
| 153 | * @since 1.0 |
||
| 154 | * @return mixed int If User ID, string If Email/Login |
||
| 155 | */ |
||
| 156 | public function get_filtered_user() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Retrieves the ID of the give_form we're filtering logs by |
||
| 162 | * |
||
| 163 | * @access public |
||
| 164 | * @since 1.0 |
||
| 165 | * @return int Download ID |
||
| 166 | */ |
||
| 167 | public function get_filtered_give_form() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Retrieves the search query string |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * @since 1.0 |
||
| 176 | * @return string|bool string If search is present, false otherwise |
||
| 177 | */ |
||
| 178 | public function get_search() { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Display Tablenav (extended) |
||
| 185 | * |
||
| 186 | * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear |
||
| 187 | * |
||
| 188 | * @see : https://github.com/WordImpress/Give/issues/564 |
||
| 189 | * |
||
| 190 | * @since 1.4.1 |
||
| 191 | * @access protected |
||
| 192 | * |
||
| 193 | * @param string $which |
||
| 194 | */ |
||
| 195 | protected function display_tablenav( $which ) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Gets the meta query for the log query |
||
| 219 | * |
||
| 220 | * This is used to return log entries that match our search query, user query, or form query |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * @since 1.0 |
||
| 224 | * @return array $meta_query |
||
| 225 | */ |
||
| 226 | public function get_meta_query() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Outputs the log views |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * @since 1.0 |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | function bulk_actions( $which = '' ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Sets up the forms filter |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * @since 1.0 |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | public function give_forms_filter() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Gets the log entries for the current view |
||
| 333 | * |
||
| 334 | * @access public |
||
| 335 | * @since 1.0 |
||
| 336 | * |
||
| 337 | * @return array $logs_data Array of all the Log entires |
||
| 338 | */ |
||
| 339 | public function get_logs() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Setup the final data for the table |
||
| 381 | * |
||
| 382 | * @access public |
||
| 383 | * @since 1.0 |
||
| 384 | * @uses Give_Sales_Log_Table::get_columns() |
||
| 385 | * @uses WP_List_Table::get_sortable_columns() |
||
| 386 | * @uses Give_Sales_Log_Table::get_pagenum() |
||
| 387 | * @uses Give_Sales_Log_Table::get_logs() |
||
| 388 | * @uses Give_Sales_Log_Table::get_log_count() |
||
| 389 | * |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | public function prepare_items() { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Get log query param. |
||
| 412 | * |
||
| 413 | * @since 2.0 |
||
| 414 | * @access public |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | public function get_query_params() { |
||
| 438 | } |
||
| 439 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.