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 ActionScheduler_ListTable 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 ActionScheduler_ListTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class ActionScheduler_ListTable extends ActionScheduler_Abstract_ListTable { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * The package name. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $package = 'action-scheduler'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Columns to show (name => label). |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $columns = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Actions (name => label). |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $row_actions = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The active data stores |
||
| 32 | * |
||
| 33 | * @var ActionScheduler_Store |
||
| 34 | */ |
||
| 35 | protected $store; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A logger to use for getting action logs to display |
||
| 39 | * |
||
| 40 | * @var ActionScheduler_Logger |
||
| 41 | */ |
||
| 42 | protected $logger; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * A ActionScheduler_QueueRunner runner instance (or child class) |
||
| 46 | * |
||
| 47 | * @var ActionScheduler_QueueRunner |
||
| 48 | */ |
||
| 49 | protected $runner; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Bulk actions. The key of the array is the method name of the implementation: |
||
| 53 | * |
||
| 54 | * bulk_<key>(array $ids, string $sql_in). |
||
| 55 | * |
||
| 56 | * See the comments in the parent class for further details |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $bulk_actions = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Flag variable to render our notifications, if any, once. |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | protected static $did_notification = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Array of seconds for common time periods, like week or month, alongside an internationalised string representation, i.e. "Day" or "Days" |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $time_periods; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sets the current data store object into `store->action` and initialises the object. |
||
| 78 | * |
||
| 79 | * @param ActionScheduler_Store $store |
||
| 80 | * @param ActionScheduler_Logger $logger |
||
| 81 | * @param ActionScheduler_QueueRunner $runner |
||
| 82 | */ |
||
| 83 | public function __construct( ActionScheduler_Store $store, ActionScheduler_Logger $logger, ActionScheduler_QueueRunner $runner ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Convert an interval of seconds into a two part human friendly string. |
||
| 180 | * |
||
| 181 | * The WordPress human_time_diff() function only calculates the time difference to one degree, meaning |
||
| 182 | * even if an action is 1 day and 11 hours away, it will display "1 day". This function goes one step |
||
| 183 | * further to display two degrees of accuracy. |
||
| 184 | * |
||
| 185 | * Inspired by the Crontrol::interval() function by Edward Dale: https://wordpress.org/plugins/wp-crontrol/ |
||
| 186 | * |
||
| 187 | * @param int $interval A interval in seconds. |
||
| 188 | * @param int $periods_to_include Depth of time periods to include, e.g. for an interval of 70, and $periods_to_include of 2, both minutes and seconds would be included. With a value of 1, only minutes would be included. |
||
| 189 | * @return string A human friendly string representation of the interval. |
||
| 190 | */ |
||
| 191 | private static function human_interval( $interval, $periods_to_include = 2 ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the recurrence of an action or 'Non-repeating'. The output is human readable. |
||
| 218 | * |
||
| 219 | * @param ActionScheduler_Action $action |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | protected function get_recurrence( $action ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Serializes the argument of an action to render it in a human friendly format. |
||
| 240 | * |
||
| 241 | * @param array $row The array representation of the current row of the table |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function column_args( array $row ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
||
| 261 | * |
||
| 262 | * @param array $row Action array. |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function column_log_entries( array $row ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Prints the logs entries inline. We do so to avoid loading Javascript and other hacks to show it in a modal. |
||
| 282 | * |
||
| 283 | * @param ActionScheduler_LogEntry $log_entry |
||
| 284 | * @param DateTimezone $timezone |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function get_log_entry_html( ActionScheduler_LogEntry $log_entry, DateTimezone $timezone ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Only display row actions for pending actions. |
||
| 295 | * |
||
| 296 | * @param array $row Row to render |
||
| 297 | * @param string $column_name Current row |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function maybe_render_actions( $row, $column_name ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Renders admin notifications |
||
| 311 | * |
||
| 312 | * Notifications: |
||
| 313 | * 1. When the maximum number of tasks are being executed simultaneously |
||
| 314 | * 2. Notifications when a task us manually executed |
||
| 315 | */ |
||
| 316 | public function display_admin_notices() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Prints the scheduled date in a human friendly format. |
||
| 363 | * |
||
| 364 | * @param array $row The array representation of the current row of the table |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function column_schedule( $row ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the scheduled date in a human friendly format. |
||
| 374 | * |
||
| 375 | * @param ActionScheduler_Schedule $schedule |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | protected function get_schedule_display_string( ActionScheduler_Schedule $schedule ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Bulk delete |
||
| 402 | * |
||
| 403 | * Deletes actions based on their ID. This is the handler for the bulk delete. It assumes the data |
||
| 404 | * properly validated by the callee and it will delete the actions without any extra validation. |
||
| 405 | * |
||
| 406 | * @param array $ids |
||
| 407 | * @param string $ids_sql Inherited and unused |
||
| 408 | */ |
||
| 409 | protected function bulk_delete( array $ids, $ids_sql ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
||
| 417 | * parameters are valid. |
||
| 418 | * |
||
| 419 | * @param int $action_id |
||
| 420 | */ |
||
| 421 | protected function row_action_cancel( $action_id ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Implements the logic behind running an action. ActionScheduler_Abstract_ListTable validates the request and their |
||
| 427 | * parameters are valid. |
||
| 428 | * |
||
| 429 | * @param int $action_id |
||
| 430 | */ |
||
| 431 | protected function row_action_run( $action_id ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Implements the logic behind processing an action once an action link is clicked on the list table. |
||
| 437 | * |
||
| 438 | * @param int $action_id |
||
| 439 | * @param string $row_action_type The type of action to perform on the action. |
||
| 440 | */ |
||
| 441 | protected function process_row_action( $action_id, $row_action_type ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritDoc} |
||
| 463 | */ |
||
| 464 | public function prepare_items() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Prints the available statuses so the user can click to filter. |
||
| 521 | */ |
||
| 522 | protected function display_filter_by_status() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get the text to display in the search box on the list table. |
||
| 529 | */ |
||
| 530 | protected function get_search_box_button_text() { |
||
| 533 | } |
||
| 534 |