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 | * CMB taxonomies array for term meta |
||
39 | * @var array |
||
40 | * @since 2.2.0 |
||
41 | */ |
||
42 | protected $taxonomies = 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() { |
||
85 | |||
86 | public function comment_hooks() { |
||
90 | |||
91 | public function user_hooks() { |
||
102 | |||
103 | public function term_hooks() { |
||
137 | |||
138 | /** |
||
139 | * Registers styles for CMB2 |
||
140 | * @since 2.0.7 |
||
141 | */ |
||
142 | 1 | protected static function register_styles() { |
|
158 | |||
159 | /** |
||
160 | * Registers scripts for CMB2 |
||
161 | * @since 2.0.7 |
||
162 | */ |
||
163 | 1 | protected static function register_js() { |
|
173 | |||
174 | /** |
||
175 | * Registers scripts and styles for CMB2 |
||
176 | * @since 1.0.0 |
||
177 | */ |
||
178 | public static function register_scripts() { |
||
182 | |||
183 | /** |
||
184 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
185 | * @since 1.0.0 |
||
186 | */ |
||
187 | public function do_scripts( $hook ) { |
||
211 | |||
212 | /** |
||
213 | * Add metaboxes (to 'post' or 'comment' object types) |
||
214 | * @since 1.0.0 |
||
215 | */ |
||
216 | public function add_metaboxes() { |
||
242 | |||
243 | /** |
||
244 | * Add 'closed' class to metabox |
||
245 | * @since 2.0.0 |
||
246 | * @param array $classes Array of classes |
||
247 | * @return array Modified array of classes |
||
248 | */ |
||
249 | public function close_metabox_class( $classes ) { |
||
253 | |||
254 | /** |
||
255 | * Display metaboxes for a post or comment object |
||
256 | * @since 1.0.0 |
||
257 | */ |
||
258 | public function metabox_callback() { |
||
262 | |||
263 | /** |
||
264 | * Display metaboxes for new user page |
||
265 | * @since 1.0.0 |
||
266 | */ |
||
267 | public function user_new_metabox( $section ) { |
||
274 | |||
275 | /** |
||
276 | * Display metaboxes for a user object |
||
277 | * @since 1.0.0 |
||
278 | */ |
||
279 | public function user_metabox() { |
||
282 | |||
283 | /** |
||
284 | * Display metaboxes for a taxonomy term object |
||
285 | * @since 2.2.0 |
||
286 | */ |
||
287 | public function term_metabox() { |
||
290 | |||
291 | /** |
||
292 | * Display metaboxes for an object type |
||
293 | * @since 2.2.0 |
||
294 | * @param string $type Object type |
||
295 | * @return void |
||
296 | */ |
||
297 | public function show_form_for_type( $type ) { |
||
315 | |||
316 | /** |
||
317 | * Determines if metabox should be shown in current context |
||
318 | * @since 2.0.0 |
||
319 | * @return bool Whether metabox should be added/shown |
||
320 | */ |
||
321 | public function show_on() { |
||
336 | |||
337 | /** |
||
338 | * Get the CMB priority property set to numeric hook priority. |
||
339 | * @since 2.2.0 |
||
340 | * @param integer $default Default display hook priority. |
||
341 | * @return integer Hook priority. |
||
342 | */ |
||
343 | public function get_priority( $default = 10 ) { |
||
365 | |||
366 | /** |
||
367 | * Save data from post metabox |
||
368 | * @since 1.0.0 |
||
369 | * @param int $post_id Post ID |
||
370 | * @param mixed $post Post object |
||
371 | * @return null |
||
372 | */ |
||
373 | public function save_post( $post_id, $post = false ) { |
||
392 | |||
393 | /** |
||
394 | * Save data from comment metabox |
||
395 | * @since 2.0.9 |
||
396 | * @param int $comment_id Comment ID |
||
397 | * @return null |
||
398 | */ |
||
399 | public function save_comment( $comment_id ) { |
||
407 | |||
408 | /** |
||
409 | * Save data from user fields |
||
410 | * @since 1.0.x |
||
411 | * @param int $user_id User ID |
||
412 | * @return null |
||
413 | */ |
||
414 | public function save_user( $user_id ) { |
||
420 | |||
421 | /** |
||
422 | * Save data from term fields |
||
423 | * @since 2.2.0 |
||
424 | * @param int $term_id Term ID |
||
425 | * @param int $tt_id Term Taxonomy ID |
||
426 | * @param string $taxonomy Taxonomy |
||
427 | * @return null |
||
428 | */ |
||
429 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
437 | |||
438 | /** |
||
439 | * Delete term meta when a term is deleted. |
||
440 | * @since 2.2.0 |
||
441 | * @param int $term_id Term ID |
||
442 | * @param int $tt_id Term Taxonomy ID |
||
443 | * @param string $taxonomy Taxonomy |
||
444 | * @return null |
||
445 | */ |
||
446 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
456 | |||
457 | /** |
||
458 | * Determines if the current object is able to be saved |
||
459 | * @since 2.0.9 |
||
460 | * @param string $type Current post_type or comment_type |
||
461 | * @return bool Whether object can be saved |
||
462 | */ |
||
463 | public function can_save( $type = '' ) { |
||
475 | |||
476 | /** |
||
477 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
478 | * @since 2.2.0 |
||
479 | * @param string $taxonomy Taxonomy of term being modified. |
||
480 | * @return bool Whether taxonomy is editable. |
||
481 | */ |
||
482 | public function taxonomy_can_save( $taxonomy ) { |
||
495 | |||
496 | /** |
||
497 | * Includes CMB2 styles |
||
498 | * @since 2.0.0 |
||
499 | */ |
||
500 | 1 | public static function enqueue_cmb_css() { |
|
508 | |||
509 | /** |
||
510 | * Includes CMB2 JS |
||
511 | * @since 2.0.0 |
||
512 | */ |
||
513 | 1 | public static function enqueue_cmb_js() { |
|
521 | |||
522 | } |
||
523 |
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.