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 | * Get All Meta Data. |
||
120 | * @since 2.6.0 |
||
121 | * @return array |
||
122 | */ |
||
123 | public function get_meta_data() { |
||
126 | |||
127 | /** |
||
128 | * Internal meta keys we don't want exposed as part of meta_data. This is in |
||
129 | * addition to all data props with _ prefix. |
||
130 | * @since 2.6.0 |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function prefix_key( $key ) { |
||
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 get_internal_meta_keys() { |
||
146 | |||
147 | /** |
||
148 | * Get Meta Data by Key. |
||
149 | * @since 2.6.0 |
||
150 | * @param string $key |
||
151 | * @param bool $single return first found meta with key, or all with $key |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function get_meta( $key = '', $single = true ) { |
||
168 | |||
169 | /** |
||
170 | * Set all meta data from array. |
||
171 | * @since 2.6.0 |
||
172 | * @param array $data Key/Value pairs |
||
173 | */ |
||
174 | public function set_meta_data( $data ) { |
||
188 | |||
189 | /** |
||
190 | * Add meta data. |
||
191 | * @since 2.6.0 |
||
192 | * @param string $key Meta key |
||
193 | * @param string $value Meta value |
||
194 | * @param bool $unique Should this be a unique key? |
||
195 | */ |
||
196 | public function add_meta_data( $key, $value, $unique = false ) { |
||
206 | |||
207 | /** |
||
208 | * Update meta data by key or ID, if provided. |
||
209 | * @since 2.6.0 |
||
210 | * @param string $key |
||
211 | * @param string $value |
||
212 | * @param int $meta_id |
||
213 | */ |
||
214 | public function update_meta_data( $key, $value, $meta_id = '' ) { |
||
229 | |||
230 | /** |
||
231 | * Delete meta data. |
||
232 | * @since 2.6.0 |
||
233 | * @param array $key Meta key |
||
234 | */ |
||
235 | public function delete_meta_data( $key ) { |
||
239 | |||
240 | /** |
||
241 | * Delete meta data. |
||
242 | * @since 2.6.0 |
||
243 | * @param int $mid Meta ID |
||
244 | */ |
||
245 | public function delete_meta_data_by_mid( $mid ) { |
||
249 | |||
250 | /** |
||
251 | * Read Meta Data from the database. Ignore any internal properties. |
||
252 | * @since 2.6.0 |
||
253 | */ |
||
254 | protected function read_meta_data() { |
||
299 | |||
300 | /** |
||
301 | * Update Meta Data in the database. |
||
302 | * @since 2.6.0 |
||
303 | */ |
||
304 | protected function save_meta_data() { |
||
337 | |||
338 | /** |
||
339 | * Table structure is slightly different between meta types, this function will return what we need to know. |
||
340 | * @since 2.6.0 |
||
341 | * @return array Array elements: table, object_id_field, meta_id_field |
||
342 | */ |
||
343 | protected function _get_db_info() { |
||
372 | |||
373 | /** |
||
374 | * Set all props to default values. |
||
375 | */ |
||
376 | protected function set_defaults() { |
||
379 | |||
380 | /** |
||
381 | * Get internal data prop (raw). |
||
382 | * @param string ...$param Prop keys to retrieve. Supports multiple keys to get nested values. |
||
383 | * @return mixed |
||
384 | */ |
||
385 | protected function get_prop() { |
||
398 | |||
399 | /** |
||
400 | * Set internal data prop to specified value. |
||
401 | * @param int ...$param Prop keys followed by value to set. |
||
402 | * @throws WC_Data_Exception |
||
403 | */ |
||
404 | protected function set_prop() { |
||
419 | |||
420 | /** |
||
421 | * When invalid data is found, throw an exception unless reading from the DB. |
||
422 | * @param string $error_code Error code. |
||
423 | * @param string $data $error_message Error message. |
||
|
|||
424 | * @throws WC_Data_Exception |
||
425 | */ |
||
426 | protected function invalid_data( $error_code, $error_message ) { |
||
431 | } |
||
432 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.