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 |
||
16 | abstract class Container implements Datastore_Holder_Interface { |
||
17 | /** |
||
18 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
19 | */ |
||
20 | const TABS_TAIL = 1; |
||
21 | const TABS_HEAD = 2; |
||
22 | |||
23 | /** |
||
24 | * Stores if the container is active on the current page |
||
25 | * |
||
26 | * @see activate() |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $active = false; |
||
30 | |||
31 | /** |
||
32 | * List of registered unique field names for this container instance |
||
33 | * |
||
34 | * @see verify_unique_field_name() |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $registered_field_names = array(); |
||
38 | |||
39 | /** |
||
40 | * Stores all the container Backbone templates |
||
41 | * |
||
42 | * @see factory() |
||
43 | * @see add_template() |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $templates = array(); |
||
47 | |||
48 | /** |
||
49 | * Tabs available |
||
50 | */ |
||
51 | protected $tabs = array(); |
||
52 | |||
53 | /** |
||
54 | * List of default container settings |
||
55 | * |
||
56 | * @see init() |
||
57 | * @var array |
||
58 | */ |
||
59 | public $settings = array(); |
||
60 | |||
61 | /** |
||
62 | * Title of the container |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | public $title = ''; |
||
67 | |||
68 | /** |
||
69 | * List of notification messages to be displayed on the front-end |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $notifications = array(); |
||
74 | |||
75 | /** |
||
76 | * List of error messages to be displayed on the front-end |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $errors = array(); |
||
81 | |||
82 | /** |
||
83 | * List of container fields |
||
84 | * |
||
85 | * @see add_fields() |
||
86 | * @var array |
||
87 | */ |
||
88 | protected $fields = array(); |
||
89 | |||
90 | /** |
||
91 | * Container datastores. Propagated to all container fields |
||
92 | * |
||
93 | * @see set_datastore() |
||
94 | * @see get_datastore() |
||
95 | * @var object |
||
96 | */ |
||
97 | protected $datastore; |
||
98 | |||
99 | /** |
||
100 | * Flag whether the datastore is the default one or replaced with a custom one |
||
101 | * |
||
102 | * @see set_datastore() |
||
103 | * @see get_datastore() |
||
104 | * @var boolean |
||
105 | */ |
||
106 | protected $has_default_datastore = true; |
||
107 | |||
108 | /** |
||
109 | * Normalizes a container type string to an expected format |
||
110 | * |
||
111 | * @param string $type |
||
112 | * @return string $normalized_type |
||
113 | **/ |
||
114 | protected static function normalize_container_type( $type ) { |
||
123 | |||
124 | /** |
||
125 | * Resolves a string-based type to a fully qualified container class name |
||
126 | * |
||
127 | * @param string $type |
||
128 | * @return string $class_name |
||
129 | **/ |
||
130 | protected static function container_type_to_class( $type ) { |
||
138 | |||
139 | /** |
||
140 | * Create a new container of type $type and name $name. |
||
141 | * |
||
142 | * @param string $type |
||
143 | * @param string $name Human-readable name of the container |
||
144 | * @return object $container |
||
145 | **/ |
||
146 | 9 | public static function factory( $type, $name ) { |
|
157 | |||
158 | /** |
||
159 | * An alias of factory(). |
||
160 | * |
||
161 | * @see Container::factory() |
||
162 | **/ |
||
163 | public static function make( $type, $name ) { |
||
166 | |||
167 | /** |
||
168 | * Create a new container |
||
169 | * |
||
170 | * @param string $unique_id Unique id of the container |
||
171 | * @param string $title title of the container |
||
172 | * @param string $type Type of the container |
||
173 | **/ |
||
174 | 2 | public function __construct( $unique_id, $title, $type ) { |
|
185 | |||
186 | /** |
||
187 | * Return whether the container is active |
||
188 | **/ |
||
189 | public function active() { |
||
192 | |||
193 | /** |
||
194 | * Activate the container and trigger an action |
||
195 | **/ |
||
196 | protected function activate() { |
||
201 | |||
202 | /** |
||
203 | * Activates and boots a field recursively |
||
204 | **/ |
||
205 | protected function activate_field( $field ) { |
||
218 | |||
219 | /** |
||
220 | * Perform instance initialization |
||
221 | **/ |
||
222 | abstract public function init(); |
||
223 | |||
224 | /** |
||
225 | * Prints the container Underscore template |
||
226 | **/ |
||
227 | public function template() { |
||
254 | |||
255 | /** |
||
256 | * Boot the container once it's attached. |
||
257 | **/ |
||
258 | protected function boot() { |
||
264 | |||
265 | /** |
||
266 | * Called first as part of the container save procedure. |
||
267 | * Responsible for checking the request validity and |
||
268 | * calling the container-specific save() method |
||
269 | * |
||
270 | * @see save() |
||
271 | * @see is_valid_save() |
||
272 | **/ |
||
273 | public function _save() { |
||
279 | |||
280 | /** |
||
281 | * Load submitted data and save each field in the container |
||
282 | * |
||
283 | * @see is_valid_save() |
||
284 | **/ |
||
285 | public function save( $data ) { |
||
291 | |||
292 | /** |
||
293 | * Checks whether the current request is valid |
||
294 | * |
||
295 | * @return bool |
||
296 | **/ |
||
297 | public function is_valid_save() { |
||
300 | |||
301 | /** |
||
302 | * Load the value for each field in the container. |
||
303 | * Could be used internally during container rendering |
||
304 | **/ |
||
305 | public function load() { |
||
310 | |||
311 | /** |
||
312 | * Called first as part of the container attachment procedure. |
||
313 | * Responsible for checking it's OK to attach the container |
||
314 | * and if it is, calling the container-specific attach() method |
||
315 | * |
||
316 | * @see attach() |
||
317 | * @see is_valid_attach() |
||
318 | **/ |
||
319 | public function _attach() { |
||
334 | |||
335 | /** |
||
336 | * Returns all the Backbone templates |
||
337 | * |
||
338 | * @return array |
||
339 | **/ |
||
340 | public function get_templates() { |
||
343 | |||
344 | /** |
||
345 | * Adds a new Backbone template |
||
346 | **/ |
||
347 | protected function add_template( $name, $callback ) { |
||
350 | |||
351 | /** |
||
352 | * Attach the container rendering and helping methods |
||
353 | * to concrete WordPress Action hooks |
||
354 | **/ |
||
355 | public function attach() {} |
||
356 | |||
357 | /** |
||
358 | * Perform checks whether the container should be attached during the current request |
||
359 | * |
||
360 | * @return bool True if the container is allowed to be attached |
||
361 | **/ |
||
362 | final public function is_valid_attach() { |
||
366 | |||
367 | /** |
||
368 | * Require extending containers to define their own attach rules |
||
369 | * |
||
370 | * @return bool True if the container is allowed to be attached |
||
371 | **/ |
||
372 | abstract protected function _is_valid_attach(); |
||
373 | |||
374 | /** |
||
375 | * Whether this container is currently viewed. |
||
376 | **/ |
||
377 | public function is_active() { |
||
380 | |||
381 | /** |
||
382 | * Perform a check whether the current container has fields |
||
383 | * |
||
384 | * @return bool |
||
385 | **/ |
||
386 | public function has_fields() { |
||
389 | |||
390 | /** |
||
391 | * Returns the private container array of fields. |
||
392 | * Use only if you are completely aware of what you are doing. |
||
393 | * |
||
394 | * @return array |
||
395 | **/ |
||
396 | public function get_fields() { |
||
399 | |||
400 | /** |
||
401 | * Return root field from container with specified name |
||
402 | * |
||
403 | * @example crb_complex |
||
404 | * |
||
405 | * @param string $field_name |
||
406 | * @return Field |
||
407 | **/ |
||
408 | public function get_root_field_by_name( $field_name ) { |
||
417 | |||
418 | /** |
||
419 | * Return field from container with specified name |
||
420 | * |
||
421 | * @example crb_complex/text_field |
||
422 | * @example crb_complex/complex_2 |
||
423 | * @example crb_complex/complex_2:text_group/text_field |
||
424 | * |
||
425 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
426 | * @return Field |
||
427 | **/ |
||
428 | public function get_field_by_name( $field_name ) { |
||
463 | |||
464 | /** |
||
465 | * Perform checks whether there is a field registered with the name $name. |
||
466 | * If not, the field name is recorded. |
||
467 | * |
||
468 | * @param string $name |
||
469 | **/ |
||
470 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
477 | |||
478 | /** |
||
479 | * Remove field name $name from the list of unique field names |
||
480 | * |
||
481 | * @param string $name |
||
482 | **/ |
||
483 | public function drop_unique_field_name( $name ) { |
||
490 | |||
491 | /** |
||
492 | * Return whether the datastore instance is the default one or has been overriden |
||
493 | * |
||
494 | * @return boolean |
||
495 | **/ |
||
496 | 6 | public function has_default_datastore() { |
|
499 | |||
500 | /** |
||
501 | * Set datastore instance |
||
502 | * |
||
503 | * @param Datastore_Interface $datastore |
||
504 | * @return object $this |
||
505 | **/ |
||
506 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
518 | |||
519 | /** |
||
520 | * Get the DataStore instance |
||
521 | * |
||
522 | * @return Datastore_Interface $datastore |
||
523 | **/ |
||
524 | 6 | public function get_datastore() { |
|
527 | |||
528 | /** |
||
529 | * Return WordPress nonce name used to identify the current container instance |
||
530 | * |
||
531 | * @return string |
||
532 | **/ |
||
533 | public function get_nonce_name() { |
||
536 | |||
537 | /** |
||
538 | * Return WordPress nonce field |
||
539 | * |
||
540 | * @return string |
||
541 | **/ |
||
542 | public function get_nonce_field() { |
||
545 | |||
546 | /** |
||
547 | * Check if the nonce is present in the request and that it is verified |
||
548 | * |
||
549 | * @return bool |
||
550 | **/ |
||
551 | protected function verified_nonce_in_request() { |
||
556 | |||
557 | /** |
||
558 | * Internal function that creates the tab and associates it with particular field set |
||
559 | * |
||
560 | * @param string $tab_name |
||
561 | * @param array $fields |
||
562 | * @param int $queue_end |
||
563 | * @return object $this |
||
564 | */ |
||
565 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
586 | |||
587 | /** |
||
588 | * Whether the container is tabbed or not |
||
589 | * |
||
590 | * @return bool |
||
591 | */ |
||
592 | public function is_tabbed() { |
||
595 | |||
596 | /** |
||
597 | * Retrieve all fields that are not defined under a specific tab |
||
598 | * |
||
599 | * @return array |
||
600 | */ |
||
601 | protected function get_untabbed_fields() { |
||
623 | |||
624 | /** |
||
625 | * Retrieve all tabs. |
||
626 | * Create a default tab if there are any untabbed fields. |
||
627 | * |
||
628 | * @return array |
||
629 | */ |
||
630 | protected function get_tabs() { |
||
639 | |||
640 | /** |
||
641 | * Build the tabs JSON |
||
642 | * |
||
643 | * @return array |
||
644 | */ |
||
645 | protected function get_tabs_json() { |
||
657 | |||
658 | /** |
||
659 | * Underscore template for tabs |
||
660 | */ |
||
661 | public function template_tabs() { |
||
680 | |||
681 | /** |
||
682 | * Returns an array that holds the container data, suitable for JSON representation. |
||
683 | * This data will be available in the Underscore template and the Backbone Model. |
||
684 | * |
||
685 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
686 | * @return array |
||
687 | */ |
||
688 | public function to_json( $load ) { |
||
705 | |||
706 | /** |
||
707 | * Enqueue admin scripts |
||
708 | */ |
||
709 | public static function admin_hook_scripts() { |
||
719 | |||
720 | /** |
||
721 | * Enqueue admin styles |
||
722 | */ |
||
723 | public static function admin_hook_styles() { |
||
726 | |||
727 | /** |
||
728 | * COMMON USAGE METHODS |
||
729 | */ |
||
730 | |||
731 | /** |
||
732 | * Append array of fields to the current fields set. All items of the array |
||
733 | * must be instances of Field and their names should be unique for all |
||
734 | * Carbon containers. |
||
735 | * If a field does not have DataStore already, the container datastore is |
||
736 | * assigned to them instead. |
||
737 | * |
||
738 | * @param array $fields |
||
739 | * @return object $this |
||
740 | **/ |
||
741 | public function add_fields( $fields ) { |
||
759 | |||
760 | /** |
||
761 | * Configuration function for adding tab with fields |
||
762 | * |
||
763 | * @param string $tab_name |
||
764 | * @param array $fields |
||
765 | * @return object $this |
||
766 | */ |
||
767 | public function add_tab( $tab_name, $fields ) { |
||
775 | } |
||
776 |