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 | * Read Meta Data from the database. Ignore any internal properties. |
||
| 269 | * @since 2.6.0 |
||
| 270 | */ |
||
| 271 | protected function read_meta_data() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Update Meta Data in the database. |
||
| 319 | * @since 2.6.0 |
||
| 320 | */ |
||
| 321 | protected function save_meta_data() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Table structure is slightly different between meta types, this function will return what we need to know. |
||
| 343 | * @since 2.6.0 |
||
| 344 | * @return array Array elements: table, object_id_field, meta_id_field |
||
| 345 | */ |
||
| 346 | protected function _get_db_info() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set all props to default values. |
||
| 378 | */ |
||
| 379 | protected function set_defaults() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get internal data prop (raw). |
||
| 385 | * @param string ...$param Prop keys to retrieve. Supports multiple keys to get nested values. |
||
| 386 | * @return mixed |
||
| 387 | */ |
||
| 388 | protected function get_prop() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set a collection of props in one go, collect any errors, and return the result. |
||
| 404 | * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
||
| 405 | * @return WP_Error|bool |
||
| 406 | */ |
||
| 407 | public function set_props( $props ) { |
||
| 408 | $errors = new WP_Error(); |
||
| 409 | |||
| 410 | foreach ( $props as $prop => $value ) { |
||
| 411 | try { |
||
| 412 | $setter = "set_$prop"; |
||
| 413 | if ( ! is_null( $value ) && is_callable( array( $this, $setter ) ) ) { |
||
| 414 | $this->{$setter}( wc_clean( wp_unslash( $value ) ) ); |
||
| 415 | } |
||
| 416 | } catch ( WC_Data_Exception $e ) { |
||
| 417 | $errors->add( $e->getErrorCode(), $e->getMessage() ); |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | return sizeof( $errors->get_error_codes() ) ? $errors : true; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Set internal data prop to specified value. |
||
| 426 | * @param int ...$param Prop keys followed by value to set. |
||
| 427 | * @throws WC_Data_Exception |
||
| 428 | */ |
||
| 429 | protected function set_prop() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * When invalid data is found, throw an exception unless reading from the DB. |
||
| 447 | * @param string $error_code Error code. |
||
| 448 | * @param string $error_message Error message. |
||
| 449 | * @throws WC_Data_Exception |
||
| 450 | */ |
||
| 451 | protected function error( $error_code, $error_message ) { |
||
| 454 | } |
||
| 455 |
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: