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 | * Custom field columns. |
||
52 | * @var array |
||
53 | * @since 2.2.2 |
||
54 | */ |
||
55 | protected $columns = array(); |
||
56 | |||
57 | /** |
||
58 | * Constructor |
||
59 | * @since 2.0.0 |
||
60 | * @param CMB2 $cmb The CMB2 object to hookup |
||
61 | */ |
||
62 | public function __construct( CMB2 $cmb ) { |
||
83 | |||
84 | public function universal_hooks() { |
||
97 | |||
98 | public function post_hooks() { |
||
111 | |||
112 | public function comment_hooks() { |
||
121 | |||
122 | public function user_hooks() { |
||
138 | |||
139 | public function term_hooks() { |
||
178 | |||
179 | /** |
||
180 | * Registers styles for CMB2 |
||
181 | * @since 2.0.7 |
||
182 | */ |
||
183 | protected static function register_styles() { |
||
200 | 1 | ||
201 | 1 | /** |
|
202 | * Registers scripts for CMB2 |
||
203 | * @since 2.0.7 |
||
204 | */ |
||
205 | protected static function register_js() { |
||
215 | 1 | ||
216 | 1 | /** |
|
217 | * Registers scripts and styles for CMB2 |
||
218 | * @since 1.0.0 |
||
219 | */ |
||
220 | public static function register_scripts() { |
||
224 | |||
225 | /** |
||
226 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
227 | * @since 1.0.0 |
||
228 | */ |
||
229 | public function do_scripts( $hook ) { |
||
253 | |||
254 | /** |
||
255 | * Register the CMB2 field column headers. |
||
256 | * @since 2.2.2 |
||
257 | */ |
||
258 | public function register_column_headers( $columns ) { |
||
285 | |||
286 | /** |
||
287 | * The CMB2 field column display output. |
||
288 | * @since 2.2.2 |
||
289 | */ |
||
290 | public function column_display( $column_name, $object_id ) { |
||
302 | |||
303 | /** |
||
304 | * Returns the column display. |
||
305 | * @since 2.2.2 |
||
306 | */ |
||
307 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
314 | |||
315 | /** |
||
316 | * Add metaboxes (to 'post' or 'comment' object types) |
||
317 | * @since 1.0.0 |
||
318 | */ |
||
319 | public function add_metaboxes() { |
||
346 | |||
347 | /** |
||
348 | * Add 'closed' class to metabox |
||
349 | * @since 2.0.0 |
||
350 | * @param array $classes Array of classes |
||
351 | * @return array Modified array of classes |
||
352 | */ |
||
353 | public function close_metabox_class( $classes ) { |
||
357 | |||
358 | /** |
||
359 | * Display metaboxes for a post or comment object |
||
360 | * @since 1.0.0 |
||
361 | */ |
||
362 | public function metabox_callback() { |
||
366 | |||
367 | /** |
||
368 | * Display metaboxes for new user page |
||
369 | * @since 1.0.0 |
||
370 | */ |
||
371 | public function user_new_metabox( $section ) { |
||
378 | |||
379 | /** |
||
380 | * Display metaboxes for a user object |
||
381 | * @since 1.0.0 |
||
382 | */ |
||
383 | public function user_metabox() { |
||
386 | |||
387 | /** |
||
388 | * Display metaboxes for a taxonomy term object |
||
389 | * @since 2.2.0 |
||
390 | */ |
||
391 | public function term_metabox() { |
||
394 | |||
395 | /** |
||
396 | * Display metaboxes for an object type |
||
397 | * @since 2.2.0 |
||
398 | * @param string $type Object type |
||
399 | * @return void |
||
400 | */ |
||
401 | public function show_form_for_type( $type ) { |
||
419 | |||
420 | /** |
||
421 | * Determines if metabox should be shown in current context |
||
422 | * @since 2.0.0 |
||
423 | * @return bool Whether metabox should be added/shown |
||
424 | */ |
||
425 | public function show_on() { |
||
440 | |||
441 | /** |
||
442 | * Get the CMB priority property set to numeric hook priority. |
||
443 | * @since 2.2.0 |
||
444 | * @param integer $default Default display hook priority. |
||
445 | * @return integer Hook priority. |
||
446 | */ |
||
447 | public function get_priority( $default = 10 ) { |
||
469 | |||
470 | /** |
||
471 | * Save data from post metabox |
||
472 | * @since 1.0.0 |
||
473 | * @param int $post_id Post ID |
||
474 | * @param mixed $post Post object |
||
475 | * @return null |
||
476 | */ |
||
477 | public function save_post( $post_id, $post = false ) { |
||
496 | |||
497 | /** |
||
498 | * Save data from comment metabox |
||
499 | * @since 2.0.9 |
||
500 | * @param int $comment_id Comment ID |
||
501 | * @return null |
||
502 | */ |
||
503 | public function save_comment( $comment_id ) { |
||
511 | |||
512 | /** |
||
513 | * Save data from user fields |
||
514 | * @since 1.0.x |
||
515 | * @param int $user_id User ID |
||
516 | * @return null |
||
517 | */ |
||
518 | public function save_user( $user_id ) { |
||
524 | |||
525 | /** |
||
526 | * Save data from term fields |
||
527 | * @since 2.2.0 |
||
528 | * @param int $term_id Term ID |
||
529 | * @param int $tt_id Term Taxonomy ID |
||
530 | * @param string $taxonomy Taxonomy |
||
531 | * @return null |
||
532 | */ |
||
533 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
541 | |||
542 | /** |
||
543 | * Delete term meta when a term is deleted. |
||
544 | * @since 2.2.0 |
||
545 | * @param int $term_id Term ID |
||
546 | * @param int $tt_id Term Taxonomy ID |
||
547 | * @param string $taxonomy Taxonomy |
||
548 | * @return null |
||
549 | */ |
||
550 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
560 | |||
561 | /** |
||
562 | * Determines if the current object is able to be saved |
||
563 | * @since 2.0.9 |
||
564 | * @param string $type Current post_type or comment_type |
||
565 | * @return bool Whether object can be saved |
||
566 | */ |
||
567 | public function can_save( $type = '' ) { |
||
579 | |||
580 | /** |
||
581 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
582 | * @since 2.2.0 |
||
583 | * @param string $taxonomy Taxonomy of term being modified. |
||
584 | * @return bool Whether taxonomy is editable. |
||
585 | */ |
||
586 | public function taxonomy_can_save( $taxonomy ) { |
||
599 | |||
600 | /** |
||
601 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
602 | * @since 2.2.2.1 |
||
603 | */ |
||
604 | protected function maybe_enqueue_column_display_styles() { |
||
615 | |||
616 | /** |
||
617 | * Includes CMB2 styles |
||
618 | * @since 2.0.0 |
||
619 | */ |
||
620 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
633 | |||
634 | /** |
||
635 | * Includes CMB2 JS |
||
636 | * @since 2.0.0 |
||
637 | */ |
||
638 | public static function enqueue_cmb_js() { |
||
646 | 1 | ||
647 | } |
||
648 |
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.