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 | * Core data for this object. Name value pairs (name + default value).  | 
            ||
| 20 | * @var array  | 
            ||
| 21 | */  | 
            ||
| 22 | protected $_data = array();  | 
            ||
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * Set to _data on construct so we can track and reset data if needed.  | 
            ||
| 26 | * @var array  | 
            ||
| 27 | */  | 
            ||
| 28 | protected $_default_data = array();  | 
            ||
| 29 | |||
| 30 | /**  | 
            ||
| 31 | * Stores meta in cache for future reads.  | 
            ||
| 32 | * A group must be set to to enable caching.  | 
            ||
| 33 | * @var string  | 
            ||
| 34 | */  | 
            ||
| 35 | protected $_cache_group = '';  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Meta type. This should match up with  | 
            ||
| 39 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata.  | 
            ||
| 40 | * WP defines 'post', 'user', 'comment', and 'term'.  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $_meta_type = 'post';  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * This only needs set if you are using a custom metadata type (for example payment tokens.  | 
            ||
| 46 | * This should be the name of the field your table uses for associating meta with objects.  | 
            ||
| 47 | * For example, in payment_tokenmeta, this would be payment_token_id.  | 
            ||
| 48 | * @var string  | 
            ||
| 49 | */  | 
            ||
| 50 | protected $object_id_field_for_meta = '';  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * Stores additonal meta data.  | 
            ||
| 54 | * @var array  | 
            ||
| 55 | */  | 
            ||
| 56 | protected $_meta_data = array();  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Internal meta keys we don't want exposed for the object.  | 
            ||
| 60 | * @var array  | 
            ||
| 61 | */  | 
            ||
| 62 | protected $_internal_meta_keys = array();  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * Default constructor.  | 
            ||
| 66 | * @param int|object|array $read ID to load from the DB (optional) or already queried data.  | 
            ||
| 67 | */  | 
            ||
| 68 | 	public function __construct( $read = 0 ) { | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Returns the unique ID for this object.  | 
            ||
| 74 | * @return int  | 
            ||
| 75 | */  | 
            ||
| 76 | abstract public function get_id();  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * Creates new object in the database.  | 
            ||
| 80 | */  | 
            ||
| 81 | abstract public function create();  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * Read object from the database.  | 
            ||
| 85 | * @param int ID of the object to load.  | 
            ||
| 86 | */  | 
            ||
| 87 | abstract public function read( $id );  | 
            ||
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * Updates object data in the database.  | 
            ||
| 91 | */  | 
            ||
| 92 | abstract public function update();  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Updates object data in the database.  | 
            ||
| 96 | */  | 
            ||
| 97 | abstract public function delete();  | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Save should create or update based on object existance.  | 
            ||
| 101 | */  | 
            ||
| 102 | abstract public function save();  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Change data to JSON format.  | 
            ||
| 106 | * @return string Data in JSON format.  | 
            ||
| 107 | */  | 
            ||
| 108 | 	public function __toString() { | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * Returns all data for this object.  | 
            ||
| 114 | * @return array  | 
            ||
| 115 | */  | 
            ||
| 116 | 	public function get_data() { | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * Filter null meta values from array.  | 
            ||
| 122 | * @return bool  | 
            ||
| 123 | */  | 
            ||
| 124 | 	protected function filter_null_meta( $meta ) { | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Get All Meta Data.  | 
            ||
| 130 | * @since 2.6.0  | 
            ||
| 131 | * @return array  | 
            ||
| 132 | */  | 
            ||
| 133 | 	public function get_meta_data() { | 
            ||
| 136 | |||
| 137 | /**  | 
            ||
| 138 | * Internal meta keys we don't want exposed as part of meta_data. This is in  | 
            ||
| 139 | * addition to all data props with _ prefix.  | 
            ||
| 140 | * @since 2.6.0  | 
            ||
| 141 | * @return array  | 
            ||
| 142 | */  | 
            ||
| 143 | 	protected function prefix_key( $key ) { | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Internal meta keys we don't want exposed as part of meta_data. This is in  | 
            ||
| 149 | * addition to all data props with _ prefix.  | 
            ||
| 150 | * @since 2.6.0  | 
            ||
| 151 | * @return array  | 
            ||
| 152 | */  | 
            ||
| 153 | 	protected function get_internal_meta_keys() { | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * Get Meta Data by Key.  | 
            ||
| 159 | * @since 2.6.0  | 
            ||
| 160 | * @param string $key  | 
            ||
| 161 | * @param bool $single return first found meta with key, or all with $key  | 
            ||
| 162 | * @return mixed  | 
            ||
| 163 | */  | 
            ||
| 164 | 	public function get_meta( $key = '', $single = true ) { | 
            ||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * Set all meta data from array.  | 
            ||
| 181 | * @since 2.6.0  | 
            ||
| 182 | * @param array $data Key/Value pairs  | 
            ||
| 183 | */  | 
            ||
| 184 | 	public function set_meta_data( $data ) { | 
            ||
| 198 | |||
| 199 | /**  | 
            ||
| 200 | * Add meta data.  | 
            ||
| 201 | * @since 2.6.0  | 
            ||
| 202 | * @param string $key Meta key  | 
            ||
| 203 | * @param string $value Meta value  | 
            ||
| 204 | * @param bool $unique Should this be a unique key?  | 
            ||
| 205 | */  | 
            ||
| 206 | 	public function add_meta_data( $key, $value, $unique = false ) { | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * Update meta data by key or ID, if provided.  | 
            ||
| 218 | * @since 2.6.0  | 
            ||
| 219 | * @param string $key  | 
            ||
| 220 | * @param string $value  | 
            ||
| 221 | * @param int $meta_id  | 
            ||
| 222 | */  | 
            ||
| 223 | 	public function update_meta_data( $key, $value, $meta_id = '' ) { | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * Delete meta data.  | 
            ||
| 241 | * @since 2.6.0  | 
            ||
| 242 | * @param array $key Meta key  | 
            ||
| 243 | */  | 
            ||
| 244 | View Code Duplication | 	public function delete_meta_data( $key ) { | 
            |
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * Delete meta data.  | 
            ||
| 255 | * @since 2.6.0  | 
            ||
| 256 | * @param int $mid Meta ID  | 
            ||
| 257 | */  | 
            ||
| 258 | View Code Duplication | 	public function delete_meta_data_by_mid( $mid ) { | 
            |
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Callback to remove unwanted meta data.  | 
            ||
| 269 | *  | 
            ||
| 270 | * @param object $meta  | 
            ||
| 271 | * @return bool  | 
            ||
| 272 | */  | 
            ||
| 273 | 	protected function exclude_internal_meta_keys( $meta ) { | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * Read Meta Data from the database. Ignore any internal properties.  | 
            ||
| 279 | * @since 2.6.0  | 
            ||
| 280 | */  | 
            ||
| 281 | 	protected function read_meta_data() { | 
            ||
| 324 | |||
| 325 | /**  | 
            ||
| 326 | * Update Meta Data in the database.  | 
            ||
| 327 | * @since 2.6.0  | 
            ||
| 328 | */  | 
            ||
| 329 | 	protected function save_meta_data() { | 
            ||
| 348 | |||
| 349 | /**  | 
            ||
| 350 | * Table structure is slightly different between meta types, this function will return what we need to know.  | 
            ||
| 351 | * @since 2.6.0  | 
            ||
| 352 | * @return array Array elements: table, object_id_field, meta_id_field  | 
            ||
| 353 | */  | 
            ||
| 354 | 	protected function _get_db_info() { | 
            ||
| 383 | |||
| 384 | /**  | 
            ||
| 385 | * Set all props to default values.  | 
            ||
| 386 | */  | 
            ||
| 387 | 	protected function set_defaults() { | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Set a collection of props in one go, collect any errors, and return the result.  | 
            ||
| 393 | * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name.  | 
            ||
| 394 | * @return WP_Error|bool  | 
            ||
| 395 | */  | 
            ||
| 396 | 	public function set_props( $props ) { | 
            ||
| 415 | |||
| 416 | /**  | 
            ||
| 417 | * When invalid data is found, throw an exception unless reading from the DB.  | 
            ||
| 418 | * @param string $error_code Error code.  | 
            ||
| 419 | * @param string $error_message Error message.  | 
            ||
| 420 | * @throws WC_Data_Exception  | 
            ||
| 421 | */  | 
            ||
| 422 | 	protected function error( $error_code, $error_message ) { | 
            ||
| 425 | }  | 
            ||
| 426 | 
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: