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 | /** |
||
| 19 | * Sync module name. |
||
| 20 | * |
||
| 21 | * @access public |
||
| 22 | * |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public function name() { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The id field in the database. |
||
| 31 | * |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public function id_field() { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The table in the database. |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function table_name() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Allows WordPress.com servers to retrieve term-related objects via the sync API. |
||
| 53 | * |
||
| 54 | * @param string $object_type The type of object. |
||
| 55 | * @param int $id The id of the object. |
||
| 56 | * |
||
| 57 | * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type. |
||
| 58 | */ |
||
| 59 | public function get_object_by_id( $object_type, $id ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Initialize terms action listeners. |
||
| 94 | * |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param callable $callable Action handler callable. |
||
| 98 | */ |
||
| 99 | public function init_listeners( $callable ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Initialize terms action listeners for full sync. |
||
| 113 | * |
||
| 114 | * @access public |
||
| 115 | * |
||
| 116 | * @param callable $callable Action handler callable. |
||
| 117 | */ |
||
| 118 | public function init_full_sync_listeners( $callable ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Initialize the module in the sender. |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | */ |
||
| 127 | public function init_before_send() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Enqueue the terms 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 the WHERE SQL clause based on the module config. |
||
| 149 | * |
||
| 150 | * @access public |
||
| 151 | * |
||
| 152 | * @param array $config Full sync configuration for this sync module. |
||
| 153 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function get_where_sql( $config ) { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 167 | * |
||
| 168 | * @access public |
||
| 169 | * |
||
| 170 | * @param array $config Full sync configuration for this sync module. |
||
| 171 | * @return int Number of items yet to be enqueued. |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 191 | * |
||
| 192 | * @access public |
||
| 193 | * |
||
| 194 | * @return array Full sync actions of this module. |
||
| 195 | */ |
||
| 196 | public function get_full_sync_actions() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Handler for creating and updating terms. |
||
| 202 | * |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @param int $term_id Term ID. |
||
| 206 | * @param int $tt_id Term taxonomy ID. |
||
| 207 | * @param string $taxonomy Taxonomy slug. |
||
| 208 | */ |
||
| 209 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Filter blacklisted taxonomies. |
||
| 242 | * |
||
| 243 | * @access public |
||
| 244 | * |
||
| 245 | * @param array $args Hook args. |
||
| 246 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 247 | */ |
||
| 248 | public function filter_blacklisted_taxonomies( $args ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * |
||
| 263 | * @param array $args The hook parameters. |
||
| 264 | * @return array $args The expanded hook parameters. |
||
| 265 | */ |
||
| 266 | public function expand_term_taxonomy_id( $args ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a term object based on a given row from the term_relationships database table. |
||
| 284 | * |
||
| 285 | * @access public |
||
| 286 | * |
||
| 287 | * @param object $relationship A row object from the term_relationships table. |
||
| 288 | * @return object|bool A term object, or false if term taxonomy doesn't exist. |
||
| 289 | */ |
||
| 290 | public function expand_terms_for_relationship( $relationship ) { |
||
| 293 | |||
| 294 | } |
||
| 295 |