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 | * Whitelist for taxonomies we want to sync. |
||
| 19 | * |
||
| 20 | * @access private |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $taxonomy_whitelist; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Sync module name. |
||
| 28 | * |
||
| 29 | * @access public |
||
| 30 | * |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | public function name() { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Allows WordPress.com servers to retrieve term-related objects via the sync API. |
||
| 39 | * |
||
| 40 | * @param string $object_type The type of object. |
||
| 41 | * @param int $id The id of the object. |
||
| 42 | * |
||
| 43 | * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type. |
||
| 44 | */ |
||
| 45 | public function get_object_by_id( $object_type, $id ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Initialize terms action listeners. |
||
| 80 | * |
||
| 81 | * @access public |
||
| 82 | * |
||
| 83 | * @param callable $callable Action handler callable. |
||
| 84 | */ |
||
| 85 | public function init_listeners( $callable ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize terms 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 ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initialize the module in the sender. |
||
| 110 | * |
||
| 111 | * @access public |
||
| 112 | */ |
||
| 113 | public function init_before_send() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Enqueue the terms actions for full sync. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * |
||
| 123 | * @param array $config Full sync configuration for this sync module. |
||
| 124 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 125 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 126 | * @return array Number of actions enqueued, and next module state. |
||
| 127 | */ |
||
| 128 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Retrieve the WHERE SQL clause based on the module config. |
||
| 135 | * |
||
| 136 | * @access private |
||
| 137 | * |
||
| 138 | * @param array $config Full sync configuration for this sync module. |
||
| 139 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
| 140 | */ |
||
| 141 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 153 | * |
||
| 154 | * @access public |
||
| 155 | * |
||
| 156 | * @param array $config Full sync configuration for this sync module. |
||
| 157 | * @return int Number of items yet to be enqueued. |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 177 | * |
||
| 178 | * @access public |
||
| 179 | * |
||
| 180 | * @return array Full sync actions of this module. |
||
| 181 | */ |
||
| 182 | public function get_full_sync_actions() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Handler for creating and updating terms. |
||
| 188 | * |
||
| 189 | * @access public |
||
| 190 | * |
||
| 191 | * @param int $term_id Term ID. |
||
| 192 | * @param int $tt_id Term taxonomy ID. |
||
| 193 | * @param string $taxonomy Taxonomy slug. |
||
| 194 | */ |
||
| 195 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Filter blacklisted taxonomies. |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * |
||
| 231 | * @param array $args Hook args. |
||
| 232 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
| 233 | */ |
||
| 234 | public function filter_blacklisted_taxonomies( $args ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set the taxonomy whitelist. |
||
| 246 | * |
||
| 247 | * @access public |
||
| 248 | * |
||
| 249 | * @param array $taxonomies The new taxonomyy whitelist. |
||
| 250 | */ |
||
| 251 | public function set_taxonomy_whitelist( $taxonomies ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Set module defaults. |
||
| 257 | * Define the taxonomy whitelist to be the default one. |
||
| 258 | * |
||
| 259 | * @access public |
||
| 260 | */ |
||
| 261 | public function set_defaults() { |
||
| 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 | * Appends a term object to a row object from the term_relationships table. |
||
| 291 | * This aids WordPress.com in re-syncing term_relationship tables that are out of sync. |
||
| 292 | * |
||
| 293 | * @access public |
||
| 294 | * |
||
| 295 | * @param object $relationship A row object from the term_relationships table. |
||
| 296 | * @return object A row object from the term_relationships table with corresponding term object appended. |
||
| 297 | */ |
||
| 298 | public function add_term_to_relationship( $relationship ) { |
||
| 306 | } |
||
| 307 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.