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 | * Only allow JS registration once |
||
18 | * @var bool |
||
19 | * @since 2.0.7 |
||
20 | */ |
||
21 | protected static $js_registration_done = false; |
||
22 | |||
23 | /** |
||
24 | * Only allow CSS registration once |
||
25 | * @var bool |
||
26 | * @since 2.0.7 |
||
27 | */ |
||
28 | protected static $css_registration_done = false; |
||
29 | |||
30 | /** |
||
31 | * CMB taxonomies array for term meta |
||
32 | * @var array |
||
33 | * @since 2.2.0 |
||
34 | */ |
||
35 | protected $taxonomies = array(); |
||
36 | |||
37 | /** |
||
38 | * Custom field columns. |
||
39 | * @var array |
||
40 | * @since 2.2.2 |
||
41 | */ |
||
42 | protected $columns = array(); |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * @since 2.0.0 |
||
47 | * @param CMB2 $cmb The CMB2 object to hookup |
||
48 | */ |
||
49 | public function __construct( CMB2 $cmb ) { |
||
53 | |||
54 | public function universal_hooks() { |
||
78 | |||
79 | public function post_hooks() { |
||
118 | |||
119 | public function comment_hooks() { |
||
128 | |||
129 | public function user_hooks() { |
||
145 | |||
146 | public function term_hooks() { |
||
185 | |||
186 | 1 | /** |
|
187 | 1 | * Registers styles for CMB2 |
|
188 | * @since 2.0.7 |
||
189 | */ |
||
190 | protected static function register_styles() { |
||
207 | |||
208 | /** |
||
209 | * Registers scripts for CMB2 |
||
210 | * @since 2.0.7 |
||
211 | */ |
||
212 | protected static function register_js() { |
||
222 | |||
223 | /** |
||
224 | * Registers scripts and styles for CMB2 |
||
225 | * @since 1.0.0 |
||
226 | */ |
||
227 | public static function register_scripts() { |
||
231 | |||
232 | /** |
||
233 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
234 | * @since 1.0.0 |
||
235 | */ |
||
236 | public function do_scripts( $hook ) { |
||
260 | |||
261 | /** |
||
262 | * Register the CMB2 field column headers. |
||
263 | * @since 2.2.2 |
||
264 | */ |
||
265 | public function register_column_headers( $columns ) { |
||
292 | |||
293 | /** |
||
294 | * The CMB2 field column display output. |
||
295 | * @since 2.2.2 |
||
296 | */ |
||
297 | public function column_display( $column_name, $object_id ) { |
||
309 | |||
310 | /** |
||
311 | * Returns the column display. |
||
312 | * @since 2.2.2 |
||
313 | */ |
||
314 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
321 | |||
322 | /** |
||
323 | * Output the CMB2 fields in an alternate context (not in a metabox). |
||
324 | * @since 2.2.4 |
||
325 | */ |
||
326 | public function add_context_metabox() { |
||
336 | |||
337 | /** |
||
338 | * Add metaboxes (to 'post' or 'comment' object types) |
||
339 | * @since 1.0.0 |
||
340 | */ |
||
341 | public function add_metaboxes() { |
||
372 | |||
373 | /** |
||
374 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
375 | * @since 2.2.3 |
||
376 | * @param string $post_type Post type to remove the metabox for. |
||
377 | */ |
||
378 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
388 | |||
389 | /** |
||
390 | * Add 'closed' class to metabox |
||
391 | * @since 2.0.0 |
||
392 | * @param array $classes Array of classes |
||
393 | * @return array Modified array of classes |
||
394 | */ |
||
395 | public function close_metabox_class( $classes ) { |
||
399 | |||
400 | /** |
||
401 | * Display metaboxes for a post or comment object |
||
402 | * @since 1.0.0 |
||
403 | */ |
||
404 | public function metabox_callback() { |
||
408 | |||
409 | /** |
||
410 | * Display metaboxes for new user page |
||
411 | * @since 1.0.0 |
||
412 | */ |
||
413 | public function user_new_metabox( $section ) { |
||
420 | |||
421 | /** |
||
422 | * Display metaboxes for a user object |
||
423 | * @since 1.0.0 |
||
424 | */ |
||
425 | public function user_metabox() { |
||
428 | |||
429 | /** |
||
430 | * Display metaboxes for a taxonomy term object |
||
431 | * @since 2.2.0 |
||
432 | */ |
||
433 | public function term_metabox() { |
||
436 | |||
437 | /** |
||
438 | * Display metaboxes for an object type |
||
439 | * @since 2.2.0 |
||
440 | * @param string $type Object type |
||
441 | * @return void |
||
442 | */ |
||
443 | public function show_form_for_type( $type ) { |
||
461 | |||
462 | /** |
||
463 | * Determines if metabox should be shown in current context |
||
464 | * @since 2.0.0 |
||
465 | * @return bool Whether metabox should be added/shown |
||
466 | */ |
||
467 | public function show_on() { |
||
482 | |||
483 | /** |
||
484 | * Get the CMB priority property set to numeric hook priority. |
||
485 | * @since 2.2.0 |
||
486 | * @param integer $default Default display hook priority. |
||
487 | * @return integer Hook priority. |
||
488 | */ |
||
489 | public function get_priority( $default = 10 ) { |
||
511 | |||
512 | /** |
||
513 | * Save data from post metabox |
||
514 | * @since 1.0.0 |
||
515 | * @param int $post_id Post ID |
||
516 | * @param mixed $post Post object |
||
517 | * @return null |
||
518 | */ |
||
519 | public function save_post( $post_id, $post = false ) { |
||
538 | |||
539 | /** |
||
540 | * Save data from comment metabox |
||
541 | * @since 2.0.9 |
||
542 | * @param int $comment_id Comment ID |
||
543 | * @return null |
||
544 | */ |
||
545 | public function save_comment( $comment_id ) { |
||
553 | |||
554 | /** |
||
555 | * Save data from user fields |
||
556 | * @since 1.0.x |
||
557 | * @param int $user_id User ID |
||
558 | * @return null |
||
559 | */ |
||
560 | public function save_user( $user_id ) { |
||
566 | |||
567 | /** |
||
568 | * Save data from term fields |
||
569 | * @since 2.2.0 |
||
570 | * @param int $term_id Term ID |
||
571 | * @param int $tt_id Term Taxonomy ID |
||
572 | * @param string $taxonomy Taxonomy |
||
573 | * @return null |
||
574 | */ |
||
575 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
583 | |||
584 | /** |
||
585 | * Delete term meta when a term is deleted. |
||
586 | * @since 2.2.0 |
||
587 | * @param int $term_id Term ID |
||
588 | * @param int $tt_id Term Taxonomy ID |
||
589 | * @param string $taxonomy Taxonomy |
||
590 | * @return null |
||
591 | */ |
||
592 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
603 | |||
604 | /** |
||
605 | * Determines if the current object is able to be saved |
||
606 | * @since 2.0.9 |
||
607 | * @param string $type Current post_type or comment_type |
||
608 | * @return bool Whether object can be saved |
||
609 | */ |
||
610 | public function can_save( $type = '' ) { |
||
624 | 1 | ||
625 | 1 | /** |
|
626 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
627 | * @since 2.2.0 |
||
628 | * @param string $taxonomy Taxonomy of term being modified. |
||
629 | 1 | * @return bool Whether taxonomy is editable. |
|
630 | */ |
||
631 | public function taxonomy_can_save( $taxonomy ) { |
||
644 | |||
645 | /** |
||
646 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
647 | 1 | * @since 2.2.2.1 |
|
648 | 1 | */ |
|
649 | protected function maybe_enqueue_column_display_styles() { |
||
660 | |||
661 | /** |
||
662 | * Includes CMB2 styles |
||
663 | * @since 2.0.0 |
||
664 | */ |
||
665 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
678 | |||
679 | /** |
||
680 | * Includes CMB2 JS |
||
681 | * @since 2.0.0 |
||
682 | */ |
||
683 | public static function enqueue_cmb_js() { |
||
691 | |||
692 | } |
||
693 |
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.