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() { |
||
92 | |||
93 | public function comment_hooks() { |
||
102 | |||
103 | public function user_hooks() { |
||
119 | |||
120 | 1 | public function term_hooks() { |
|
159 | 1 | ||
160 | 1 | /** |
|
161 | 1 | * Registers styles for CMB2 |
|
162 | * @since 2.0.7 |
||
163 | 1 | */ |
|
164 | 1 | protected static function register_styles() { |
|
181 | |||
182 | 1 | /** |
|
183 | 1 | * Registers scripts for CMB2 |
|
184 | * @since 2.0.7 |
||
185 | */ |
||
186 | protected static function register_js() { |
||
196 | |||
197 | /** |
||
198 | * Registers scripts and styles for CMB2 |
||
199 | * @since 1.0.0 |
||
200 | */ |
||
201 | public static function register_scripts() { |
||
205 | |||
206 | /** |
||
207 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
208 | * @since 1.0.0 |
||
209 | */ |
||
210 | public function do_scripts( $hook ) { |
||
234 | |||
235 | /** |
||
236 | * Register the CMB2 field column headers. |
||
237 | * @since 2.2.2 |
||
238 | */ |
||
239 | public function register_column_headers( $columns ) { |
||
266 | |||
267 | /** |
||
268 | * The CMB2 field column display output. |
||
269 | * @since 2.2.2 |
||
270 | */ |
||
271 | public function column_display( $column_name, $object_id ) { |
||
283 | |||
284 | /** |
||
285 | * Returns the column display. |
||
286 | * @since 2.2.2 |
||
287 | */ |
||
288 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
295 | |||
296 | /** |
||
297 | * Add metaboxes (to 'post' or 'comment' object types) |
||
298 | * @since 1.0.0 |
||
299 | */ |
||
300 | public function add_metaboxes() { |
||
331 | |||
332 | /** |
||
333 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
334 | * @since 2.2.3 |
||
335 | * @param string $post_type Post type to remove the metabox for. |
||
336 | */ |
||
337 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
347 | |||
348 | /** |
||
349 | * Add 'closed' class to metabox |
||
350 | 1 | * @since 2.0.0 |
|
351 | 1 | * @param array $classes Array of classes |
|
352 | * @return array Modified array of classes |
||
353 | 1 | */ |
|
354 | public function close_metabox_class( $classes ) { |
||
358 | |||
359 | /** |
||
360 | 1 | * Display metaboxes for a post or comment object |
|
361 | 1 | * @since 1.0.0 |
|
362 | 1 | */ |
|
363 | 1 | public function metabox_callback() { |
|
367 | 1 | ||
368 | /** |
||
369 | * Display metaboxes for new user page |
||
370 | * @since 1.0.0 |
||
371 | */ |
||
372 | public function user_new_metabox( $section ) { |
||
379 | |||
380 | /** |
||
381 | * Display metaboxes for a user object |
||
382 | * @since 1.0.0 |
||
383 | */ |
||
384 | public function user_metabox() { |
||
387 | |||
388 | /** |
||
389 | * Display metaboxes for a taxonomy term object |
||
390 | * @since 2.2.0 |
||
391 | */ |
||
392 | public function term_metabox() { |
||
395 | |||
396 | /** |
||
397 | * Display metaboxes for an object type |
||
398 | * @since 2.2.0 |
||
399 | * @param string $type Object type |
||
400 | * @return void |
||
401 | */ |
||
402 | public function show_form_for_type( $type ) { |
||
420 | |||
421 | /** |
||
422 | * Determines if metabox should be shown in current context |
||
423 | * @since 2.0.0 |
||
424 | * @return bool Whether metabox should be added/shown |
||
425 | */ |
||
426 | public function show_on() { |
||
441 | |||
442 | /** |
||
443 | * Get the CMB priority property set to numeric hook priority. |
||
444 | * @since 2.2.0 |
||
445 | * @param integer $default Default display hook priority. |
||
446 | * @return integer Hook priority. |
||
447 | */ |
||
448 | public function get_priority( $default = 10 ) { |
||
470 | |||
471 | /** |
||
472 | * Save data from post metabox |
||
473 | * @since 1.0.0 |
||
474 | * @param int $post_id Post ID |
||
475 | * @param mixed $post Post object |
||
476 | * @return null |
||
477 | */ |
||
478 | public function save_post( $post_id, $post = false ) { |
||
497 | |||
498 | /** |
||
499 | * Save data from comment metabox |
||
500 | * @since 2.0.9 |
||
501 | * @param int $comment_id Comment ID |
||
502 | * @return null |
||
503 | */ |
||
504 | public function save_comment( $comment_id ) { |
||
512 | |||
513 | /** |
||
514 | * Save data from user fields |
||
515 | * @since 1.0.x |
||
516 | * @param int $user_id User ID |
||
517 | * @return null |
||
518 | */ |
||
519 | public function save_user( $user_id ) { |
||
525 | |||
526 | /** |
||
527 | * Save data from term fields |
||
528 | * @since 2.2.0 |
||
529 | * @param int $term_id Term ID |
||
530 | * @param int $tt_id Term Taxonomy ID |
||
531 | * @param string $taxonomy Taxonomy |
||
532 | * @return null |
||
533 | */ |
||
534 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
542 | |||
543 | /** |
||
544 | * Delete term meta when a term is deleted. |
||
545 | * @since 2.2.0 |
||
546 | * @param int $term_id Term ID |
||
547 | * @param int $tt_id Term Taxonomy ID |
||
548 | * @param string $taxonomy Taxonomy |
||
549 | * @return null |
||
550 | */ |
||
551 | 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 = '' ) { |
||
583 | |||
584 | /** |
||
585 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
586 | * @since 2.2.0 |
||
587 | * @param string $taxonomy Taxonomy of term being modified. |
||
588 | * @return bool Whether taxonomy is editable. |
||
589 | */ |
||
590 | public function taxonomy_can_save( $taxonomy ) { |
||
603 | |||
604 | /** |
||
605 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
606 | * @since 2.2.2.1 |
||
607 | */ |
||
608 | protected function maybe_enqueue_column_display_styles() { |
||
619 | |||
620 | /** |
||
621 | * Includes CMB2 styles |
||
622 | * @since 2.0.0 |
||
623 | */ |
||
624 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
637 | |||
638 | /** |
||
639 | * Includes CMB2 JS |
||
640 | * @since 2.0.0 |
||
641 | */ |
||
642 | public static function enqueue_cmb_js() { |
||
650 | |||
651 | } |
||
652 |
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.