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