Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Module { |
||
| 21 | /** |
||
| 22 | * Number of items per chunk when grouping objects for performance reasons. |
||
| 23 | * |
||
| 24 | * @access public |
||
| 25 | * |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | const ARRAY_CHUNK_SIZE = 10; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Sync module name. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | abstract public function name(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The id field in the database. |
||
| 41 | * |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function id_field() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The table in the database. |
||
| 52 | * |
||
| 53 | * @access public |
||
| 54 | * |
||
| 55 | * @return string|bool |
||
| 56 | */ |
||
| 57 | public function table_name() { |
||
| 60 | |||
| 61 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Retrieve a sync object by its ID. |
||
| 65 | * |
||
| 66 | * @access public |
||
| 67 | * |
||
| 68 | * @param string $object_type Type of the sync object. |
||
| 69 | * @param int $id ID of the sync object. |
||
| 70 | * @return mixed Object, or false if the object is invalid. |
||
| 71 | */ |
||
| 72 | public function get_object_by_id( $object_type, $id ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialize callables action listeners. |
||
| 78 | * Override these to set up listeners and set/reset data/defaults. |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @param callable $callable Action handler callable. |
||
| 83 | */ |
||
| 84 | public function init_listeners( $callable ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Initialize module action listeners for full sync. |
||
| 89 | * |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @param callable $callable Action handler callable. |
||
| 93 | */ |
||
| 94 | public function init_full_sync_listeners( $callable ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize the module in the sender. |
||
| 99 | * |
||
| 100 | * @access public |
||
| 101 | */ |
||
| 102 | public function init_before_send() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set module defaults. |
||
| 107 | * |
||
| 108 | * @access public |
||
| 109 | */ |
||
| 110 | public function set_defaults() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Perform module cleanup. |
||
| 115 | * Usually triggered when uninstalling the plugin. |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | */ |
||
| 119 | public function reset_data() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Enqueue the module actions for full sync. |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | * |
||
| 127 | * @param array $config Full sync configuration for this sync module. |
||
| 128 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 129 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 130 | * @return array Number of actions enqueued, and next module state. |
||
| 131 | */ |
||
| 132 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 139 | * |
||
| 140 | * @access public |
||
| 141 | * |
||
| 142 | * @param array $config Full sync configuration for this sync module. |
||
| 143 | * @return array Number of items yet to be enqueued. |
||
| 144 | */ |
||
| 145 | public function estimate_full_sync_actions( $config ) { |
||
| 149 | |||
| 150 | // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 154 | * |
||
| 155 | * @access public |
||
| 156 | * |
||
| 157 | * @return array Full sync actions of this module. |
||
| 158 | */ |
||
| 159 | public function get_full_sync_actions() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the number of actions that we care about. |
||
| 165 | * |
||
| 166 | * @access protected |
||
| 167 | * |
||
| 168 | * @param array $action_names Action names we're interested in. |
||
| 169 | * @param array $actions_to_count Unfiltered list of actions we want to count. |
||
| 170 | * @return array Number of actions that we're interested in. |
||
| 171 | */ |
||
| 172 | protected function count_actions( $action_names, $actions_to_count ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Calculate the checksum of one or more values. |
||
| 178 | * |
||
| 179 | * @access protected |
||
| 180 | * |
||
| 181 | * @param mixed $values Values to calculate checksum for. |
||
| 182 | * @param bool $sort If $values should have ksort called on it. |
||
| 183 | * @return int The checksum. |
||
| 184 | */ |
||
| 185 | protected function get_check_sum( $values, $sort = true ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Recursively call ksort on an Array |
||
| 195 | * |
||
| 196 | * @param array $values Array. |
||
| 197 | */ |
||
| 198 | private function recursive_ksort( &$values ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Whether a particular checksum in a set of checksums is valid. |
||
| 209 | * |
||
| 210 | * @access protected |
||
| 211 | * |
||
| 212 | * @param array $sums_to_check Array of checksums. |
||
| 213 | * @param string $name Name of the checksum. |
||
| 214 | * @param int $new_sum Checksum to compare against. |
||
| 215 | * @return boolean Whether the checksum is valid. |
||
| 216 | */ |
||
| 217 | protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Enqueue all items of a sync type as an action. |
||
| 227 | * |
||
| 228 | * @access protected |
||
| 229 | * |
||
| 230 | * @param string $action_name Name of the action. |
||
| 231 | * @param string $table_name Name of the database table. |
||
| 232 | * @param string $id_field Name of the ID field in the database. |
||
| 233 | * @param string $where_sql The SQL WHERE clause to filter to the desired items. |
||
| 234 | * @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
||
| 235 | * @param boolean $state Whether enqueueing has finished. |
||
| 236 | * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
||
| 237 | */ |
||
| 238 | protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Given the Module Full Sync Configuration and Status return the next chunk of items to send. |
||
| 287 | * |
||
| 288 | * @param array $config This module Full Sync configuration. |
||
| 289 | * @param array $status This module Full Sync status. |
||
| 290 | * @param int $chunk_size Chunk size. |
||
| 291 | * |
||
| 292 | * @return array|object|null |
||
| 293 | */ |
||
| 294 | public function get_next_chunk( $config, $status, $chunk_size ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Return the initial last sent object. |
||
| 312 | * |
||
| 313 | * @return string|array initial status. |
||
| 314 | */ |
||
| 315 | public function get_initial_last_sent() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Immediately send all items of a sync type as an action. |
||
| 321 | * |
||
| 322 | * @access protected |
||
| 323 | * |
||
| 324 | * @param string $config Full sync configuration for this module. |
||
| 325 | * @param array $status the current module full sync status. |
||
| 326 | * @param float $send_until timestamp until we want this request to send full sync events. |
||
| 327 | * |
||
| 328 | * @return array Status, the module full sync status updated. |
||
| 329 | */ |
||
| 330 | public function send_full_sync_actions( $config, $status, $send_until ) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * Immediately sends a single item without firing or enqueuing it |
||
| 366 | * |
||
| 367 | * @param string $action_name The action. |
||
| 368 | * @param array $data The data associated with the action. |
||
| 369 | */ |
||
| 370 | public function send_action( $action_name, $data = null ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Retrieve chunk IDs with previous interval end. |
||
| 377 | * |
||
| 378 | * @access protected |
||
| 379 | * |
||
| 380 | * @param array $chunks All remaining items. |
||
| 381 | * @param int $previous_interval_end The last item from the previous interval. |
||
| 382 | * @return array Chunk IDs with the previous interval end. |
||
| 383 | */ |
||
| 384 | protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get metadata of a particular object type within the designated meta key whitelist. |
||
| 399 | * |
||
| 400 | * @access protected |
||
| 401 | * |
||
| 402 | * @todo Refactor to use $wpdb->prepare() on the SQL query. |
||
| 403 | * |
||
| 404 | * @param array $ids Object IDs. |
||
| 405 | * @param string $meta_type Meta type. |
||
| 406 | * @param array $meta_key_whitelist Meta key whitelist. |
||
| 407 | * @return array Unserialized meta values. |
||
| 408 | */ |
||
| 409 | protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Initialize listeners for the particular meta type. |
||
| 433 | * |
||
| 434 | * @access public |
||
| 435 | * |
||
| 436 | * @param string $meta_type Meta type. |
||
| 437 | * @param callable $callable Action handler callable. |
||
| 438 | */ |
||
| 439 | public function init_listeners_for_meta_type( $meta_type, $callable ) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Initialize meta whitelist handler for the particular meta type. |
||
| 447 | * |
||
| 448 | * @access public |
||
| 449 | * |
||
| 450 | * @param string $meta_type Meta type. |
||
| 451 | * @param callable $whitelist_handler Action handler callable. |
||
| 452 | */ |
||
| 453 | public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Retrieve the term relationships for the specified object IDs. |
||
| 461 | * |
||
| 462 | * @access protected |
||
| 463 | * |
||
| 464 | * @todo This feels too specific to be in the abstract sync Module class. Move it? |
||
| 465 | * |
||
| 466 | * @param array $ids Object IDs. |
||
| 467 | * @return array Term relationships - object ID and term taxonomy ID pairs. |
||
| 468 | */ |
||
| 469 | protected function get_term_relationships( $ids ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Unserialize the value of a meta object, if necessary. |
||
| 478 | * |
||
| 479 | * @access public |
||
| 480 | * |
||
| 481 | * @param object $meta Meta object. |
||
| 482 | * @return object Meta object with possibly unserialized value. |
||
| 483 | */ |
||
| 484 | public function unserialize_meta( $meta ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Retrieve a set of objects by their IDs. |
||
| 491 | * |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param string $object_type Object type. |
||
| 495 | * @param array $ids Object IDs. |
||
| 496 | * @return array Array of objects. |
||
| 497 | */ |
||
| 498 | public function get_objects_by_id( $object_type, $ids ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
| 518 | * |
||
| 519 | * @access public |
||
| 520 | * |
||
| 521 | * @param int $batch_size The batch size for objects. |
||
| 522 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
| 523 | * |
||
| 524 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
| 525 | */ |
||
| 526 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Return Total number of objects. |
||
| 577 | * |
||
| 578 | * @param array $config Full Sync config. |
||
| 579 | * |
||
| 580 | * @return int total |
||
| 581 | */ |
||
| 582 | public function total( $config ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 593 | * |
||
| 594 | * @access public |
||
| 595 | * |
||
| 596 | * @param array $config Full sync configuration for this sync module. |
||
| 597 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 598 | */ |
||
| 599 | public function get_where_sql( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 602 | |||
| 603 | } |
||
| 604 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: