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 ) { |
||
70 | |||
71 | public function universal_hooks() { |
||
84 | |||
85 | public function post_hooks() { |
||
98 | |||
99 | public function comment_hooks() { |
||
108 | |||
109 | public function user_hooks() { |
||
125 | |||
126 | public function term_hooks() { |
||
165 | |||
166 | /** |
||
167 | * Registers styles for CMB2 |
||
168 | * @since 2.0.7 |
||
169 | */ |
||
170 | 1 | protected static function register_styles() { |
|
187 | |||
188 | /** |
||
189 | * Registers scripts for CMB2 |
||
190 | * @since 2.0.7 |
||
191 | */ |
||
192 | 1 | protected static function register_js() { |
|
202 | |||
203 | /** |
||
204 | * Registers scripts and styles for CMB2 |
||
205 | * @since 1.0.0 |
||
206 | */ |
||
207 | public static function register_scripts() { |
||
211 | |||
212 | /** |
||
213 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
214 | * @since 1.0.0 |
||
215 | */ |
||
216 | public function do_scripts( $hook ) { |
||
240 | |||
241 | /** |
||
242 | * Register the CMB2 field column headers. |
||
243 | * @since 2.2.2 |
||
244 | */ |
||
245 | public function register_column_headers( $columns ) { |
||
272 | |||
273 | /** |
||
274 | * The CMB2 field column display output. |
||
275 | * @since 2.2.2 |
||
276 | */ |
||
277 | public function column_display( $column_name, $object_id ) { |
||
289 | |||
290 | /** |
||
291 | * Returns the column display. |
||
292 | * @since 2.2.2 |
||
293 | */ |
||
294 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
301 | |||
302 | /** |
||
303 | * Add metaboxes (to 'post' or 'comment' object types) |
||
304 | * @since 1.0.0 |
||
305 | */ |
||
306 | public function add_metaboxes() { |
||
333 | |||
334 | /** |
||
335 | * Add 'closed' class to metabox |
||
336 | * @since 2.0.0 |
||
337 | * @param array $classes Array of classes |
||
338 | * @return array Modified array of classes |
||
339 | */ |
||
340 | public function close_metabox_class( $classes ) { |
||
344 | |||
345 | /** |
||
346 | * Display metaboxes for a post or comment object |
||
347 | * @since 1.0.0 |
||
348 | */ |
||
349 | public function metabox_callback() { |
||
353 | |||
354 | /** |
||
355 | * Display metaboxes for new user page |
||
356 | * @since 1.0.0 |
||
357 | */ |
||
358 | public function user_new_metabox( $section ) { |
||
365 | |||
366 | /** |
||
367 | * Display metaboxes for a user object |
||
368 | * @since 1.0.0 |
||
369 | */ |
||
370 | public function user_metabox() { |
||
373 | |||
374 | /** |
||
375 | * Display metaboxes for a taxonomy term object |
||
376 | * @since 2.2.0 |
||
377 | */ |
||
378 | public function term_metabox() { |
||
381 | |||
382 | /** |
||
383 | * Display metaboxes for an object type |
||
384 | * @since 2.2.0 |
||
385 | * @param string $type Object type |
||
386 | * @return void |
||
387 | */ |
||
388 | public function show_form_for_type( $type ) { |
||
406 | |||
407 | /** |
||
408 | * Determines if metabox should be shown in current context |
||
409 | * @since 2.0.0 |
||
410 | * @return bool Whether metabox should be added/shown |
||
411 | */ |
||
412 | public function show_on() { |
||
427 | |||
428 | /** |
||
429 | * Get the CMB priority property set to numeric hook priority. |
||
430 | * @since 2.2.0 |
||
431 | * @param integer $default Default display hook priority. |
||
432 | * @return integer Hook priority. |
||
433 | */ |
||
434 | public function get_priority( $default = 10 ) { |
||
456 | |||
457 | /** |
||
458 | * Save data from post metabox |
||
459 | * @since 1.0.0 |
||
460 | * @param int $post_id Post ID |
||
461 | * @param mixed $post Post object |
||
462 | * @return null |
||
463 | */ |
||
464 | public function save_post( $post_id, $post = false ) { |
||
483 | |||
484 | /** |
||
485 | * Save data from comment metabox |
||
486 | * @since 2.0.9 |
||
487 | * @param int $comment_id Comment ID |
||
488 | * @return null |
||
489 | */ |
||
490 | public function save_comment( $comment_id ) { |
||
498 | |||
499 | /** |
||
500 | * Save data from user fields |
||
501 | * @since 1.0.x |
||
502 | * @param int $user_id User ID |
||
503 | * @return null |
||
504 | */ |
||
505 | public function save_user( $user_id ) { |
||
511 | |||
512 | /** |
||
513 | * Save data from term fields |
||
514 | * @since 2.2.0 |
||
515 | * @param int $term_id Term ID |
||
516 | * @param int $tt_id Term Taxonomy ID |
||
517 | * @param string $taxonomy Taxonomy |
||
518 | * @return null |
||
519 | */ |
||
520 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
528 | |||
529 | /** |
||
530 | * Delete term meta when a term is deleted. |
||
531 | * @since 2.2.0 |
||
532 | * @param int $term_id Term ID |
||
533 | * @param int $tt_id Term Taxonomy ID |
||
534 | * @param string $taxonomy Taxonomy |
||
535 | * @return null |
||
536 | */ |
||
537 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
547 | |||
548 | /** |
||
549 | * Determines if the current object is able to be saved |
||
550 | * @since 2.0.9 |
||
551 | * @param string $type Current post_type or comment_type |
||
552 | * @return bool Whether object can be saved |
||
553 | */ |
||
554 | public function can_save( $type = '' ) { |
||
566 | |||
567 | /** |
||
568 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
569 | * @since 2.2.0 |
||
570 | * @param string $taxonomy Taxonomy of term being modified. |
||
571 | * @return bool Whether taxonomy is editable. |
||
572 | */ |
||
573 | public function taxonomy_can_save( $taxonomy ) { |
||
586 | |||
587 | /** |
||
588 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
589 | * @since 2.2.2.1 |
||
590 | */ |
||
591 | protected function maybe_enqueue_column_display_styles() { |
||
602 | |||
603 | /** |
||
604 | * Includes CMB2 styles |
||
605 | * @since 2.0.0 |
||
606 | */ |
||
607 | 1 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
|
620 | |||
621 | /** |
||
622 | * Includes CMB2 JS |
||
623 | * @since 2.0.0 |
||
624 | */ |
||
625 | 1 | public static function enqueue_cmb_js() { |
|
633 | |||
634 | } |
||
635 |
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.