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 CMB2_hookup 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 CMB2_hookup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CMB2_hookup extends CMB2_Hookup_Base { |
||
|
|||
15 | |||
16 | /** |
||
17 | * Array of all hooks done (to be run once) |
||
18 | * @var array |
||
19 | * @since 2.0.0 |
||
20 | */ |
||
21 | protected static $hooks_completed = array(); |
||
22 | |||
23 | /** |
||
24 | * Only allow JS registration once |
||
25 | * @var bool |
||
26 | * @since 2.0.7 |
||
27 | */ |
||
28 | protected static $js_registration_done = false; |
||
29 | |||
30 | /** |
||
31 | * Only allow CSS registration once |
||
32 | * @var bool |
||
33 | * @since 2.0.7 |
||
34 | */ |
||
35 | protected static $css_registration_done = false; |
||
36 | |||
37 | /** |
||
38 | * @var CMB2 object |
||
39 | * @since 2.0.2 |
||
40 | */ |
||
41 | protected $cmb; |
||
42 | |||
43 | /** |
||
44 | * CMB taxonomies array for term meta |
||
45 | * @var array |
||
46 | * @since 2.2.0 |
||
47 | */ |
||
48 | protected $taxonomies = array(); |
||
49 | |||
50 | /** |
||
51 | * The object type we are performing the hookup for |
||
52 | * @var string |
||
53 | * @since 2.0.9 |
||
54 | */ |
||
55 | protected $object_type = 'post'; |
||
56 | |||
57 | /** |
||
58 | * Custom field columns. |
||
59 | * @var array |
||
60 | * @since 2.2.2 |
||
61 | */ |
||
62 | protected $columns = array(); |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * @since 2.0.0 |
||
67 | * @param CMB2 $cmb The CMB2 object to hookup |
||
68 | */ |
||
69 | public function __construct( CMB2 $cmb ) { |
||
90 | |||
91 | public function universal_hooks() { |
||
106 | |||
107 | public function post_hooks() { |
||
120 | |||
121 | public function comment_hooks() { |
||
130 | |||
131 | public function user_hooks() { |
||
147 | |||
148 | public function term_hooks() { |
||
187 | |||
188 | /** |
||
189 | * Registers styles for CMB2 |
||
190 | * @since 2.0.7 |
||
191 | */ |
||
192 | 1 | protected static function register_styles() { |
|
209 | |||
210 | /** |
||
211 | * Registers scripts for CMB2 |
||
212 | * @since 2.0.7 |
||
213 | */ |
||
214 | 1 | protected static function register_js() { |
|
224 | |||
225 | /** |
||
226 | * Registers scripts and styles for CMB2 |
||
227 | * @since 1.0.0 |
||
228 | */ |
||
229 | public static function register_scripts() { |
||
233 | |||
234 | /** |
||
235 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
236 | * @since 1.0.0 |
||
237 | */ |
||
238 | public function do_scripts( $hook ) { |
||
262 | |||
263 | /** |
||
264 | * Register the CMB2 field column headers. |
||
265 | * @since 2.2.2 |
||
266 | */ |
||
267 | public function register_column_headers( $columns ) { |
||
294 | |||
295 | /** |
||
296 | * The CMB2 field column display output. |
||
297 | * @since 2.2.2 |
||
298 | */ |
||
299 | public function column_display( $column_name, $object_id ) { |
||
311 | |||
312 | /** |
||
313 | * Returns the column display. |
||
314 | * @since 2.2.2 |
||
315 | */ |
||
316 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
323 | |||
324 | /** |
||
325 | * Add metaboxes (to 'post' or 'comment' object types) |
||
326 | * @since 1.0.0 |
||
327 | */ |
||
328 | public function add_metaboxes() { |
||
355 | |||
356 | /** |
||
357 | * Add 'closed' class to metabox |
||
358 | * @since 2.0.0 |
||
359 | * @param array $classes Array of classes |
||
360 | * @return array Modified array of classes |
||
361 | */ |
||
362 | public function close_metabox_class( $classes ) { |
||
366 | |||
367 | /** |
||
368 | * Display metaboxes for a post or comment object |
||
369 | * @since 1.0.0 |
||
370 | */ |
||
371 | public function metabox_callback() { |
||
375 | |||
376 | /** |
||
377 | * Display metaboxes for new user page |
||
378 | * @since 1.0.0 |
||
379 | */ |
||
380 | public function user_new_metabox( $section ) { |
||
387 | |||
388 | /** |
||
389 | * Display metaboxes for a user object |
||
390 | * @since 1.0.0 |
||
391 | */ |
||
392 | public function user_metabox() { |
||
395 | |||
396 | /** |
||
397 | * Display metaboxes for a taxonomy term object |
||
398 | * @since 2.2.0 |
||
399 | */ |
||
400 | public function term_metabox() { |
||
403 | |||
404 | /** |
||
405 | * Display metaboxes for an object type |
||
406 | * @since 2.2.0 |
||
407 | * @param string $type Object type |
||
408 | * @return void |
||
409 | */ |
||
410 | public function show_form_for_type( $type ) { |
||
428 | |||
429 | /** |
||
430 | * Determines if metabox should be shown in current context |
||
431 | * @since 2.0.0 |
||
432 | * @return bool Whether metabox should be added/shown |
||
433 | */ |
||
434 | public function show_on() { |
||
449 | |||
450 | /** |
||
451 | * Get the CMB priority property set to numeric hook priority. |
||
452 | * @since 2.2.0 |
||
453 | * @param integer $default Default display hook priority. |
||
454 | * @return integer Hook priority. |
||
455 | */ |
||
456 | public function get_priority( $default = 10 ) { |
||
478 | |||
479 | /** |
||
480 | * Save data from post metabox |
||
481 | * @since 1.0.0 |
||
482 | * @param int $post_id Post ID |
||
483 | * @param mixed $post Post object |
||
484 | * @return null |
||
485 | */ |
||
486 | public function save_post( $post_id, $post = false ) { |
||
505 | |||
506 | /** |
||
507 | * Save data from comment metabox |
||
508 | * @since 2.0.9 |
||
509 | * @param int $comment_id Comment ID |
||
510 | * @return null |
||
511 | */ |
||
512 | public function save_comment( $comment_id ) { |
||
520 | |||
521 | /** |
||
522 | * Save data from user fields |
||
523 | * @since 1.0.x |
||
524 | * @param int $user_id User ID |
||
525 | * @return null |
||
526 | */ |
||
527 | public function save_user( $user_id ) { |
||
533 | |||
534 | /** |
||
535 | * Save data from term fields |
||
536 | * @since 2.2.0 |
||
537 | * @param int $term_id Term ID |
||
538 | * @param int $tt_id Term Taxonomy ID |
||
539 | * @param string $taxonomy Taxonomy |
||
540 | * @return null |
||
541 | */ |
||
542 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
550 | |||
551 | /** |
||
552 | * Delete term meta when a term is deleted. |
||
553 | * @since 2.2.0 |
||
554 | * @param int $term_id Term ID |
||
555 | * @param int $tt_id Term Taxonomy ID |
||
556 | * @param string $taxonomy Taxonomy |
||
557 | * @return null |
||
558 | */ |
||
559 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
569 | |||
570 | /** |
||
571 | * Determines if the current object is able to be saved |
||
572 | * @since 2.0.9 |
||
573 | * @param string $type Current post_type or comment_type |
||
574 | * @return bool Whether object can be saved |
||
575 | */ |
||
576 | public function can_save( $type = '' ) { |
||
588 | |||
589 | /** |
||
590 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
591 | * @since 2.2.0 |
||
592 | * @param string $taxonomy Taxonomy of term being modified. |
||
593 | * @return bool Whether taxonomy is editable. |
||
594 | */ |
||
595 | public function taxonomy_can_save( $taxonomy ) { |
||
608 | |||
609 | /** |
||
610 | * Includes CMB2 styles |
||
611 | * @since 2.0.0 |
||
612 | */ |
||
613 | 1 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
|
621 | |||
622 | /** |
||
623 | * Includes CMB2 JS |
||
624 | * @since 2.0.0 |
||
625 | */ |
||
626 | 1 | public static function enqueue_cmb_js() { |
|
634 | |||
635 | } |
||
636 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.