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 | * Load the value for each field in the container. |
||
267 | * Could be used internally during container rendering |
||
268 | **/ |
||
269 | public function load() { |
||
270 | foreach ( $this->fields as $field ) { |
||
271 | $field->load(); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Called first as part of the container save procedure. |
||
277 | * Responsible for checking the request validity and |
||
278 | * calling the container-specific save() method |
||
279 | * |
||
280 | * @see save() |
||
281 | * @see is_valid_save() |
||
282 | **/ |
||
283 | public function _save() { |
||
284 | $param = func_get_args(); |
||
285 | if ( call_user_func_array( array( $this, 'is_valid_save' ), $param ) ) { |
||
286 | call_user_func_array( array( $this, 'save' ), $param ); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Load submitted data and save each field in the container |
||
292 | * |
||
293 | * @see is_valid_save() |
||
294 | **/ |
||
295 | public function save( $data ) { |
||
296 | foreach ( $this->fields as $field ) { |
||
297 | $field->set_value_from_input(); |
||
298 | $field->save(); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Checks whether the current request is valid |
||
304 | * |
||
305 | * @return bool |
||
306 | **/ |
||
307 | public function is_valid_save() { |
||
308 | return false; |
||
309 | } |
||
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 | * Attach the container rendering and helping methods |
||
337 | * to concrete WordPress Action hooks |
||
338 | **/ |
||
339 | public function attach() {} |
||
340 | |||
341 | /** |
||
342 | * Perform checks whether the container should be attached during the current request |
||
343 | * |
||
344 | * @return bool True if the container is allowed to be attached |
||
345 | **/ |
||
346 | final public function is_valid_attach() { |
||
350 | |||
351 | /** |
||
352 | * Check container attachment rules against current page request (in admin) |
||
353 | * |
||
354 | * @return bool |
||
355 | **/ |
||
356 | abstract protected function is_valid_attach_for_request(); |
||
357 | |||
358 | /** |
||
359 | * Check container attachment rules against object id |
||
360 | * |
||
361 | * @return bool |
||
362 | **/ |
||
363 | abstract public function is_valid_attach_for_object( $object_id = null ); |
||
364 | |||
365 | /** |
||
366 | * Whether this container is currently viewed. |
||
367 | **/ |
||
368 | public function is_active() { |
||
369 | return $this->is_valid_attach(); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Returns all the Backbone templates |
||
374 | * |
||
375 | * @return array |
||
376 | **/ |
||
377 | public function get_templates() { |
||
380 | |||
381 | /** |
||
382 | * Adds a new Backbone template |
||
383 | **/ |
||
384 | protected function add_template( $name, $callback ) { |
||
385 | $this->templates[ $name ] = $callback; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Perform a check whether the current container has fields |
||
390 | * |
||
391 | * @return bool |
||
392 | **/ |
||
393 | public function has_fields() { |
||
396 | |||
397 | /** |
||
398 | * Returns the private container array of fields. |
||
399 | * Use only if you are completely aware of what you are doing. |
||
400 | * |
||
401 | * @return array |
||
402 | **/ |
||
403 | public function get_fields() { |
||
406 | |||
407 | /** |
||
408 | * Return root field from container with specified name |
||
409 | * |
||
410 | * @example crb_complex |
||
411 | * |
||
412 | * @param string $field_name |
||
413 | * @return Field |
||
414 | **/ |
||
415 | public function get_root_field_by_name( $field_name ) { |
||
424 | |||
425 | /** |
||
426 | * Return field from container with specified name |
||
427 | * |
||
428 | * @example crb_complex/text_field |
||
429 | * @example crb_complex/complex_2 |
||
430 | * @example crb_complex/complex_2:text_group/text_field |
||
431 | * |
||
432 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
433 | * @return Field |
||
434 | **/ |
||
435 | public function get_field_by_name( $field_name ) { |
||
470 | |||
471 | /** |
||
472 | * Perform checks whether there is a field registered with the name $name. |
||
473 | * If not, the field name is recorded. |
||
474 | * |
||
475 | * @param string $name |
||
476 | **/ |
||
477 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
484 | |||
485 | /** |
||
486 | * Remove field name $name from the list of unique field names |
||
487 | * |
||
488 | * @param string $name |
||
489 | **/ |
||
490 | public function drop_unique_field_name( $name ) { |
||
497 | |||
498 | /** |
||
499 | * Return whether the datastore instance is the default one or has been overriden |
||
500 | * |
||
501 | * @return boolean |
||
502 | **/ |
||
503 | 6 | public function has_default_datastore() { |
|
506 | |||
507 | /** |
||
508 | * Set datastore instance |
||
509 | * |
||
510 | * @param Datastore_Interface $datastore |
||
511 | * @return object $this |
||
512 | **/ |
||
513 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
525 | |||
526 | /** |
||
527 | * Get the DataStore instance |
||
528 | * |
||
529 | * @return Datastore_Interface $datastore |
||
530 | **/ |
||
531 | 6 | public function get_datastore() { |
|
534 | |||
535 | /** |
||
536 | * Return WordPress nonce name used to identify the current container instance |
||
537 | * |
||
538 | * @return string |
||
539 | **/ |
||
540 | public function get_nonce_name() { |
||
543 | |||
544 | /** |
||
545 | * Return WordPress nonce field |
||
546 | * |
||
547 | * @return string |
||
548 | **/ |
||
549 | public function get_nonce_field() { |
||
552 | |||
553 | /** |
||
554 | * Check if the nonce is present in the request and that it is verified |
||
555 | * |
||
556 | * @return bool |
||
557 | **/ |
||
558 | protected function verified_nonce_in_request() { |
||
563 | |||
564 | /** |
||
565 | * Internal function that creates the tab and associates it with particular field set |
||
566 | * |
||
567 | * @param string $tab_name |
||
568 | * @param array $fields |
||
569 | * @param int $queue_end |
||
570 | * @return object $this |
||
571 | */ |
||
572 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
593 | |||
594 | /** |
||
595 | * Whether the container is tabbed or not |
||
596 | * |
||
597 | * @return bool |
||
598 | */ |
||
599 | public function is_tabbed() { |
||
602 | |||
603 | /** |
||
604 | * Retrieve all fields that are not defined under a specific tab |
||
605 | * |
||
606 | * @return array |
||
607 | */ |
||
608 | protected function get_untabbed_fields() { |
||
630 | |||
631 | /** |
||
632 | * Retrieve all tabs. |
||
633 | * Create a default tab if there are any untabbed fields. |
||
634 | * |
||
635 | * @return array |
||
636 | */ |
||
637 | protected function get_tabs() { |
||
646 | |||
647 | /** |
||
648 | * Build the tabs JSON |
||
649 | * |
||
650 | * @return array |
||
651 | */ |
||
652 | protected function get_tabs_json() { |
||
664 | |||
665 | /** |
||
666 | * Underscore template for tabs |
||
667 | */ |
||
668 | public function template_tabs() { |
||
687 | |||
688 | /** |
||
689 | * Returns an array that holds the container data, suitable for JSON representation. |
||
690 | * This data will be available in the Underscore template and the Backbone Model. |
||
691 | * |
||
692 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
693 | * @return array |
||
694 | */ |
||
695 | public function to_json( $load ) { |
||
712 | |||
713 | /** |
||
714 | * Enqueue admin scripts |
||
715 | */ |
||
716 | public static function admin_hook_scripts() { |
||
726 | |||
727 | /** |
||
728 | * Enqueue admin styles |
||
729 | */ |
||
730 | public static function admin_hook_styles() { |
||
733 | |||
734 | /** |
||
735 | * COMMON USAGE METHODS |
||
736 | */ |
||
737 | |||
738 | /** |
||
739 | * Append array of fields to the current fields set. All items of the array |
||
740 | * must be instances of Field and their names should be unique for all |
||
741 | * Carbon containers. |
||
742 | * If a field does not have DataStore already, the container datastore is |
||
743 | * assigned to them instead. |
||
744 | * |
||
745 | * @param array $fields |
||
746 | * @return object $this |
||
747 | **/ |
||
748 | public function add_fields( $fields ) { |
||
766 | |||
767 | /** |
||
768 | * Configuration function for adding tab with fields |
||
769 | * |
||
770 | * @param string $tab_name |
||
771 | * @param array $fields |
||
772 | * @return object $this |
||
773 | */ |
||
774 | public function add_tab( $tab_name, $fields ) { |
||
782 | } |
||
783 |