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:
Complex classes like WC_Data often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class WC_Data { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * ID for this object. |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $id = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Core data for this object. Name value pairs (name + default value). |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $data = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Set to _data on construct so we can track and reset data if needed. |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $default_data = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Stores meta in cache for future reads. |
||
| 38 | * A group must be set to to enable caching. |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $cache_group = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Meta type. This should match up with |
||
| 45 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. |
||
| 46 | * WP defines 'post', 'user', 'comment', and 'term'. |
||
| 47 | */ |
||
| 48 | protected $meta_type = 'post'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This only needs set if you are using a custom metadata type (for example payment tokens. |
||
| 52 | * This should be the name of the field your table uses for associating meta with objects. |
||
| 53 | * For example, in payment_tokenmeta, this would be payment_token_id. |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $object_id_field_for_meta = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Stores additonal meta data. |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $meta_data = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Internal meta keys we don't want exposed for the object. |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $internal_meta_keys = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Default constructor. |
||
| 72 | * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
||
| 73 | */ |
||
| 74 | public function __construct( $read = 0 ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns the unique ID for this object. |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function get_id() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Creates new object in the database. |
||
| 88 | */ |
||
| 89 | public function create() {} |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Read object from the database. |
||
| 93 | * @param int ID of the object to load. |
||
| 94 | */ |
||
| 95 | public function read( $id ) {} |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Updates object data in the database. |
||
| 99 | */ |
||
| 100 | public function update() {} |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Updates object data in the database. |
||
| 104 | */ |
||
| 105 | public function delete() {} |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Save should create or update based on object existance. |
||
| 109 | */ |
||
| 110 | public function save() {} |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Change data to JSON format. |
||
| 114 | * @return string Data in JSON format. |
||
| 115 | */ |
||
| 116 | public function __toString() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns all data for this object. |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function get_data() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Filter null meta values from array. |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | protected function filter_null_meta( $meta ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get All Meta Data. |
||
| 138 | * @since 2.6.0 |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function get_meta_data() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Internal meta keys we don't want exposed as part of meta_data. This is in |
||
| 147 | * addition to all data props with _ prefix. |
||
| 148 | * @since 2.6.0 |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function prefix_key( $key ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Internal meta keys we don't want exposed as part of meta_data. This is in |
||
| 157 | * addition to all data props with _ prefix. |
||
| 158 | * @since 2.6.0 |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | protected function get_internal_meta_keys() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get Meta Data by Key. |
||
| 167 | * @since 2.6.0 |
||
| 168 | * @param string $key |
||
| 169 | * @param bool $single return first found meta with key, or all with $key |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | public function get_meta( $key = '', $single = true ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set all meta data from array. |
||
| 189 | * @since 2.6.0 |
||
| 190 | * @param array $data Key/Value pairs |
||
| 191 | */ |
||
| 192 | public function set_meta_data( $data ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Add meta data. |
||
| 209 | * @since 2.6.0 |
||
| 210 | * @param string $key Meta key |
||
| 211 | * @param string $value Meta value |
||
| 212 | * @param bool $unique Should this be a unique key? |
||
| 213 | */ |
||
| 214 | public function add_meta_data( $key, $value, $unique = false ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Update meta data by key or ID, if provided. |
||
| 226 | * @since 2.6.0 |
||
| 227 | * @param string $key |
||
| 228 | * @param string $value |
||
| 229 | * @param int $meta_id |
||
| 230 | */ |
||
| 231 | public function update_meta_data( $key, $value, $meta_id = '' ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Delete meta data. |
||
| 249 | * @since 2.6.0 |
||
| 250 | * @param array $key Meta key |
||
| 251 | */ |
||
| 252 | View Code Duplication | public function delete_meta_data( $key ) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Delete meta data. |
||
| 263 | * @since 2.6.0 |
||
| 264 | * @param int $mid Meta ID |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function delete_meta_data_by_mid( $mid ) { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Callback to remove unwanted meta data. |
||
| 277 | * |
||
| 278 | * @param object $meta |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | protected function exclude_internal_meta_keys( $meta ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Read Meta Data from the database. Ignore any internal properties. |
||
| 287 | * @since 2.6.0 |
||
| 288 | */ |
||
| 289 | protected function read_meta_data() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Update Meta Data in the database. |
||
| 335 | * @since 2.6.0 |
||
| 336 | */ |
||
| 337 | protected function save_meta_data() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Table structure is slightly different between meta types, this function will return what we need to know. |
||
| 359 | * @since 2.6.0 |
||
| 360 | * @return array Array elements: table, object_id_field, meta_id_field |
||
| 361 | */ |
||
| 362 | protected function get_db_info() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set ID. |
||
| 394 | * @param int $id |
||
| 395 | */ |
||
| 396 | public function set_id( $id ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set all props to default values. |
||
| 402 | */ |
||
| 403 | protected function set_defaults() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set a collection of props in one go, collect any errors, and return the result. |
||
| 409 | * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
||
| 410 | * @return WP_Error|bool |
||
| 411 | */ |
||
| 412 | public function set_props( $props ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * When invalid data is found, throw an exception unless reading from the DB. |
||
| 434 | * @param string $error_code Error code. |
||
| 435 | * @param string $error_message Error message. |
||
| 436 | * @throws WC_Data_Exception |
||
| 437 | */ |
||
| 438 | protected function error( $error_code, $error_message ) { |
||
| 441 | } |
||
| 442 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: