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 |
||
| 16 | class Terms extends Module { |
||
| 17 | /** |
||
| 18 | * An estimate of how many rows per second can be synced during a full sync. |
||
| 19 | * |
||
| 20 | * @access static |
||
| 21 | * |
||
| 22 | * @var int|null Null if speed is not important in a full sync. |
||
| 23 | */ |
||
| 24 | static $sync_speed = 769; |
||
| 25 | /** |
||
| 26 | * Sync module name. |
||
| 27 | * |
||
| 28 | * @access public |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function name() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The id field in the database. |
||
| 38 | * |
||
| 39 | * @access public |
||
| 40 | * |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function id_field() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The table in the database. |
||
| 49 | * |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function table_name() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Allows WordPress.com servers to retrieve term-related objects via the sync API. |
||
| 60 | * |
||
| 61 | * @param string $object_type The type of object. |
||
| 62 | * @param int $id The id of the object. |
||
| 63 | * |
||
| 64 | * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type. |
||
| 65 | */ |
||
| 66 | public function get_object_by_id( $object_type, $id ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialize terms action listeners. |
||
| 101 | * |
||
| 102 | * @access public |
||
| 103 | * |
||
| 104 | * @param callable $callable Action handler callable. |
||
| 105 | */ |
||
| 106 | public function init_listeners( $callable ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Initialize terms action listeners for full sync. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * |
||
| 123 | * @param callable $callable Action handler callable. |
||
| 124 | */ |
||
| 125 | public function init_full_sync_listeners( $callable ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Initialize the module in the sender. |
||
| 131 | * |
||
| 132 | * @access public |
||
| 133 | */ |
||
| 134 | public function init_before_send() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Enqueue the terms actions for full sync. |
||
| 141 | * |
||
| 142 | * @access public |
||
| 143 | * |
||
| 144 | * @param array $config Full sync configuration for this sync module. |
||
| 145 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 146 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 147 | * @return array Number of actions enqueued, and next module state. |
||
| 148 | */ |
||
| 149 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 156 | * |
||
| 157 | * @access public |
||
| 158 | * |
||
| 159 | * @param array $config Full sync configuration for this sync module. |
||
| 160 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function get_where_sql( $config ) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 174 | * |
||
| 175 | * @access public |
||
| 176 | * |
||
| 177 | * @param array $config Full sync configuration for this sync module. |
||
| 178 | * @return int Number of items yet to be enqueued. |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 198 | * |
||
| 199 | * @access public |
||
| 200 | * |
||
| 201 | * @return array Full sync actions of this module. |
||
| 202 | */ |
||
| 203 | public function get_full_sync_actions() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Handler for creating and updating terms. |
||
| 209 | * |
||
| 210 | * @access public |
||
| 211 | * |
||
| 212 | * @param int $term_id Term ID. |
||
| 213 | * @param int $tt_id Term taxonomy ID. |
||
| 214 | * @param string $taxonomy Taxonomy slug. |
||
| 215 | */ |
||
| 216 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Filter blacklisted taxonomies. |
||
| 249 | * |
||
| 250 | * @access public |
||
| 251 | * |
||
| 252 | * @param array $args Hook args. |
||
| 253 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 254 | */ |
||
| 255 | public function filter_blacklisted_taxonomies( $args ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
||
| 267 | * |
||
| 268 | * @access public |
||
| 269 | * |
||
| 270 | * @param array $args The hook parameters. |
||
| 271 | * @return array $args The expanded hook parameters. |
||
| 272 | */ |
||
| 273 | public function expand_term_taxonomy_id( $args ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Gets a term object based on a given row from the term_relationships database table. |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * |
||
| 294 | * @param object $relationship A row object from the term_relationships table. |
||
| 295 | * @return object|bool A term object, or false if term taxonomy doesn't exist. |
||
| 296 | */ |
||
| 297 | public function expand_terms_for_relationship( $relationship ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Gets the sync speed of a module. |
||
| 303 | * |
||
| 304 | * @access public |
||
| 305 | * |
||
| 306 | * @return int |
||
| 307 | */ |
||
| 308 | public function get_sync_speed() { |
||
| 311 | } |
||
| 312 |