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() { |
||
77 | |||
78 | public function post_hooks() { |
||
86 | |||
87 | public function comment_hooks() { |
||
93 | |||
94 | public function user_hooks() { |
||
105 | |||
106 | public function term_hooks() { |
||
141 | |||
142 | /** |
||
143 | * Registers styles for CMB2 |
||
144 | * @since 2.0.7 |
||
145 | */ |
||
146 | 1 | protected static function register_styles() { |
|
162 | |||
163 | /** |
||
164 | * Registers scripts for CMB2 |
||
165 | * @since 2.0.7 |
||
166 | */ |
||
167 | 1 | protected static function register_js() { |
|
177 | |||
178 | /** |
||
179 | * Registers scripts and styles for CMB2 |
||
180 | * @since 1.0.0 |
||
181 | */ |
||
182 | public static function register_scripts() { |
||
186 | |||
187 | /** |
||
188 | * Enqueues scripts and styles for CMB2 |
||
189 | * @since 1.0.0 |
||
190 | */ |
||
191 | public function do_scripts( $hook ) { |
||
202 | |||
203 | /** |
||
204 | * Add metaboxes (to 'post' or 'comment' object types) |
||
205 | * @since 1.0.0 |
||
206 | */ |
||
207 | public function add_metaboxes() { |
||
233 | |||
234 | /** |
||
235 | * Add 'closed' class to metabox |
||
236 | * @since 2.0.0 |
||
237 | * @param array $classes Array of classes |
||
238 | * @return array Modified array of classes |
||
239 | */ |
||
240 | public function close_metabox_class( $classes ) { |
||
244 | |||
245 | /** |
||
246 | * Display metaboxes for a post or comment object |
||
247 | * @since 1.0.0 |
||
248 | */ |
||
249 | public function metabox_callback() { |
||
253 | |||
254 | /** |
||
255 | * Display metaboxes for new user page |
||
256 | * @since 1.0.0 |
||
257 | */ |
||
258 | public function user_new_metabox( $section ) { |
||
265 | |||
266 | /** |
||
267 | * Display metaboxes for a user object |
||
268 | * @since 1.0.0 |
||
269 | */ |
||
270 | public function user_metabox() { |
||
273 | |||
274 | /** |
||
275 | * Display metaboxes for a taxonomy term object |
||
276 | * @since 2.2.0 |
||
277 | */ |
||
278 | public function term_metabox() { |
||
281 | |||
282 | /** |
||
283 | * Display metaboxes for an object type |
||
284 | * @since 2.2.0 |
||
285 | * @param string $type Object type |
||
286 | * @return void |
||
287 | */ |
||
288 | public function show_form_for_type( $type ) { |
||
306 | |||
307 | /** |
||
308 | * Determines if metabox should be shown in current context |
||
309 | * @since 2.0.0 |
||
310 | * @return bool Whether metabox should be added/shown |
||
311 | */ |
||
312 | public function show_on() { |
||
327 | |||
328 | /** |
||
329 | * Get the CMB priority property set to numeric hook priority. |
||
330 | * @since 2.2.0 |
||
331 | * @param integer $default Default display hook priority. |
||
332 | * @return integer Hook priority. |
||
333 | */ |
||
334 | public function get_priority( $default = 10 ) { |
||
356 | |||
357 | /** |
||
358 | * Save data from post metabox |
||
359 | * @since 1.0.0 |
||
360 | * @param int $post_id Post ID |
||
361 | * @param mixed $post Post object |
||
362 | * @return null |
||
363 | */ |
||
364 | public function save_post( $post_id, $post = false ) { |
||
383 | |||
384 | /** |
||
385 | * Save data from comment metabox |
||
386 | * @since 2.0.9 |
||
387 | * @param int $comment_id Comment ID |
||
388 | * @return null |
||
389 | */ |
||
390 | public function save_comment( $comment_id ) { |
||
398 | |||
399 | /** |
||
400 | * Save data from user fields |
||
401 | * @since 1.0.x |
||
402 | * @param int $user_id User ID |
||
403 | * @return null |
||
404 | */ |
||
405 | public function save_user( $user_id ) { |
||
411 | |||
412 | /** |
||
413 | * Save data from term fields |
||
414 | * @since 2.2.0 |
||
415 | * @param int $term_id Term ID |
||
416 | * @param int $tt_id Term Taxonomy ID |
||
417 | * @param string $taxonomy Taxonomy |
||
418 | * @return null |
||
419 | */ |
||
420 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
428 | |||
429 | /** |
||
430 | * Delete term meta when a term is deleted. |
||
431 | * @since 2.2.0 |
||
432 | * @param int $term_id Term ID |
||
433 | * @param int $tt_id Term Taxonomy ID |
||
434 | * @param string $taxonomy Taxonomy |
||
435 | * @return null |
||
436 | */ |
||
437 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
447 | |||
448 | /** |
||
449 | * Determines if the current object is able to be saved |
||
450 | * @since 2.0.9 |
||
451 | * @param string $type Current post_type or comment_type |
||
452 | * @return bool Whether object can be saved |
||
453 | */ |
||
454 | public function can_save( $type = '' ) { |
||
466 | |||
467 | /** |
||
468 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
469 | * @since 2.2.0 |
||
470 | * @param string $taxonomy Taxonomy of term being modified. |
||
471 | * @return bool Whether taxonomy is editable. |
||
472 | */ |
||
473 | public function taxonomy_can_save( $taxonomy ) { |
||
486 | |||
487 | /** |
||
488 | * Includes CMB2 styles |
||
489 | * @since 2.0.0 |
||
490 | */ |
||
491 | 1 | public static function enqueue_cmb_css() { |
|
499 | |||
500 | /** |
||
501 | * Includes CMB2 JS |
||
502 | * @since 2.0.0 |
||
503 | */ |
||
504 | 1 | public static function enqueue_cmb_js() { |
|
512 | |||
513 | } |
||
514 |
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.