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 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 get_unique_panel_id() |
||
24 | * @var array |
||
25 | */ |
||
26 | public static $registered_panel_ids = array(); |
||
27 | |||
28 | /** |
||
29 | * List of containers created via factory that |
||
30 | * should be initialized |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $init_containers = array(); |
||
35 | |||
36 | /** |
||
37 | * List of containers attached to the current page view |
||
38 | * |
||
39 | * @see _attach() |
||
40 | * @var array |
||
41 | */ |
||
42 | public static $active_containers = array(); |
||
43 | |||
44 | /** |
||
45 | * List of fields attached to the current page view |
||
46 | * |
||
47 | * @see _attach() |
||
48 | * @var array |
||
49 | */ |
||
50 | protected static $active_fields = array(); |
||
51 | |||
52 | /** |
||
53 | * List of registered unique field names for this container instance |
||
54 | * |
||
55 | * @see verify_unique_field_name() |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $registered_field_names = array(); |
||
1 ignored issue
–
show
|
|||
59 | |||
60 | /** |
||
61 | * Stores all the container Backbone templates |
||
62 | * |
||
63 | * @see factory() |
||
64 | * @see add_template() |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $templates = array(); |
||
68 | |||
69 | /** |
||
70 | * Tabs available |
||
71 | */ |
||
72 | protected $tabs = array(); |
||
73 | |||
74 | /** |
||
75 | * List of default container settings |
||
76 | * |
||
77 | * @see init() |
||
78 | * @var array |
||
79 | */ |
||
80 | public $settings = array(); |
||
81 | |||
82 | /** |
||
83 | * Title of the container |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | public $title = ''; |
||
88 | |||
89 | /** |
||
90 | * Whether the container was setup |
||
91 | * |
||
92 | * @var bool |
||
93 | */ |
||
94 | public $setup_ready = false; |
||
95 | |||
96 | /** |
||
97 | * List of notification messages to be displayed on the front-end |
||
98 | * |
||
99 | * @var array |
||
100 | */ |
||
101 | protected $notifications = array(); |
||
102 | |||
103 | /** |
||
104 | * List of error messages to be displayed on the front-end |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $errors = array(); |
||
109 | |||
110 | /** |
||
111 | * List of container fields |
||
112 | * |
||
113 | * @see add_fields() |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $fields = array(); |
||
117 | |||
118 | /** |
||
119 | * Container DataStore. Propagated to all container fields |
||
120 | * |
||
121 | * @see set_datastore() |
||
122 | * @see get_datastore() |
||
123 | * @var object |
||
124 | */ |
||
125 | protected $store; |
||
126 | |||
127 | /** |
||
128 | * Whether the container should be included in the response of the requests to the REST API |
||
129 | * |
||
130 | * @see set_rest_visibility |
||
131 | * @see get_rest_visibility |
||
132 | * @var boolean |
||
133 | */ |
||
134 | 11 | protected $visible_in_rest = true; |
|
135 | |||
136 | 11 | /** |
|
137 | 1 | * Create a new container of type $type and name $name and label $label. |
|
138 | 1 | * |
|
139 | * @param string $type |
||
140 | 11 | * @param string $name Human-readable name of the container |
|
141 | * @return object $container |
||
142 | 11 | **/ |
|
143 | public static function factory( $type, $name ) { |
||
165 | |||
166 | /** |
||
167 | * An alias of factory(). |
||
168 | * |
||
169 | * @see Container::factory() |
||
170 | **/ |
||
171 | public static function make( $type, $name ) { |
||
174 | |||
175 | /** |
||
176 | * Initialize containers created via factory |
||
177 | * |
||
178 | * @return object |
||
179 | **/ |
||
180 | public static function init_containers() { |
||
187 | |||
188 | /** |
||
189 | * Returns all the active containers created via factory |
||
190 | * |
||
191 | * @return array |
||
192 | **/ |
||
193 | public static function get_active_containers() { |
||
196 | |||
197 | /** |
||
198 | * Adds a container to the active containers array and triggers an action |
||
199 | **/ |
||
200 | public static function activate_container( $container ) { |
||
207 | |||
208 | /** |
||
209 | * Returns all the active fields created via factory |
||
210 | * |
||
211 | * @return array |
||
212 | **/ |
||
213 | public static function get_active_fields() { |
||
216 | |||
217 | /** |
||
218 | * Adds a field to the active fields array and triggers an action |
||
219 | **/ |
||
220 | public static function activate_field( $field ) { |
||
235 | |||
236 | /** |
||
237 | * Perform instance initialization after calling setup() |
||
238 | **/ |
||
239 | abstract public function init(); |
||
240 | |||
241 | /** |
||
242 | * Prints the container Underscore template |
||
243 | **/ |
||
244 | public function template() { |
||
271 | |||
272 | /** |
||
273 | 8 | * Create a new container |
|
274 | 8 | * |
|
275 | 8 | * @param string $title Unique title of the container |
|
276 | **/ |
||
277 | 8 | public function __construct( $title ) { |
|
288 | |||
289 | /** |
||
290 | * Boot the container once it's attached. |
||
291 | **/ |
||
292 | public function boot() { |
||
298 | |||
299 | /** |
||
300 | * Update container settings and begin initialization |
||
301 | * |
||
302 | * @see init() |
||
303 | * @param array $settings |
||
304 | * @return object $this |
||
305 | **/ |
||
306 | public function setup( $settings = array() ) { |
||
325 | |||
326 | /** |
||
327 | * Check if all required container settings have been specified |
||
328 | * |
||
329 | * @param array $settings Container settings |
||
330 | **/ |
||
331 | public function check_setup_settings( &$settings = array() ) { |
||
337 | |||
338 | /** |
||
339 | * Called first as part of the container save procedure. |
||
340 | * Responsible for checking the request validity and |
||
341 | * calling the container-specific save() method |
||
342 | * |
||
343 | * @see save() |
||
344 | * @see is_valid_save() |
||
345 | **/ |
||
346 | public function _save() { |
||
352 | |||
353 | /** |
||
354 | * Load submitted data and save each field in the container |
||
355 | * |
||
356 | * @see is_valid_save() |
||
357 | **/ |
||
358 | public function save( $data ) { |
||
364 | |||
365 | /** |
||
366 | * Checks whether the current request is valid |
||
367 | * |
||
368 | * @return bool |
||
369 | **/ |
||
370 | public function is_valid_save() { |
||
373 | |||
374 | /** |
||
375 | * Load the value for each field in the container. |
||
376 | * Could be used internally during container rendering |
||
377 | **/ |
||
378 | public function load() { |
||
383 | |||
384 | |||
385 | /** |
||
386 | * Called first as part of the container attachment procedure. |
||
387 | * Responsible for checking it's OK to attach the container |
||
388 | * and if it is, calling the container-specific attach() method |
||
389 | * |
||
390 | * @see attach() |
||
391 | * @see is_valid_attach() |
||
392 | **/ |
||
393 | public function _attach() { |
||
403 | |||
404 | /** |
||
405 | * Attach all containers |
||
406 | * |
||
407 | */ |
||
408 | public function _attach_all() { |
||
411 | |||
412 | /** |
||
413 | * Calls the container-specific attach() method |
||
414 | */ |
||
415 | private function _attach_containers() { |
||
423 | |||
424 | /** |
||
425 | * Returns all the Backbone templates |
||
426 | * |
||
427 | * @return array |
||
428 | **/ |
||
429 | public function get_templates() { |
||
432 | |||
433 | /** |
||
434 | * Adds a new Backbone template |
||
435 | **/ |
||
436 | public function add_template( $name, $callback ) { |
||
439 | |||
440 | /** |
||
441 | * Attach the container rendering and helping methods |
||
442 | * to concrete WordPress Action hooks |
||
443 | **/ |
||
444 | public function attach() {} |
||
445 | |||
446 | /** |
||
447 | * Perform checks whether the container is active for current request |
||
448 | * |
||
449 | * @return bool True if the container is active |
||
450 | **/ |
||
451 | public function is_active() { |
||
454 | |||
455 | /** |
||
456 | * Perform checks whether the container should be attached during the current request |
||
457 | * |
||
458 | * @return bool True if the container is allowed to be attached |
||
459 | **/ |
||
460 | public function is_valid_attach() { |
||
463 | |||
464 | /** |
||
465 | * Revert the result of attach() |
||
466 | **/ |
||
467 | public function detach() { |
||
475 | |||
476 | /** |
||
477 | * Append array of fields to the current fields set. All items of the array |
||
478 | * must be instances of Field and their names should be unique for all |
||
479 | * Carbon containers. |
||
480 | * If a field does not have DataStore already, the container data store is |
||
481 | * assigned to them instead. |
||
482 | * |
||
483 | * @param array $fields |
||
484 | * @return object $this |
||
485 | **/ |
||
486 | public function add_fields( $fields ) { |
||
504 | |||
505 | /** |
||
506 | * Configuration function for adding tab with fields |
||
507 | * |
||
508 | * @param string $tab_name |
||
509 | * @param array $fields |
||
510 | * @return object $this |
||
511 | */ |
||
512 | public function add_tab( $tab_name, $fields ) { |
||
520 | |||
521 | /** |
||
522 | * Configuration function for setting the container visibility in the response of the requests to the REST API |
||
523 | * |
||
524 | * @param bool $visible |
||
525 | * @return object $this |
||
526 | */ |
||
527 | public function show_in_rest( $visible ) { |
||
532 | |||
533 | /** |
||
534 | * Set the REST visibility of the container |
||
535 | * |
||
536 | * @param bool $visible |
||
537 | */ |
||
538 | public function set_rest_visibility( $visible ) { |
||
541 | |||
542 | /** |
||
543 | * Get the REST visibility of the container |
||
544 | * |
||
545 | * @return bool True if the container is visible |
||
546 | */ |
||
547 | public function get_rest_visibility() { |
||
550 | |||
551 | /** |
||
552 | * Internal function that creates the tab and associates it with particular field set |
||
553 | * |
||
554 | * @param string $tab_name |
||
555 | * @param array $fields |
||
556 | * @param int $queue_end |
||
557 | * @return object $this |
||
558 | */ |
||
559 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
580 | |||
581 | /** |
||
582 | * Whether the container is tabbed or not |
||
583 | * |
||
584 | * @return bool |
||
585 | */ |
||
586 | public function is_tabbed() { |
||
589 | |||
590 | /** |
||
591 | * Retrieve all fields that are not defined under a specific tab |
||
592 | * |
||
593 | * @return array |
||
594 | */ |
||
595 | public function get_untabbed_fields() { |
||
617 | |||
618 | /** |
||
619 | * Retrieve all tabs. |
||
620 | * Create a default tab if there are any untabbed fields. |
||
621 | * |
||
622 | * @return array |
||
623 | */ |
||
624 | public function get_tabs() { |
||
633 | |||
634 | /** |
||
635 | * Build the tabs JSON |
||
636 | * |
||
637 | * @return array |
||
638 | */ |
||
639 | public function get_tabs_json() { |
||
651 | |||
652 | /** |
||
653 | * Returns the private container array of fields. |
||
654 | * Use only if you are completely aware of what you are doing. |
||
655 | * |
||
656 | * @return array |
||
657 | **/ |
||
658 | public function get_fields() { |
||
661 | |||
662 | /** |
||
663 | * Perform a check whether the current container has fields |
||
664 | * |
||
665 | * @return bool |
||
666 | **/ |
||
667 | public function has_fields() { |
||
670 | |||
671 | /** |
||
672 | * Perform checks whether there is a container registered with identificator $id |
||
673 | */ |
||
674 | public static function get_unique_panel_id( $id ) { |
||
685 | |||
686 | |||
687 | /** |
||
688 | * Remove container identificator $id from the list of unique container ids |
||
689 | * |
||
690 | * @param string $id |
||
691 | **/ |
||
692 | public static function drop_unique_panel_id( $id ) { |
||
697 | |||
698 | /** |
||
699 | * Perform checks whether there is a field registered with the name $name. |
||
700 | * If not, the field name is recorded. |
||
701 | * |
||
702 | * @param string $name |
||
703 | **/ |
||
704 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
711 | |||
712 | /** |
||
713 | * Remove field name $name from the list of unique field names |
||
714 | * |
||
715 | * @param string $name |
||
716 | **/ |
||
717 | public function drop_unique_field_name( $name ) { |
||
724 | |||
725 | /** |
||
726 | * Assign DataStore instance for use by the container fields |
||
727 | * |
||
728 | * @param object $store |
||
729 | * @return object $this |
||
730 | **/ |
||
731 | public function set_datastore( $store ) { |
||
740 | |||
741 | /** |
||
742 | * Return the DataStore instance used by container fields |
||
743 | * |
||
744 | * @return object $store |
||
745 | **/ |
||
746 | public function get_datastore() { |
||
749 | |||
750 | /** |
||
751 | * Return WordPress nonce name used to identify the current container instance |
||
752 | * |
||
753 | * @return string |
||
754 | **/ |
||
755 | public function get_nonce_name() { |
||
758 | |||
759 | /** |
||
760 | * Return WordPress nonce field |
||
761 | * |
||
762 | * @return string |
||
763 | **/ |
||
764 | public function get_nonce_field() { |
||
767 | |||
768 | /** |
||
769 | * Returns an array that holds the container data, suitable for JSON representation. |
||
770 | * This data will be available in the Underscore template and the Backbone Model. |
||
771 | * |
||
772 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
773 | * @return array |
||
774 | */ |
||
775 | public function to_json( $load ) { |
||
792 | |||
793 | /** |
||
794 | * Underscore template for tabs |
||
795 | */ |
||
796 | public function template_tabs() { |
||
815 | |||
816 | /** |
||
817 | * Enqueue admin scripts |
||
818 | */ |
||
819 | public static function admin_hook_scripts() { |
||
829 | |||
830 | /** |
||
831 | * Enqueue admin styles |
||
832 | */ |
||
833 | public static function admin_hook_styles() { |
||
836 | } // END Container |
||
837 | |||
838 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.