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 default values for this object, name value pairs (name + default value). |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $_default_data = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Core data for this object. |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $_data = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * True when reading from the database. |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $_reading = false; |
||
| 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 | * Returns the unique ID for this object. |
||
| 72 | * @return int |
||
| 73 | */ |
||
| 74 | abstract public function get_id(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Creates new object in the database. |
||
| 78 | */ |
||
| 79 | abstract public function create(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Read object from the database. |
||
| 83 | * @param int ID of the object to load. |
||
| 84 | */ |
||
| 85 | abstract public function read( $id ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Updates object data in the database. |
||
| 89 | */ |
||
| 90 | abstract public function update(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Updates object data in the database. |
||
| 94 | */ |
||
| 95 | abstract public function delete(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Save should create or update based on object existance. |
||
| 99 | */ |
||
| 100 | abstract public function save(); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Change data to JSON format. |
||
| 104 | * @return string Data in JSON format. |
||
| 105 | */ |
||
| 106 | public function __toString() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns all data for this object. |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function get_data() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Filter null meta values from array. |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | protected function filter_null_meta( $meta ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get All Meta Data. |
||
| 128 | * @since 2.6.0 |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function get_meta_data() { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Internal meta keys we don't want exposed as part of meta_data. This is in |
||
| 137 | * addition to all data props with _ prefix. |
||
| 138 | * @since 2.6.0 |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | protected function prefix_key( $key ) { |
||
| 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 get_internal_meta_keys() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get Meta Data by Key. |
||
| 157 | * @since 2.6.0 |
||
| 158 | * @param string $key |
||
| 159 | * @param bool $single return first found meta with key, or all with $key |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function get_meta( $key = '', $single = true ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set all meta data from array. |
||
| 179 | * @since 2.6.0 |
||
| 180 | * @param array $data Key/Value pairs |
||
| 181 | */ |
||
| 182 | public function set_meta_data( $data ) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Add meta data. |
||
| 199 | * @since 2.6.0 |
||
| 200 | * @param string $key Meta key |
||
| 201 | * @param string $value Meta value |
||
| 202 | * @param bool $unique Should this be a unique key? |
||
| 203 | */ |
||
| 204 | public function add_meta_data( $key, $value, $unique = false ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Update meta data by key or ID, if provided. |
||
| 216 | * @since 2.6.0 |
||
| 217 | * @param string $key |
||
| 218 | * @param string $value |
||
| 219 | * @param int $meta_id |
||
| 220 | */ |
||
| 221 | public function update_meta_data( $key, $value, $meta_id = '' ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Delete meta data. |
||
| 239 | * @since 2.6.0 |
||
| 240 | * @param array $key Meta key |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function delete_meta_data( $key ) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Delete meta data. |
||
| 253 | * @since 2.6.0 |
||
| 254 | * @param int $mid Meta ID |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function delete_meta_data_by_mid( $mid ) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Read Meta Data from the database. Ignore any internal properties. |
||
| 267 | * @since 2.6.0 |
||
| 268 | */ |
||
| 269 | protected function read_meta_data() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Update Meta Data in the database. |
||
| 317 | * @since 2.6.0 |
||
| 318 | */ |
||
| 319 | protected function save_meta_data() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Table structure is slightly different between meta types, this function will return what we need to know. |
||
| 341 | * @since 2.6.0 |
||
| 342 | * @return array Array elements: table, object_id_field, meta_id_field |
||
| 343 | */ |
||
| 344 | protected function _get_db_info() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set all props to default values. |
||
| 376 | */ |
||
| 377 | protected function set_defaults() { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get internal data prop (raw). |
||
| 383 | * @param string ...$param Prop keys to retrieve. Supports multiple keys to get nested values. |
||
| 384 | * @return mixed |
||
| 385 | */ |
||
| 386 | protected function get_prop() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set internal data prop to specified value. |
||
| 402 | * @param int ...$param Prop keys followed by value to set. |
||
| 403 | * @throws WC_Data_Exception |
||
| 404 | */ |
||
| 405 | protected function set_prop() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * When invalid data is found, throw an exception unless reading from the DB. |
||
| 423 | * @param string $error_code Error code. |
||
| 424 | * @param string $data $error_message Error message. |
||
| 425 | * @throws WC_Data_Exception |
||
| 426 | */ |
||
| 427 | protected function invalid_data( $error_code, $error_message ) { |
||
| 432 | } |
||
| 433 |
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: