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() { |
||
80 | |||
81 | public function post_hooks() { |
||
89 | |||
90 | public function comment_hooks() { |
||
96 | |||
97 | public function user_hooks() { |
||
125 | |||
126 | public function term_hooks() { |
||
160 | |||
161 | /** |
||
162 | * Registers styles for CMB2 |
||
163 | * @since 2.0.7 |
||
164 | */ |
||
165 | protected static function register_styles() { |
||
181 | |||
182 | /** |
||
183 | * Registers scripts for CMB2 |
||
184 | 1 | * @since 2.0.7 |
|
185 | 1 | */ |
|
186 | 1 | protected static function register_js() { |
|
196 | |||
197 | /** |
||
198 | * Registers scripts and styles for CMB2 |
||
199 | 1 | * @since 1.0.0 |
|
200 | 1 | */ |
|
201 | public static function register_scripts() { |
||
205 | 1 | ||
206 | /** |
||
207 | 1 | * Enqueues scripts and styles for CMB2 |
|
208 | 1 | * @since 1.0.0 |
|
209 | */ |
||
210 | public function do_scripts( $hook ) { |
||
221 | |||
222 | /** |
||
223 | * Add metaboxes (to 'post' or 'comment' object types) |
||
224 | * @since 1.0.0 |
||
225 | */ |
||
226 | public function add_metaboxes() { |
||
252 | |||
253 | /** |
||
254 | * Add 'closed' class to metabox |
||
255 | * @since 2.0.0 |
||
256 | * @param array $classes Array of classes |
||
257 | * @return array Modified array of classes |
||
258 | */ |
||
259 | public function close_metabox_class( $classes ) { |
||
263 | |||
264 | /** |
||
265 | * Display metaboxes for a post or comment object |
||
266 | * @since 1.0.0 |
||
267 | */ |
||
268 | public function metabox_callback() { |
||
272 | |||
273 | /** |
||
274 | * Display metaboxes for new user page |
||
275 | * @since 1.0.0 |
||
276 | */ |
||
277 | public function user_new_metabox( $section ) { |
||
284 | |||
285 | /** |
||
286 | * Display metaboxes for a user object |
||
287 | * @since 1.0.0 |
||
288 | */ |
||
289 | public function user_metabox() { |
||
292 | |||
293 | /** |
||
294 | * Display metaboxes for a taxonomy term object |
||
295 | * @since 2.2.0 |
||
296 | */ |
||
297 | public function term_metabox() { |
||
300 | |||
301 | /** |
||
302 | * Display metaboxes for an object type |
||
303 | * @since 2.2.0 |
||
304 | * @param string $type Object type |
||
305 | * @return void |
||
306 | */ |
||
307 | public function show_form_for_type( $type ) { |
||
325 | |||
326 | /** |
||
327 | * Determines if metabox should be shown in current context |
||
328 | * @since 2.0.0 |
||
329 | * @return bool Whether metabox should be added/shown |
||
330 | */ |
||
331 | public function show_on() { |
||
346 | |||
347 | /** |
||
348 | * Save data from post metabox |
||
349 | * @since 1.0.0 |
||
350 | * @param int $post_id Post ID |
||
351 | * @param mixed $post Post object |
||
352 | * @return null |
||
353 | */ |
||
354 | public function save_post( $post_id, $post = false ) { |
||
373 | |||
374 | /** |
||
375 | * Save data from comment metabox |
||
376 | * @since 2.0.9 |
||
377 | * @param int $comment_id Comment ID |
||
378 | * @return null |
||
379 | */ |
||
380 | public function save_comment( $comment_id ) { |
||
388 | |||
389 | /** |
||
390 | * Save data from user fields |
||
391 | * @since 1.0.x |
||
392 | * @param int $user_id User ID |
||
393 | * @return null |
||
394 | */ |
||
395 | public function save_user( $user_id ) { |
||
401 | |||
402 | /** |
||
403 | * Save data from term fields |
||
404 | * @since 1.0.x |
||
405 | * @param int $term_id Term ID |
||
406 | * @param int $tt_id Term Taxonomy ID |
||
407 | * @param string $taxonomy Taxonomy |
||
408 | * @return null |
||
409 | */ |
||
410 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
418 | |||
419 | /** |
||
420 | * Delete term meta when a term is deleted. |
||
421 | * @since 1.0.x |
||
422 | * @param int $term_id Term ID |
||
423 | * @param int $tt_id Term Taxonomy ID |
||
424 | * @param string $taxonomy Taxonomy |
||
425 | * @return null |
||
426 | */ |
||
427 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
437 | |||
438 | /** |
||
439 | * Determines if the current object is able to be saved |
||
440 | * @since 2.0.9 |
||
441 | * @param string $type Current post_type or comment_type |
||
442 | * @return bool Whether object can be saved |
||
443 | */ |
||
444 | public function can_save( $type = '' ) { |
||
456 | |||
457 | /** |
||
458 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
459 | * @since 2.2.0 |
||
460 | * @param string $taxonomy Taxonomy of term being modified. |
||
461 | * @return bool Whether taxonomy is editable. |
||
462 | */ |
||
463 | public function taxonomy_can_save( $taxonomy ) { |
||
478 | |||
479 | /** |
||
480 | * Includes CMB2 styles |
||
481 | * @since 2.0.0 |
||
482 | */ |
||
483 | public static function enqueue_cmb_css() { |
||
491 | |||
492 | /** |
||
493 | * Includes CMB2 JS |
||
494 | * @since 2.0.0 |
||
495 | */ |
||
496 | public static function enqueue_cmb_js() { |
||
504 | |||
505 | } |
||
506 |
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.