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 |
||
| 18 | class WC_Post_Data { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Editing term. |
||
| 22 | * |
||
| 23 | * @var object |
||
| 24 | */ |
||
| 25 | private static $editing_term = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Hook in methods. |
||
| 29 | */ |
||
| 30 | public static function init() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Delete transients when terms are set. |
||
| 48 | */ |
||
| 49 | public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * When a post status changes. |
||
| 57 | */ |
||
| 58 | public static function transition_post_status( $new_status, $old_status, $post ) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified. |
||
| 66 | */ |
||
| 67 | public static function delete_product_query_transients() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * When editing a term, check for product attributes. |
||
| 87 | * @param id $term_id |
||
| 88 | * @param id $tt_id |
||
| 89 | * @param string $taxonomy |
||
| 90 | */ |
||
| 91 | public static function edit_term( $term_id, $tt_id, $taxonomy ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * When a term is edited, check for product attributes and update variations. |
||
| 101 | * @param id $term_id |
||
| 102 | * @param id $tt_id |
||
| 103 | * @param string $taxonomy |
||
| 104 | */ |
||
| 105 | public static function edited_term( $term_id, $tt_id, $taxonomy ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Ensure floats are correctly converted to strings based on PHP locale. |
||
| 121 | * |
||
| 122 | * @param null $check |
||
| 123 | * @param int $object_id |
||
| 124 | * @param string $meta_key |
||
| 125 | * @param mixed $meta_value |
||
| 126 | * @param mixed $prev_value |
||
| 127 | * @return null|bool |
||
| 128 | */ |
||
| 129 | public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Ensure floats are correctly converted to strings based on PHP locale. |
||
| 146 | * |
||
| 147 | * @param null $check |
||
| 148 | * @param int $object_id |
||
| 149 | * @param string $meta_key |
||
| 150 | * @param mixed $meta_value |
||
| 151 | * @param mixed $prev_value |
||
| 152 | * @return null|bool |
||
| 153 | */ |
||
| 154 | public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * When setting stock level, ensure the stock status is kept in sync. |
||
| 171 | * @param int $meta_id |
||
| 172 | * @param int $object_id |
||
| 173 | * @param string $meta_key |
||
| 174 | * @param mixed $_meta_value |
||
| 175 | */ |
||
| 176 | public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $_meta_value ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Forces the order posts to have a title in a certain format (containing the date). |
||
| 185 | * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent. |
||
| 186 | * |
||
| 187 | * @param array $data |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public static function wp_insert_post_data( $data ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Some functions, like the term recount, require the visibility to be set prior. Lets save that here. |
||
| 214 | * |
||
| 215 | * @param int $post_id |
||
| 216 | */ |
||
| 217 | public static function pre_post_update( $post_id ) { |
||
| 229 | } |
||
| 230 | |||
| 232 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.