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 | * An estimate of how many rows per second can be synced during a full sync. |
||
| 33 | * |
||
| 34 | * @access public |
||
| 35 | * |
||
| 36 | * @var int|null Null if speed is not important in a full sync. |
||
| 37 | */ |
||
| 38 | public $sync_speed = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sync module name. |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | abstract public function name(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The id field in the database. |
||
| 51 | * |
||
| 52 | * @access public |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function id_field() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The table in the database. |
||
| 62 | * |
||
| 63 | * @access public |
||
| 64 | * |
||
| 65 | * @return string|bool |
||
| 66 | */ |
||
| 67 | public function table_name() { |
||
| 70 | |||
| 71 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Retrieve a sync object by its ID. |
||
| 75 | * |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param string $object_type Type of the sync object. |
||
| 79 | * @param int $id ID of the sync object. |
||
| 80 | * @return mixed Object, or false if the object is invalid. |
||
| 81 | */ |
||
| 82 | public function get_object_by_id( $object_type, $id ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Initialize callables action listeners. |
||
| 88 | * Override these to set up listeners and set/reset data/defaults. |
||
| 89 | * |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @param callable $callable Action handler callable. |
||
| 93 | */ |
||
| 94 | public function init_listeners( $callable ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize module action listeners for full sync. |
||
| 99 | * |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @param callable $callable Action handler callable. |
||
| 103 | */ |
||
| 104 | public function init_full_sync_listeners( $callable ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initialize the module in the sender. |
||
| 109 | * |
||
| 110 | * @access public |
||
| 111 | */ |
||
| 112 | public function init_before_send() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set module defaults. |
||
| 117 | * |
||
| 118 | * @access public |
||
| 119 | */ |
||
| 120 | public function set_defaults() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Perform module cleanup. |
||
| 125 | * Usually triggered when uninstalling the plugin. |
||
| 126 | * |
||
| 127 | * @access public |
||
| 128 | */ |
||
| 129 | public function reset_data() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Enqueue the module actions for full sync. |
||
| 134 | * |
||
| 135 | * @access public |
||
| 136 | * |
||
| 137 | * @param array $config Full sync configuration for this sync module. |
||
| 138 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 139 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 140 | * @return array Number of actions enqueued, and next module state. |
||
| 141 | */ |
||
| 142 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * |
||
| 152 | * @param array $config Full sync configuration for this sync module. |
||
| 153 | * @return array Number of items yet to be enqueued. |
||
| 154 | */ |
||
| 155 | public function estimate_full_sync_actions( $config ) { |
||
| 159 | |||
| 160 | // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 164 | * |
||
| 165 | * @access public |
||
| 166 | * |
||
| 167 | * @return array Full sync actions of this module. |
||
| 168 | */ |
||
| 169 | public function get_full_sync_actions() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the number of actions that we care about. |
||
| 175 | * |
||
| 176 | * @access protected |
||
| 177 | * |
||
| 178 | * @param array $action_names Action names we're interested in. |
||
| 179 | * @param array $actions_to_count Unfiltered list of actions we want to count. |
||
| 180 | * @return array Number of actions that we're interested in. |
||
| 181 | */ |
||
| 182 | protected function count_actions( $action_names, $actions_to_count ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Calculate the checksum of one or more values. |
||
| 188 | * |
||
| 189 | * @access protected |
||
| 190 | * |
||
| 191 | * @param mixed $values Values to calculate checksum for. |
||
| 192 | * @return int The checksum. |
||
| 193 | */ |
||
| 194 | protected function get_check_sum( $values ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Whether a particular checksum in a set of checksums is valid. |
||
| 200 | * |
||
| 201 | * @access protected |
||
| 202 | * |
||
| 203 | * @param array $sums_to_check Array of checksums. |
||
| 204 | * @param string $name Name of the checksum. |
||
| 205 | * @param int $new_sum Checksum to compare against. |
||
| 206 | * @return boolean Whether the checksum is valid. |
||
| 207 | */ |
||
| 208 | protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Enqueue all items of a sync type as an action. |
||
| 218 | * |
||
| 219 | * @access protected |
||
| 220 | * |
||
| 221 | * @param string $action_name Name of the action. |
||
| 222 | * @param string $table_name Name of the database table. |
||
| 223 | * @param string $id_field Name of the ID field in the database. |
||
| 224 | * @param string $where_sql The SQL WHERE clause to filter to the desired items. |
||
| 225 | * @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
||
| 226 | * @param boolean $state Whether enqueueing has finished. |
||
| 227 | * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
||
| 228 | */ |
||
| 229 | protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Given the Module Full Sync Configuration and Status return the next chunk of items to send. |
||
| 278 | * |
||
| 279 | * @param array $config This module Full Sync configuration. |
||
| 280 | * @param array $status This module Full Sync status. |
||
| 281 | * @param int $chunk_size Chunk size. |
||
| 282 | * |
||
| 283 | * @return array|object|null |
||
| 284 | */ |
||
| 285 | public function get_next_chunk( $config, $status, $chunk_size ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return the initial last sent object. |
||
| 301 | * |
||
| 302 | * @return string|array initial status. |
||
| 303 | */ |
||
| 304 | public function get_initial_last_sent() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Immediately send all items of a sync type as an action. |
||
| 310 | * |
||
| 311 | * @access protected |
||
| 312 | * |
||
| 313 | * @param string $config Full sync configuration for this module. |
||
| 314 | * @param array $status the current module full sync status. |
||
| 315 | * @param float $send_until timestamp until we want this request to send full sync events. |
||
| 316 | * |
||
| 317 | * @return array Status, the module full sync status updated. |
||
| 318 | */ |
||
| 319 | public function send_full_sync_actions( $config, $status, $send_until ) { |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Immediately sends a single item without firing or enqueuing it |
||
| 355 | * |
||
| 356 | * @param string $action_name The action. |
||
| 357 | * @param array $data The data associated with the action. |
||
| 358 | */ |
||
| 359 | public function send_action( $action_name, $data = null ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Retrieve chunk IDs with previous interval end. |
||
| 366 | * |
||
| 367 | * @access protected |
||
| 368 | * |
||
| 369 | * @param array $chunks All remaining items. |
||
| 370 | * @param int $previous_interval_end The last item from the previous interval. |
||
| 371 | * @return array Chunk IDs with the previous interval end. |
||
| 372 | */ |
||
| 373 | protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get metadata of a particular object type within the designated meta key whitelist. |
||
| 388 | * |
||
| 389 | * @access protected |
||
| 390 | * |
||
| 391 | * @todo Refactor to use $wpdb->prepare() on the SQL query. |
||
| 392 | * |
||
| 393 | * @param array $ids Object IDs. |
||
| 394 | * @param string $meta_type Meta type. |
||
| 395 | * @param array $meta_key_whitelist Meta key whitelist. |
||
| 396 | * @return array Unserialized meta values. |
||
| 397 | */ |
||
| 398 | protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Initialize listeners for the particular meta type. |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * |
||
| 425 | * @param string $meta_type Meta type. |
||
| 426 | * @param callable $callable Action handler callable. |
||
| 427 | */ |
||
| 428 | public function init_listeners_for_meta_type( $meta_type, $callable ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Initialize meta whitelist handler for the particular meta type. |
||
| 436 | * |
||
| 437 | * @access public |
||
| 438 | * |
||
| 439 | * @param string $meta_type Meta type. |
||
| 440 | * @param callable $whitelist_handler Action handler callable. |
||
| 441 | */ |
||
| 442 | public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Retrieve the term relationships for the specified object IDs. |
||
| 450 | * |
||
| 451 | * @access protected |
||
| 452 | * |
||
| 453 | * @todo This feels too specific to be in the abstract sync Module class. Move it? |
||
| 454 | * |
||
| 455 | * @param array $ids Object IDs. |
||
| 456 | * @return array Term relationships - object ID and term taxonomy ID pairs. |
||
| 457 | */ |
||
| 458 | protected function get_term_relationships( $ids ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Unserialize the value of a meta object, if necessary. |
||
| 467 | * |
||
| 468 | * @access public |
||
| 469 | * |
||
| 470 | * @param object $meta Meta object. |
||
| 471 | * @return object Meta object with possibly unserialized value. |
||
| 472 | */ |
||
| 473 | public function unserialize_meta( $meta ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Retrieve a set of objects by their IDs. |
||
| 480 | * |
||
| 481 | * @access public |
||
| 482 | * |
||
| 483 | * @param string $object_type Object type. |
||
| 484 | * @param array $ids Object IDs. |
||
| 485 | * @return array Array of objects. |
||
| 486 | */ |
||
| 487 | public function get_objects_by_id( $object_type, $ids ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
||
| 507 | * |
||
| 508 | * @access public |
||
| 509 | * |
||
| 510 | * @param int $batch_size The batch size for objects. |
||
| 511 | * @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
||
| 512 | * |
||
| 513 | * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
||
| 514 | */ |
||
| 515 | public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return Total number of objects. |
||
| 566 | * |
||
| 567 | * @param array $config Full Sync config. |
||
| 568 | * |
||
| 569 | * @return int total |
||
| 570 | */ |
||
| 571 | public function total( $config ) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 582 | * |
||
| 583 | * @access public |
||
| 584 | * |
||
| 585 | * @param array $config Full sync configuration for this sync module. |
||
| 586 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 587 | */ |
||
| 588 | public function get_where_sql( $config ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Gets the sync speed of a module. |
||
| 594 | * |
||
| 595 | * @access public |
||
| 596 | * |
||
| 597 | * @return int|null |
||
| 598 | */ |
||
| 599 | public function get_sync_speed() { |
||
| 602 | } |
||
| 603 |
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: