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 |
||
| 15 | class Comments extends Module { |
||
| 16 | /** |
||
| 17 | * An estimate of how many rows per second can be synced during a full sync. |
||
| 18 | * |
||
| 19 | * @access static |
||
| 20 | * |
||
| 21 | * @var int|null Null if speed is not important in a full sync. |
||
| 22 | */ |
||
| 23 | static $sync_speed = 43; |
||
| 24 | /** |
||
| 25 | * Sync module name. |
||
| 26 | * |
||
| 27 | * @access public |
||
| 28 | * |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | public function name() { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The id field in the database. |
||
| 37 | * |
||
| 38 | * @access public |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public function id_field() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The table in the database. |
||
| 48 | * |
||
| 49 | * @access public |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function table_name() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Retrieve a comment by its ID. |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @param string $object_type Type of the sync object. |
||
| 63 | * @param int $id ID of the sync object. |
||
| 64 | * @return \WP_Comment|bool Filtered \WP_Comment object, or false if the object is not a comment. |
||
| 65 | */ |
||
| 66 | public function get_object_by_id( $object_type, $id ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initialize comments action listeners. |
||
| 80 | * Also responsible for initializing comment meta listeners. |
||
| 81 | * |
||
| 82 | * @access public |
||
| 83 | * |
||
| 84 | * @param callable $callable Action handler callable. |
||
| 85 | */ |
||
| 86 | public function init_listeners( $callable ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Handler for any comment content updates. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * |
||
| 123 | * @param array $new_comment The new, processed comment data. |
||
| 124 | * @param array $old_comment The old, unslashed comment data. |
||
| 125 | * @param array $new_comment_with_slashes The new, raw comment data. |
||
| 126 | * @return array The new, processed comment data. |
||
| 127 | */ |
||
| 128 | public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Initialize comments action listeners for full sync. |
||
| 159 | * |
||
| 160 | * @access public |
||
| 161 | * |
||
| 162 | * @param callable $callable Action handler callable. |
||
| 163 | */ |
||
| 164 | public function init_full_sync_listeners( $callable ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Gets a filtered list of comment types that sync can hook into. |
||
| 170 | * |
||
| 171 | * @access public |
||
| 172 | * |
||
| 173 | * @return array Defaults to [ '', 'trackback', 'pingback' ]. |
||
| 174 | */ |
||
| 175 | public function get_whitelisted_comment_types() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Prevents any comment types that are not in the whitelist from being enqueued and sent to WordPress.com. |
||
| 191 | * |
||
| 192 | * @param array $args Arguments passed to wp_insert_comment. |
||
| 193 | * |
||
| 194 | * @return bool or array $args Arguments passed to wp_insert_comment |
||
| 195 | */ |
||
| 196 | public function only_allow_white_listed_comment_types( $args ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Initialize the module in the sender. |
||
| 208 | * |
||
| 209 | * @access public |
||
| 210 | */ |
||
| 211 | public function init_before_send() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Enqueue the comments actions for full sync. |
||
| 233 | * |
||
| 234 | * @access public |
||
| 235 | * |
||
| 236 | * @param array $config Full sync configuration for this sync module. |
||
| 237 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 238 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 239 | * @return array Number of actions enqueued, and next module state. |
||
| 240 | */ |
||
| 241 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 248 | * |
||
| 249 | * @access public |
||
| 250 | * |
||
| 251 | * @param array $config Full sync configuration for this sync module. |
||
| 252 | * @return int Number of items yet to be enqueued. |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 273 | * |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @param array $config Full sync configuration for this sync module. |
||
| 277 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 278 | */ |
||
| 279 | public function get_where_sql( $config ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 289 | * |
||
| 290 | * @access public |
||
| 291 | * |
||
| 292 | * @return array Full sync actions of this module. |
||
| 293 | */ |
||
| 294 | public function get_full_sync_actions() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Count all the actions that are going to be sent. |
||
| 300 | * |
||
| 301 | * @access public |
||
| 302 | * |
||
| 303 | * @param array $action_names Names of all the actions that will be sent. |
||
| 304 | * @return int Number of actions. |
||
| 305 | */ |
||
| 306 | public function count_full_sync_actions( $action_names ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Expand the comment status change before the data is serialized and sent to the server. |
||
| 312 | * |
||
| 313 | * @access public |
||
| 314 | * @todo This is not used currently - let's implement it. |
||
| 315 | * |
||
| 316 | * @param array $args The hook parameters. |
||
| 317 | * @return array The expanded hook parameters. |
||
| 318 | */ |
||
| 319 | public function expand_wp_comment_status_change( $args ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Expand the comment creation before the data is serialized and sent to the server. |
||
| 325 | * |
||
| 326 | * @access public |
||
| 327 | * |
||
| 328 | * @param array $args The hook parameters. |
||
| 329 | * @return array The expanded hook parameters. |
||
| 330 | */ |
||
| 331 | public function expand_wp_insert_comment( $args ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Filter a comment object to the fields we need. |
||
| 337 | * |
||
| 338 | * @access public |
||
| 339 | * |
||
| 340 | * @param \WP_Comment $comment The unfiltered comment object. |
||
| 341 | * @return \WP_Comment Filtered comment object. |
||
| 342 | */ |
||
| 343 | public function filter_comment( $comment ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Whether a certain comment meta key is whitelisted for sync. |
||
| 372 | * |
||
| 373 | * @access public |
||
| 374 | * |
||
| 375 | * @param string $meta_key Comment meta key. |
||
| 376 | * @return boolean Whether the meta key is whitelisted. |
||
| 377 | */ |
||
| 378 | public function is_whitelisted_comment_meta( $meta_key ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Handler for filtering out non-whitelisted comment meta. |
||
| 384 | * |
||
| 385 | * @access public |
||
| 386 | * |
||
| 387 | * @param array $args Hook args. |
||
| 388 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 389 | */ |
||
| 390 | public function filter_meta( $args ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Expand the comment IDs to comment objects and meta before being serialized and sent to the server. |
||
| 396 | * |
||
| 397 | * @access public |
||
| 398 | * |
||
| 399 | * @param array $args The hook parameters. |
||
| 400 | * @return array The expanded hook parameters. |
||
| 401 | */ |
||
| 402 | public function expand_comment_ids( $args ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Gets the sync speed of a module. |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * |
||
| 425 | * @return int |
||
| 426 | */ |
||
| 427 | public function get_sync_speed() { |
||
| 430 | } |
||
| 431 |