Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class Container { |
||
14 | /** |
||
15 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
16 | */ |
||
17 | const TABS_TAIL = 1; |
||
18 | const TABS_HEAD = 2; |
||
19 | |||
20 | /** |
||
21 | * List of registered unique panel identificators |
||
22 | * |
||
23 | * @see verify_unique_panel_id() |
||
24 | * @var array |
||
25 | */ |
||
26 | public static $registered_panel_ids = array(); |
||
27 | |||
28 | /** |
||
29 | * List of registered unique field names |
||
30 | * |
||
31 | * @see verify_unique_field_name() |
||
32 | * @var array |
||
33 | */ |
||
34 | static protected $registered_field_names = array(); |
||
35 | |||
36 | /** |
||
37 | * List of containers created via factory that |
||
38 | * should be initialized |
||
39 | * |
||
40 | * @see verify_unique_field_name() |
||
41 | * @var array |
||
42 | */ |
||
43 | static protected $init_containers = array(); |
||
44 | |||
45 | /** |
||
46 | * List of containers attached to the current page view |
||
47 | * |
||
48 | * @see _attach() |
||
49 | * @var array |
||
50 | */ |
||
51 | public static $active_containers = array(); |
||
52 | |||
53 | /** |
||
54 | * List of fields attached to the current page view |
||
55 | * |
||
56 | * @see _attach() |
||
57 | * @var array |
||
58 | */ |
||
59 | static protected $active_fields = array(); |
||
60 | |||
61 | /** |
||
62 | * Stores all the container Backbone templates |
||
63 | * |
||
64 | * @see factory() |
||
65 | * @see add_template() |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $templates = array(); |
||
69 | |||
70 | /** |
||
71 | * Tabs available |
||
72 | */ |
||
73 | protected $tabs = array(); |
||
74 | |||
75 | /** |
||
76 | * List of default container settings |
||
77 | * |
||
78 | * @see init() |
||
79 | * @var array |
||
80 | */ |
||
81 | public $settings = array(); |
||
82 | |||
83 | /** |
||
84 | * Title of the container |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | public $title = ''; |
||
89 | |||
90 | /** |
||
91 | * Whether the container was setup |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | public $setup_ready = false; |
||
96 | |||
97 | /** |
||
98 | * List of notification messages to be displayed on the front-end |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $notifications = array(); |
||
103 | |||
104 | /** |
||
105 | * List of error messages to be displayed on the front-end |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $errors = array(); |
||
110 | |||
111 | /** |
||
112 | * List of container fields |
||
113 | * |
||
114 | * @see add_fields() |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $fields = array(); |
||
118 | |||
119 | /** |
||
120 | * Container DataStore. Propagated to all container fields |
||
121 | * |
||
122 | * @see set_datastore() |
||
123 | * @see get_datastore() |
||
124 | * @var object |
||
125 | */ |
||
126 | protected $store; |
||
127 | |||
128 | /** |
||
129 | * Create a new container of type $type and name $name and label $label. |
||
130 | * |
||
131 | * @param string $type |
||
132 | * @param string $name Human-readable name of the container |
||
133 | * @return object $container |
||
134 | **/ |
||
135 | 11 | public static function factory( $type, $name ) { |
|
157 | |||
158 | /** |
||
159 | * An alias of factory(). |
||
160 | * |
||
161 | * @see Container::factory() |
||
162 | **/ |
||
163 | 11 | public static function make( $type, $name ) { |
|
166 | |||
167 | /** |
||
168 | * Initialize containers created via factory |
||
169 | * |
||
170 | * @return object |
||
171 | **/ |
||
172 | public static function init_containers() { |
||
179 | |||
180 | /** |
||
181 | * Returns all the active containers created via factory |
||
182 | * |
||
183 | * @return array |
||
184 | **/ |
||
185 | public static function get_active_containers() { |
||
188 | |||
189 | /** |
||
190 | * Adds a container to the active containers array and triggers an action |
||
191 | **/ |
||
192 | public static function activate_container( $container ) { |
||
199 | |||
200 | /** |
||
201 | * Returns all the active fields created via factory |
||
202 | * |
||
203 | * @return array |
||
204 | **/ |
||
205 | public static function get_active_fields() { |
||
208 | |||
209 | /** |
||
210 | * Adds a field to the active fields array and triggers an action |
||
211 | **/ |
||
212 | public static function activate_field( $field ) { |
||
227 | |||
228 | /** |
||
229 | * Perform instance initialization after calling setup() |
||
230 | **/ |
||
231 | abstract public function init(); |
||
232 | |||
233 | /** |
||
234 | * Prints the container Underscore template |
||
235 | **/ |
||
236 | public function template() { |
||
263 | |||
264 | /** |
||
265 | * Create a new container |
||
266 | * |
||
267 | * @param string $title Unique title of the container |
||
268 | **/ |
||
269 | 9 | public function __construct( $title ) { |
|
279 | |||
280 | /** |
||
281 | * Boot the container once it's attached. |
||
282 | **/ |
||
283 | public function boot() { |
||
289 | |||
290 | /** |
||
291 | * Update container settings and begin initialization |
||
292 | * |
||
293 | * @see init() |
||
294 | * @param array $settings |
||
295 | * @return object $this |
||
296 | **/ |
||
297 | public function setup( $settings = array() ) { |
||
316 | |||
317 | /** |
||
318 | * Check if all required container settings have been specified |
||
319 | * |
||
320 | * @param array $settings Container settings |
||
321 | **/ |
||
322 | public function check_setup_settings( &$settings = array() ) { |
||
328 | |||
329 | /** |
||
330 | * Called first as part of the container save procedure. |
||
331 | * Responsible for checking the request validity and |
||
332 | * calling the container-specific save() method |
||
333 | * |
||
334 | * @see save() |
||
335 | * @see is_valid_save() |
||
336 | **/ |
||
337 | public function _save() { |
||
343 | |||
344 | /** |
||
345 | * Load submitted data and save each field in the container |
||
346 | * |
||
347 | * @see is_valid_save() |
||
348 | **/ |
||
349 | public function save( $data ) { |
||
355 | |||
356 | /** |
||
357 | * Checks whether the current request is valid |
||
358 | * |
||
359 | * @return bool |
||
360 | **/ |
||
361 | public function is_valid_save() { |
||
364 | |||
365 | /** |
||
366 | * Load the value for each field in the container. |
||
367 | * Could be used internally during container rendering |
||
368 | **/ |
||
369 | public function load() { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Called first as part of the container attachment procedure. |
||
378 | * Responsible for checking it's OK to attach the container |
||
379 | * and if it is, calling the container-specific attach() method |
||
380 | * |
||
381 | * @see attach() |
||
382 | * @see is_valid_attach() |
||
383 | **/ |
||
384 | public function _attach() { |
||
399 | |||
400 | /** |
||
401 | * Returns all the Backbone templates |
||
402 | * |
||
403 | * @return array |
||
404 | **/ |
||
405 | public function get_templates() { |
||
408 | |||
409 | /** |
||
410 | * Adds a new Backbone template |
||
411 | **/ |
||
412 | public function add_template( $name, $callback ) { |
||
415 | |||
416 | /** |
||
417 | * Attach the container rendering and helping methods |
||
418 | * to concrete WordPress Action hooks |
||
419 | **/ |
||
420 | public function attach() {} |
||
421 | |||
422 | /** |
||
423 | * Perform checks whether the container is active for current request |
||
424 | * |
||
425 | * @return bool True if the container is active |
||
426 | **/ |
||
427 | public function is_active() { |
||
430 | |||
431 | /** |
||
432 | * Perform checks whether the container should be attached during the current request |
||
433 | * |
||
434 | * @return bool True if the container is allowed to be attached |
||
435 | **/ |
||
436 | public function is_valid_attach() { |
||
439 | |||
440 | /** |
||
441 | * Revert the result of attach() |
||
442 | **/ |
||
443 | public function detach() { |
||
451 | |||
452 | /** |
||
453 | * Append array of fields to the current fields set. All items of the array |
||
454 | * must be instances of Field and their names should be unique for all |
||
455 | * Carbon containers. |
||
456 | * If a field does not have DataStore already, the container data store is |
||
457 | * assigned to them instead. |
||
458 | * |
||
459 | * @param array $fields |
||
460 | * @return object $this |
||
461 | **/ |
||
462 | public function add_fields( $fields ) { |
||
480 | |||
481 | /** |
||
482 | * Configuration function for adding tab with fields |
||
483 | * |
||
484 | * @param string $tab_name |
||
485 | * @param array $fields |
||
486 | * @return object $this |
||
487 | */ |
||
488 | public function add_tab( $tab_name, $fields ) { |
||
496 | |||
497 | /** |
||
498 | * Internal function that creates the tab and associates it with particular field set |
||
499 | * |
||
500 | * @param string $tab_name |
||
501 | * @param array $fields |
||
502 | * @param int $queue_end |
||
503 | * @return object $this |
||
504 | */ |
||
505 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
526 | |||
527 | /** |
||
528 | * Whether the container is tabbed or not |
||
529 | * |
||
530 | * @return bool |
||
531 | */ |
||
532 | public function is_tabbed() { |
||
535 | |||
536 | /** |
||
537 | * Retrieve all fields that are not defined under a specific tab |
||
538 | * |
||
539 | * @return array |
||
540 | */ |
||
541 | public function get_untabbed_fields() { |
||
563 | |||
564 | /** |
||
565 | * Retrieve all tabs. |
||
566 | * Create a default tab if there are any untabbed fields. |
||
567 | * |
||
568 | * @return array |
||
569 | */ |
||
570 | public function get_tabs() { |
||
579 | |||
580 | /** |
||
581 | * Build the tabs JSON |
||
582 | * |
||
583 | * @return array |
||
584 | */ |
||
585 | public function get_tabs_json() { |
||
597 | |||
598 | /** |
||
599 | * Returns the private container array of fields. |
||
600 | * Use only if you are completely aware of what you are doing. |
||
601 | * |
||
602 | * @return array |
||
603 | **/ |
||
604 | public function get_fields() { |
||
607 | |||
608 | /** |
||
609 | * Perform a check whether the current container has fields |
||
610 | * |
||
611 | * @return bool |
||
612 | **/ |
||
613 | public function has_fields() { |
||
616 | |||
617 | /** |
||
618 | * Perform checks whether there is a container registered with identificator $id |
||
619 | */ |
||
620 | public static function verify_unique_panel_id( $id ) { |
||
627 | |||
628 | |||
629 | /** |
||
630 | * Remove container identificator $id from the list of unique container ids |
||
631 | * |
||
632 | * @param string $id |
||
633 | **/ |
||
634 | public static function drop_unique_panel_id( $id ) { |
||
639 | |||
640 | /** |
||
641 | * Perform checks whether there is a field registered with the name $name. |
||
642 | * If not, the field name is recorded. |
||
643 | * |
||
644 | * @param string $name |
||
645 | **/ |
||
646 | public function verify_unique_field_name( $name ) { |
||
653 | |||
654 | /** |
||
655 | * Remove field name $name from the list of unique field names |
||
656 | * |
||
657 | * @param string $name |
||
658 | **/ |
||
659 | public function drop_unique_field_name( $name ) { |
||
665 | |||
666 | /** |
||
667 | * Assign DataStore instance for use by the container fields |
||
668 | * |
||
669 | * @param object $store |
||
670 | * @return object $this |
||
671 | **/ |
||
672 | public function set_datastore( $store ) { |
||
681 | |||
682 | /** |
||
683 | * Return the DataStore instance used by container fields |
||
684 | * |
||
685 | * @return object $store |
||
686 | **/ |
||
687 | public function get_datastore() { |
||
690 | |||
691 | /** |
||
692 | * Return WordPress nonce name used to identify the current container instance |
||
693 | * |
||
694 | * @return string |
||
695 | **/ |
||
696 | public function get_nonce_name() { |
||
699 | |||
700 | /** |
||
701 | * Return WordPress nonce field |
||
702 | * |
||
703 | * @return string |
||
704 | **/ |
||
705 | public function get_nonce_field() { |
||
708 | |||
709 | /** |
||
710 | * Returns an array that holds the container data, suitable for JSON representation. |
||
711 | * This data will be available in the Underscore template and the Backbone Model. |
||
712 | * |
||
713 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
714 | * @return array |
||
715 | */ |
||
716 | public function to_json( $load ) { |
||
733 | |||
734 | /** |
||
735 | * Underscore template for tabs |
||
736 | */ |
||
737 | public function template_tabs() { |
||
756 | |||
757 | /** |
||
758 | * Enqueue admin scripts |
||
759 | */ |
||
760 | public static function admin_hook_scripts() { |
||
770 | |||
771 | /** |
||
772 | * Enqueue admin styles |
||
773 | */ |
||
774 | public static function admin_hook_styles() { |
||
777 | } // END Container |
||
778 | |||
779 |