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 { |
||
|
|
|||
| 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 | * Custom field columns. |
||
| 52 | * @var array |
||
| 53 | * @since 2.2.2 |
||
| 54 | */ |
||
| 55 | protected $columns = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The object type we are performing the hookup for |
||
| 59 | * @var string |
||
| 60 | * @since 2.0.9 |
||
| 61 | */ |
||
| 62 | protected $object_type = 'post'; |
||
| 63 | |||
| 64 | public function __construct( CMB2 $cmb ) { |
||
| 85 | |||
| 86 | public function universal_hooks() { |
||
| 99 | |||
| 100 | public function post_hooks() { |
||
| 113 | |||
| 114 | public function comment_hooks() { |
||
| 123 | |||
| 124 | public function user_hooks() { |
||
| 140 | |||
| 141 | public function term_hooks() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Registers styles for CMB2 |
||
| 183 | * @since 2.0.7 |
||
| 184 | */ |
||
| 185 | protected static function register_styles() { |
||
| 202 | 1 | ||
| 203 | 1 | /** |
|
| 204 | * Registers scripts for CMB2 |
||
| 205 | * @since 2.0.7 |
||
| 206 | */ |
||
| 207 | protected static function register_js() { |
||
| 217 | 1 | ||
| 218 | 1 | /** |
|
| 219 | * Registers scripts and styles for CMB2 |
||
| 220 | * @since 1.0.0 |
||
| 221 | */ |
||
| 222 | public static function register_scripts() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 229 | * @since 1.0.0 |
||
| 230 | */ |
||
| 231 | public function do_scripts( $hook ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Register the CMB2 field column headers. |
||
| 258 | * @since 2.2.2 |
||
| 259 | */ |
||
| 260 | public function register_column_headers( $columns ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * The CMB2 field column display output. |
||
| 290 | * @since 2.2.2 |
||
| 291 | */ |
||
| 292 | public function column_display( $column_name, $object_id ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the column display. |
||
| 307 | * @since 2.2.2 |
||
| 308 | */ |
||
| 309 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 319 | * @since 1.0.0 |
||
| 320 | */ |
||
| 321 | public function add_metaboxes() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Add 'closed' class to metabox |
||
| 351 | * @since 2.0.0 |
||
| 352 | * @param array $classes Array of classes |
||
| 353 | * @return array Modified array of classes |
||
| 354 | */ |
||
| 355 | public function close_metabox_class( $classes ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Display metaboxes for a post or comment object |
||
| 362 | * @since 1.0.0 |
||
| 363 | */ |
||
| 364 | public function metabox_callback() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Display metaboxes for new user page |
||
| 371 | * @since 1.0.0 |
||
| 372 | */ |
||
| 373 | public function user_new_metabox( $section ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Display metaboxes for a user object |
||
| 383 | * @since 1.0.0 |
||
| 384 | */ |
||
| 385 | public function user_metabox() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Display metaboxes for a taxonomy term object |
||
| 391 | * @since 2.2.0 |
||
| 392 | */ |
||
| 393 | public function term_metabox() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Display metaboxes for an object type |
||
| 399 | * @since 2.2.0 |
||
| 400 | * @param string $type Object type |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function show_form_for_type( $type ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Determines if metabox should be shown in current context |
||
| 424 | * @since 2.0.0 |
||
| 425 | * @return bool Whether metabox should be added/shown |
||
| 426 | */ |
||
| 427 | public function show_on() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Get the CMB priority property set to numeric hook priority. |
||
| 445 | * @since 2.2.0 |
||
| 446 | * @param integer $default Default display hook priority. |
||
| 447 | * @return integer Hook priority. |
||
| 448 | */ |
||
| 449 | public function get_priority( $default = 10 ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Save data from post metabox |
||
| 474 | * @since 1.0.0 |
||
| 475 | * @param int $post_id Post ID |
||
| 476 | * @param mixed $post Post object |
||
| 477 | * @return null |
||
| 478 | */ |
||
| 479 | public function save_post( $post_id, $post = false ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Save data from comment metabox |
||
| 501 | * @since 2.0.9 |
||
| 502 | * @param int $comment_id Comment ID |
||
| 503 | * @return null |
||
| 504 | */ |
||
| 505 | public function save_comment( $comment_id ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Save data from user fields |
||
| 516 | * @since 1.0.x |
||
| 517 | * @param int $user_id User ID |
||
| 518 | * @return null |
||
| 519 | */ |
||
| 520 | public function save_user( $user_id ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Save data from term fields |
||
| 529 | * @since 2.2.0 |
||
| 530 | * @param int $term_id Term ID |
||
| 531 | * @param int $tt_id Term Taxonomy ID |
||
| 532 | * @param string $taxonomy Taxonomy |
||
| 533 | * @return null |
||
| 534 | */ |
||
| 535 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Delete term meta when a term is deleted. |
||
| 546 | * @since 2.2.0 |
||
| 547 | * @param int $term_id Term ID |
||
| 548 | * @param int $tt_id Term Taxonomy ID |
||
| 549 | * @param string $taxonomy Taxonomy |
||
| 550 | * @return null |
||
| 551 | */ |
||
| 552 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Determines if the current object is able to be saved |
||
| 565 | * @since 2.0.9 |
||
| 566 | * @param string $type Current post_type or comment_type |
||
| 567 | * @return bool Whether object can be saved |
||
| 568 | */ |
||
| 569 | public function can_save( $type = '' ) { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 584 | * @since 2.2.0 |
||
| 585 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 586 | * @return bool Whether taxonomy is editable. |
||
| 587 | */ |
||
| 588 | public function taxonomy_can_save( $taxonomy ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Ensures WordPress hook only gets fired once |
||
| 604 | * @since 2.0.0 |
||
| 605 | * @param string $action The name of the filter to hook the $hook callback to. |
||
| 606 | * @param callback $hook The callback to be run when the filter is applied. |
||
| 607 | * @param integer $priority Order the functions are executed |
||
| 608 | * @param int $accepted_args The number of arguments the function accepts. |
||
| 609 | */ |
||
| 610 | public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
| 623 | * @since 2.2.2.1 |
||
| 624 | */ |
||
| 625 | protected function maybe_enqueue_column_display_styles() { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Includes CMB2 styles |
||
| 639 | * @since 2.0.0 |
||
| 640 | 1 | */ |
|
| 641 | 1 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Includes CMB2 JS |
||
| 657 | * @since 2.0.0 |
||
| 658 | */ |
||
| 659 | public static function enqueue_cmb_js() { |
||
| 667 | |||
| 668 | } |
||
| 669 |
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.