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:
| 1 | <?php |
||
| 17 | abstract class Module { |
||
| 18 | /** |
||
| 19 | * Number of items per chunk when grouping objects for performance reasons. |
||
| 20 | * |
||
| 21 | * @access public |
||
| 22 | * |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | const ARRAY_CHUNK_SIZE = 10; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Sync module name. |
||
| 29 | * |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | abstract public function name(); |
||
| 35 | |||
| 36 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Retrieve a sync object by its ID. |
||
| 40 | * |
||
| 41 | * @access public |
||
| 42 | * |
||
| 43 | * @param string $object_type Type of the sync object. |
||
| 44 | * @param int $id ID of the sync object. |
||
| 45 | * @return mixed Object, or false if the object is invalid. |
||
| 46 | */ |
||
| 47 | public function get_object_by_id( $object_type, $id ) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Initialize callables action listeners. |
||
| 53 | * Override these to set up listeners and set/reset data/defaults. |
||
| 54 | * |
||
| 55 | * @access public |
||
| 56 | * |
||
| 57 | * @param callable $callable Action handler callable. |
||
| 58 | */ |
||
| 59 | public function init_listeners( $callable ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize module action listeners for full sync. |
||
| 64 | * |
||
| 65 | * @access public |
||
| 66 | * |
||
| 67 | * @param callable $callable Action handler callable. |
||
| 68 | */ |
||
| 69 | public function init_full_sync_listeners( $callable ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Initialize the module in the sender. |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | */ |
||
| 77 | public function init_before_send() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set module defaults. |
||
| 82 | * |
||
| 83 | * @access public |
||
| 84 | */ |
||
| 85 | public function set_defaults() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Perform module cleanup. |
||
| 90 | * Usually triggered when uninstalling the plugin. |
||
| 91 | * |
||
| 92 | * @access public |
||
| 93 | */ |
||
| 94 | public function reset_data() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Enqueue the module actions for full sync. |
||
| 99 | * |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @param array $config Full sync configuration for this sync module. |
||
| 103 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 104 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 105 | * @return array Number of actions enqueued, and next module state. |
||
| 106 | */ |
||
| 107 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 114 | * |
||
| 115 | * @access public |
||
| 116 | * |
||
| 117 | * @param array $config Full sync configuration for this sync module. |
||
| 118 | * @return array Number of items yet to be enqueued. |
||
| 119 | */ |
||
| 120 | public function estimate_full_sync_actions( $config ) { |
||
| 124 | |||
| 125 | // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 129 | * |
||
| 130 | * @access public |
||
| 131 | * |
||
| 132 | * @return array Full sync actions of this module. |
||
| 133 | */ |
||
| 134 | public function get_full_sync_actions() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the number of actions that we care about. |
||
| 140 | * |
||
| 141 | * @access protected |
||
| 142 | * |
||
| 143 | * @param array $action_names Action names we're interested in. |
||
| 144 | * @param array $actions_to_count Unfiltered list of actions we want to count. |
||
| 145 | * @return array Number of actions that we're interested in. |
||
| 146 | */ |
||
| 147 | protected function count_actions( $action_names, $actions_to_count ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Calculate the checksum of one or more values. |
||
| 153 | * |
||
| 154 | * @access protected |
||
| 155 | * |
||
| 156 | * @param mixed $values Values to calculate checksum for. |
||
| 157 | * @return int The checksum. |
||
| 158 | */ |
||
| 159 | protected function get_check_sum( $values ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Whether a particular checksum in a set of checksums is valid. |
||
| 165 | * |
||
| 166 | * @access protected |
||
| 167 | * |
||
| 168 | * @param array $sums_to_check Array of checksums. |
||
| 169 | * @param string $name Name of the checksum. |
||
| 170 | * @param int $new_sum Checksum to compare against. |
||
| 171 | * @return boolean Whether the checksum is valid. |
||
| 172 | */ |
||
| 173 | protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Enqueue all items of a sync type as an action. |
||
| 183 | * |
||
| 184 | * @access protected |
||
| 185 | * |
||
| 186 | * @param string $action_name Name of the action. |
||
| 187 | * @param string $table_name Name of the database table. |
||
| 188 | * @param string $id_field Name of the ID field in the database. |
||
| 189 | * @param string $where_sql The SQL WHERE clause to filter to the desired items. |
||
| 190 | * @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
||
| 191 | * @param boolean $state Whether enqueueing has finished. |
||
| 192 | * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
||
| 193 | */ |
||
| 194 | protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
||
| 195 | global $wpdb; |
||
| 196 | |||
| 197 | if ( ! $where_sql ) { |
||
| 198 | $where_sql = '1 = 1'; |
||
| 199 | } |
||
| 200 | |||
| 201 | $items_per_page = 1000; |
||
| 202 | $page = 1; |
||
| 203 | $chunk_count = 0; |
||
| 204 | $previous_interval_end = $state ? $state : '~0'; |
||
| 205 | $listener = Listener::get_instance(); |
||
| 206 | |||
| 207 | // Count down from max_id to min_id so we get newest posts/comments/etc first. |
||
| 208 | // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
||
| 209 | while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_interval_end} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) { |
||
| 210 | // Request posts in groups of N for efficiency. |
||
| 211 | $chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
||
| 212 | |||
| 213 | // If we hit our row limit, process and return. |
||
| 214 | View Code Duplication | if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) { |
|
| 215 | $remaining_items_count = $max_items_to_enqueue - $chunk_count; |
||
| 216 | $remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count ); |
||
| 217 | $remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end ); |
||
| 218 | $listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end ); |
||
| 219 | |||
| 220 | $last_chunk = end( $remaining_items ); |
||
| 221 | return array( $remaining_items_count + $chunk_count, end( $last_chunk ) ); |
||
| 222 | } |
||
| 223 | $chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end ); |
||
| 224 | |||
| 225 | $listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end ); |
||
| 226 | |||
| 227 | $chunk_count += count( $chunked_ids ); |
||
| 228 | $page++; |
||
| 229 | // The $ids are ordered in descending order. |
||
| 230 | $previous_interval_end = end( $ids ); |
||
| 231 | } |
||
| 232 | |||
| 233 | return array( $chunk_count, true ); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Retrieve chunk IDs with previous interval end. |
||
| 238 | * |
||
| 239 | * @access protected |
||
| 240 | * |
||
| 241 | * @param array $chunks All remaining items. |
||
| 242 | * @param int $previous_interval_end The last item from the previous interval. |
||
| 243 | * @return array Chunk IDs with the previous interval end. |
||
| 244 | */ |
||
| 245 | protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get metadata of a particular object type within the designated meta key whitelist. |
||
| 260 | * |
||
| 261 | * @access protected |
||
| 262 | * |
||
| 263 | * @todo Refactor to use $wpdb->prepare() on the SQL query. |
||
| 264 | * |
||
| 265 | * @param array $ids Object IDs. |
||
| 266 | * @param string $meta_type Meta type. |
||
| 267 | * @param array $meta_key_whitelist Meta key whitelist. |
||
| 268 | * @return array Unserialized meta values. |
||
| 269 | */ |
||
| 270 | protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Initialize listeners for the particular meta type. |
||
| 294 | * |
||
| 295 | * @access public |
||
| 296 | * |
||
| 297 | * @param string $meta_type Meta type. |
||
| 298 | * @param callable $callable Action handler callable. |
||
| 299 | */ |
||
| 300 | public function init_listeners_for_meta_type( $meta_type, $callable ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Initialize meta whitelist handler for the particular meta type. |
||
| 308 | * |
||
| 309 | * @access public |
||
| 310 | * |
||
| 311 | * @param string $meta_type Meta type. |
||
| 312 | * @param callable $whitelist_handler Action handler callable. |
||
| 313 | */ |
||
| 314 | public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Retrieve the term relationships for the specified object IDs. |
||
| 322 | * |
||
| 323 | * @access protected |
||
| 324 | * |
||
| 325 | * @todo This feels too specific to be in the abstract sync Module class. Move it? |
||
| 326 | * |
||
| 327 | * @param array $ids Object IDs. |
||
| 328 | * @return array Term relationships - object ID and term taxonomy ID pairs. |
||
| 329 | */ |
||
| 330 | protected function get_term_relationships( $ids ) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Unserialize the value of a meta object, if necessary. |
||
| 339 | * |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @param object $meta Meta object. |
||
| 343 | * @return object Meta object with possibly unserialized value. |
||
| 344 | */ |
||
| 345 | public function unserialize_meta( $meta ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Retrieve a set of objects by their IDs. |
||
| 352 | * |
||
| 353 | * @access public |
||
| 354 | * |
||
| 355 | * @param string $object_type Object type. |
||
| 356 | * @param array $ids Object IDs. |
||
| 357 | * @return array Array of objects. |
||
| 358 | */ |
||
| 359 | public function get_objects_by_id( $object_type, $ids ) { |
||
| 376 | } |
||
| 377 |