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 Give_DB_Meta 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 Give_DB_Meta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Give_DB_Meta extends Give_DB { |
||
18 | /** |
||
19 | * Post type |
||
20 | * |
||
21 | * @since 2.0 |
||
22 | * @access protected |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $post_type = ''; |
||
26 | |||
27 | /** |
||
28 | * Meta type |
||
29 | * |
||
30 | * @since 2.0 |
||
31 | * @access protected |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $meta_type = ''; |
||
35 | |||
36 | /** |
||
37 | * Flag to handle result type |
||
38 | * |
||
39 | * @since 2.0 |
||
40 | * @access protected |
||
41 | */ |
||
42 | protected $raw_result = false; |
||
43 | |||
44 | /** |
||
45 | * Flag for short circuit of meta function |
||
46 | * |
||
47 | * @since 2.0 |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $check = false; |
||
51 | |||
52 | |||
53 | /** |
||
54 | * Meta supports. |
||
55 | * |
||
56 | * @since 2.0 |
||
57 | * @access protected |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $supports = array( |
||
61 | 'add_post_metadata', |
||
62 | 'get_post_metadata', |
||
63 | 'update_post_metadata', |
||
64 | 'delete_post_metadata', |
||
65 | 'posts_where', |
||
66 | 'posts_join', |
||
67 | 'posts_groupby', |
||
68 | 'posts_orderby' |
||
69 | ); |
||
70 | |||
71 | /** |
||
72 | * Give_DB_Meta constructor. |
||
73 | * |
||
74 | * @since 2.0 |
||
75 | */ |
||
76 | function __construct() { |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Retrieve payment meta field for a payment. |
||
120 | * |
||
121 | * @access public |
||
122 | * @since 2.0 |
||
123 | * |
||
124 | * @param int $id Pst Type ID. |
||
125 | * @param string $meta_key The meta key to retrieve. |
||
126 | * @param bool $single Whether to return a single value. |
||
127 | * |
||
128 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
||
129 | * is true. |
||
130 | */ |
||
131 | public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Add meta data field to a payment. |
||
157 | * |
||
158 | * For internal use only. Use Give_Payment->add_meta() for public usage. |
||
159 | * |
||
160 | * @access private |
||
161 | * @since 2.0 |
||
162 | * |
||
163 | * @param int $id Post Type ID. |
||
164 | * @param string $meta_key Metadata name. |
||
165 | * @param mixed $meta_value Metadata value. |
||
166 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
167 | * |
||
168 | * @return int|bool False for failure. True for success. |
||
169 | */ |
||
170 | View Code Duplication | public function add_meta( $id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
186 | |||
187 | /** |
||
188 | * Update payment meta field based on Post Type ID. |
||
189 | * |
||
190 | * For internal use only. Use Give_Payment->update_meta() for public usage. |
||
191 | * |
||
192 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
193 | * same key and Post Type ID. |
||
194 | * |
||
195 | * If the meta field for the payment does not exist, it will be added. |
||
196 | * |
||
197 | * @access public |
||
198 | * @since 2.0 |
||
199 | * |
||
200 | * @param int $id Post Type ID. |
||
201 | * @param string $meta_key Metadata key. |
||
202 | * @param mixed $meta_value Metadata value. |
||
203 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
204 | * |
||
205 | * @return int|bool False on failure, true if success. |
||
206 | */ |
||
207 | View Code Duplication | public function update_meta( $id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
223 | |||
224 | /** |
||
225 | * Remove metadata matching criteria from a payment. |
||
226 | * |
||
227 | * You can match based on the key, or key and value. Removing based on key and |
||
228 | * value, will keep from removing duplicate metadata with the same key. It also |
||
229 | * allows removing all metadata matching key, if needed. |
||
230 | * |
||
231 | * @access public |
||
232 | * @since 2.0 |
||
233 | * |
||
234 | * @param int $id Post Type ID. |
||
235 | * @param string $meta_key Metadata name. |
||
236 | * @param mixed $meta_value Optional. Metadata value. |
||
237 | * @param mixed $delete_all Optional. |
||
238 | * |
||
239 | * @return bool False for failure. True for success. |
||
240 | */ |
||
241 | View Code Duplication | public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
|
257 | |||
258 | /** |
||
259 | * Rename query clauses of every query for new meta table |
||
260 | * |
||
261 | * @since 2.0 |
||
262 | * @access public |
||
263 | * |
||
264 | * @param string $clause |
||
265 | * @param WP_Query $wp_query |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | public function __rename_meta_table_name_in_query( $clause, $wp_query ) { |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Rename query clauses for new meta table |
||
281 | * |
||
282 | * @param $clause |
||
283 | * @param $filter |
||
284 | * |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function __rename_meta_table_name( $clause, $filter ) { |
||
328 | |||
329 | |||
330 | /** |
||
331 | * Check if current query for post type or not. |
||
332 | * |
||
333 | * @since 2.0 |
||
334 | * @access protected |
||
335 | * |
||
336 | * @param WP_Query $wp_query |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | protected function is_post_type_query( $wp_query ) { |
||
360 | |||
361 | /** |
||
362 | * Check if current id of post type or not |
||
363 | * |
||
364 | * @since 2.0 |
||
365 | * @access protected |
||
366 | * |
||
367 | * @param $ID |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | protected function is_valid_post_type( $ID ) { |
||
374 | |||
375 | /** |
||
376 | * check if custom meta table enabled or not. |
||
377 | * |
||
378 | * @since 2.0 |
||
379 | * @access protected |
||
380 | * @return bool |
||
381 | */ |
||
382 | protected function is_custom_meta_table_active() { |
||
385 | |||
386 | |||
387 | /** |
||
388 | * Update last_changed key |
||
389 | * |
||
390 | * @since 2.0 |
||
391 | * @access private |
||
392 | * |
||
393 | * @param int $id |
||
394 | * @param string $meta_type |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | private function delete_cache( $id, $meta_type = '' ) { |
||
412 | |||
413 | /** |
||
414 | * Add support for hidden functions. |
||
415 | * |
||
416 | * @since 2.0 |
||
417 | * @access public |
||
418 | * |
||
419 | * @param $name |
||
420 | * @param $arguments |
||
421 | * |
||
422 | * @return mixed |
||
423 | */ |
||
424 | public function __call( $name, $arguments ) { |
||
483 | |||
484 | /** |
||
485 | * Create Meta Tables. |
||
486 | * |
||
487 | * @since 2.0.1 |
||
488 | * @access public |
||
489 | */ |
||
490 | public function create_table() { |
||
510 | |||
511 | |||
512 | /** |
||
513 | * Get meta type |
||
514 | * |
||
515 | * @since 2.0.4 |
||
516 | * @access public |
||
517 | * |
||
518 | * @return string |
||
519 | */ |
||
520 | public function get_meta_type() { |
||
523 | |||
524 | /** |
||
525 | * Remove all meta data matching criteria from a meta table. |
||
526 | * |
||
527 | * @since 2.1.3 |
||
528 | * @access public |
||
529 | * |
||
530 | * @param int $id ID. |
||
531 | * |
||
532 | * @return bool False for failure. True for success. |
||
533 | */ |
||
534 | public function delete_all_meta( $id = 0 ) { |
||
544 | } |
||
545 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.