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 | * Separator signifying field hierarchy relation |
||
25 | * Used when searching for fields in a specific complex field |
||
26 | */ |
||
27 | const HIERARCHY_FIELD_SEPARATOR = '/'; |
||
28 | |||
29 | /** |
||
30 | * Separator signifying complex_field->group relation |
||
31 | * Used when searching for fields in a specific complex field group |
||
32 | */ |
||
33 | const HIERARCHY_GROUP_SEPARATOR = ':'; |
||
34 | |||
35 | /** |
||
36 | * Stores if the container is active on the current page |
||
37 | * |
||
38 | * @see activate() |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $active = false; |
||
42 | |||
43 | /** |
||
44 | * List of registered unique field names for this container instance |
||
45 | * |
||
46 | * @see verify_unique_field_name() |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $registered_field_names = array(); |
||
50 | |||
51 | /** |
||
52 | * Stores all the container Backbone templates |
||
53 | * |
||
54 | * @see factory() |
||
55 | * @see add_template() |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $templates = array(); |
||
59 | |||
60 | /** |
||
61 | * Tabs available |
||
62 | */ |
||
63 | protected $tabs = array(); |
||
64 | |||
65 | /** |
||
66 | * List of default container settings |
||
67 | * |
||
68 | * @see init() |
||
69 | * @var array |
||
70 | */ |
||
71 | public $settings = array(); |
||
72 | |||
73 | /** |
||
74 | * Title of the container |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | public $title = ''; |
||
79 | |||
80 | /** |
||
81 | * List of notification messages to be displayed on the front-end |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | protected $notifications = array(); |
||
86 | |||
87 | /** |
||
88 | * List of error messages to be displayed on the front-end |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $errors = array(); |
||
93 | |||
94 | /** |
||
95 | * List of container fields |
||
96 | * |
||
97 | * @see add_fields() |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $fields = array(); |
||
101 | |||
102 | /** |
||
103 | * Container datastores. Propagated to all container fields |
||
104 | * |
||
105 | * @see set_datastore() |
||
106 | * @see get_datastore() |
||
107 | * @var object |
||
108 | */ |
||
109 | protected $datastore; |
||
110 | |||
111 | /** |
||
112 | * Flag whether the datastore is the default one or replaced with a custom one |
||
113 | * |
||
114 | * @see set_datastore() |
||
115 | * @see get_datastore() |
||
116 | * @var boolean |
||
117 | */ |
||
118 | protected $has_default_datastore = true; |
||
119 | |||
120 | /** |
||
121 | * Normalizes a container type string to an expected format |
||
122 | * |
||
123 | * @param string $type |
||
124 | * @return string $normalized_type |
||
125 | **/ |
||
126 | protected static function normalize_container_type( $type ) { |
||
127 | // backward compatibility: post_meta container used to be called custom_fields |
||
128 | if ( $type === 'custom_fields' ) { |
||
129 | $type = 'post_meta'; |
||
130 | } |
||
131 | |||
132 | $normalized_type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) ); |
||
133 | return $normalized_type; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Resolves a string-based type to a fully qualified container class name |
||
138 | * |
||
139 | * @param string $type |
||
140 | * @return string $class_name |
||
141 | **/ |
||
142 | protected static function container_type_to_class( $type ) { |
||
143 | $class = __NAMESPACE__ . '\\' . $type . '_Container'; |
||
144 | View Code Duplication | if ( ! class_exists( $class ) ) { |
|
145 | Incorrect_Syntax_Exception::raise( 'Unknown container "' . $type . '".' ); |
||
146 | $class = __NAMESPACE__ . '\\Broken_Container'; |
||
147 | } |
||
148 | return $class; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Create a new container of type $type and name $name. |
||
153 | * |
||
154 | * @param string $type |
||
155 | * @param string $name Human-readable name of the container |
||
156 | * @return object $container |
||
157 | **/ |
||
158 | 9 | public static function factory( $type, $name ) { |
|
159 | 9 | $repository = App::resolve( 'container_repository' ); |
|
160 | 9 | $unique_id = $repository->get_unique_panel_id( $name ); |
|
161 | |||
162 | 9 | $normalized_type = static::normalize_container_type( $type ); |
|
163 | 9 | $class = static::container_type_to_class( $normalized_type ); |
|
164 | 7 | $container = new $class( $unique_id, $name, $normalized_type ); |
|
165 | 7 | $repository->register_container( $container ); |
|
166 | |||
167 | 7 | return $container; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * An alias of factory(). |
||
172 | * |
||
173 | * @see Container::factory() |
||
174 | **/ |
||
175 | public static function make( $type, $name ) { |
||
176 | return static::factory( $type, $name ); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Create a new container |
||
181 | * |
||
182 | * @param string $unique_id Unique id of the container |
||
183 | * @param string $title title of the container |
||
184 | * @param string $type Type of the container |
||
185 | **/ |
||
186 | 2 | public function __construct( $unique_id, $title, $type ) { |
|
187 | 2 | App::verify_boot(); |
|
188 | |||
189 | 2 | if ( empty( $title ) ) { |
|
190 | 1 | Incorrect_Syntax_Exception::raise( 'Empty container title is not supported' ); |
|
191 | } |
||
192 | |||
193 | 1 | $this->id = $unique_id; |
|
194 | 1 | $this->title = $title; |
|
195 | 1 | $this->type = $type; |
|
196 | 1 | } |
|
197 | |||
198 | /** |
||
199 | * Return whether the container is active |
||
200 | **/ |
||
201 | public function active() { |
||
202 | return $this->active; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Activate the container and trigger an action |
||
207 | **/ |
||
208 | protected function activate() { |
||
209 | $this->active = true; |
||
210 | $this->boot(); |
||
211 | do_action( 'crb_container_activated', $this ); |
||
212 | |||
213 | $fields = $this->get_fields(); |
||
214 | foreach ( $fields as $field ) { |
||
215 | $field->activate(); |
||
216 | } |
||
217 | } |
||
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() { |
||
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() { |
||
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 = null ) { |
||
301 | |||
302 | /** |
||
303 | * Checks whether the current request is valid |
||
304 | * |
||
305 | * @return bool |
||
306 | **/ |
||
307 | public function is_valid_save() { |
||
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() { |
||
330 | |||
331 | /** |
||
332 | * Attach the container rendering and helping methods |
||
333 | * to concrete WordPress Action hooks |
||
334 | **/ |
||
335 | public function attach() {} |
||
336 | |||
337 | /** |
||
338 | * Perform checks whether the container should be attached during the current request |
||
339 | * |
||
340 | * @return bool True if the container is allowed to be attached |
||
341 | **/ |
||
342 | final public function is_valid_attach() { |
||
346 | |||
347 | /** |
||
348 | * Check container attachment rules against current page request (in admin) |
||
349 | * |
||
350 | * @return bool |
||
351 | **/ |
||
352 | abstract protected function is_valid_attach_for_request(); |
||
353 | |||
354 | /** |
||
355 | * Check container attachment rules against object id |
||
356 | * |
||
357 | * @param int $object_id |
||
358 | * @return bool |
||
359 | **/ |
||
360 | abstract public function is_valid_attach_for_object( $object_id = null ); |
||
361 | |||
362 | /** |
||
363 | * Whether this container is currently viewed. |
||
364 | **/ |
||
365 | public function should_activate() { |
||
368 | |||
369 | /** |
||
370 | * Returns all the Backbone templates |
||
371 | * |
||
372 | * @return array |
||
373 | **/ |
||
374 | public function get_templates() { |
||
377 | |||
378 | /** |
||
379 | * Adds a new Backbone template |
||
380 | **/ |
||
381 | protected function add_template( $name, $callback ) { |
||
384 | |||
385 | /** |
||
386 | * Perform a check whether the current container has fields |
||
387 | * |
||
388 | * @return bool |
||
389 | **/ |
||
390 | public function has_fields() { |
||
393 | |||
394 | /** |
||
395 | * Returns the private container array of fields. |
||
396 | * Use only if you are completely aware of what you are doing. |
||
397 | * |
||
398 | * @return array |
||
399 | **/ |
||
400 | public function get_fields() { |
||
403 | |||
404 | /** |
||
405 | * Return root field from container with specified name |
||
406 | * |
||
407 | * @example crb_complex |
||
408 | * |
||
409 | * @param string $field_name |
||
410 | * @return Field |
||
411 | **/ |
||
412 | public function get_root_field_by_name( $field_name ) { |
||
421 | |||
422 | /** |
||
423 | * Return field from container with specified name |
||
424 | * |
||
425 | * @example crb_complex/text_field |
||
426 | * @example crb_complex/complex_2 |
||
427 | * @example crb_complex/complex_2:text_group/text_field |
||
428 | * |
||
429 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
430 | * @return Field |
||
431 | **/ |
||
432 | public function get_field_by_name( $field_name ) { |
||
467 | |||
468 | /** |
||
469 | * Perform checks whether there is a field registered with the name $name. |
||
470 | * If not, the field name is recorded. |
||
471 | * |
||
472 | * @param string $name |
||
473 | **/ |
||
474 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
481 | |||
482 | /** |
||
483 | * Remove field name $name from the list of unique field names |
||
484 | * |
||
485 | * @param string $name |
||
486 | **/ |
||
487 | public function drop_unique_field_name( $name ) { |
||
494 | |||
495 | /** |
||
496 | * Return whether the datastore instance is the default one or has been overriden |
||
497 | * |
||
498 | * @return boolean |
||
499 | **/ |
||
500 | 6 | public function has_default_datastore() { |
|
503 | |||
504 | /** |
||
505 | * Set datastore instance |
||
506 | * |
||
507 | * @param Datastore_Interface $datastore |
||
508 | * @return object $this |
||
509 | **/ |
||
510 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
522 | |||
523 | /** |
||
524 | * Get the DataStore instance |
||
525 | * |
||
526 | * @return Datastore_Interface $datastore |
||
527 | **/ |
||
528 | 6 | public function get_datastore() { |
|
531 | |||
532 | /** |
||
533 | * Return WordPress nonce name used to identify the current container instance |
||
534 | * |
||
535 | * @return string |
||
536 | **/ |
||
537 | public function get_nonce_name() { |
||
540 | |||
541 | /** |
||
542 | * Return WordPress nonce field |
||
543 | * |
||
544 | * @return string |
||
545 | **/ |
||
546 | public function get_nonce_field() { |
||
549 | |||
550 | /** |
||
551 | * Check if the nonce is present in the request and that it is verified |
||
552 | * |
||
553 | * @return bool |
||
554 | **/ |
||
555 | protected function verified_nonce_in_request() { |
||
560 | |||
561 | /** |
||
562 | * Internal function that creates the tab and associates it with particular field set |
||
563 | * |
||
564 | * @param string $tab_name |
||
565 | * @param array $fields |
||
566 | * @param int $queue_end |
||
567 | * @return object $this |
||
568 | */ |
||
569 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
590 | |||
591 | /** |
||
592 | * Whether the container is tabbed or not |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | public function is_tabbed() { |
||
599 | |||
600 | /** |
||
601 | * Retrieve all fields that are not defined under a specific tab |
||
602 | * |
||
603 | * @return array |
||
604 | */ |
||
605 | protected function get_untabbed_fields() { |
||
627 | |||
628 | /** |
||
629 | * Retrieve all tabs. |
||
630 | * Create a default tab if there are any untabbed fields. |
||
631 | * |
||
632 | * @return array |
||
633 | */ |
||
634 | protected function get_tabs() { |
||
643 | |||
644 | /** |
||
645 | * Build the tabs JSON |
||
646 | * |
||
647 | * @return array |
||
648 | */ |
||
649 | protected function get_tabs_json() { |
||
661 | |||
662 | /** |
||
663 | * Underscore template for tabs |
||
664 | */ |
||
665 | public function template_tabs() { |
||
684 | |||
685 | /** |
||
686 | * Returns an array that holds the container data, suitable for JSON representation. |
||
687 | * This data will be available in the Underscore template and the Backbone Model. |
||
688 | * |
||
689 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
690 | * @return array |
||
691 | */ |
||
692 | public function to_json( $load ) { |
||
709 | |||
710 | /** |
||
711 | * Enqueue admin scripts |
||
712 | */ |
||
713 | public static function admin_hook_scripts() { |
||
723 | |||
724 | /** |
||
725 | * Enqueue admin styles |
||
726 | */ |
||
727 | public static function admin_hook_styles() { |
||
730 | |||
731 | /** |
||
732 | * COMMON USAGE METHODS |
||
733 | */ |
||
734 | |||
735 | /** |
||
736 | * Append array of fields to the current fields set. All items of the array |
||
737 | * must be instances of Field and their names should be unique for all |
||
738 | * Carbon containers. |
||
739 | * If a field does not have DataStore already, the container datastore is |
||
740 | * assigned to them instead. |
||
741 | * |
||
742 | * @param array $fields |
||
743 | * @return object $this |
||
744 | **/ |
||
745 | public function add_fields( $fields ) { |
||
763 | |||
764 | /** |
||
765 | * Configuration function for adding tab with fields |
||
766 | * |
||
767 | * @param string $tab_name |
||
768 | * @param array $fields |
||
769 | * @return object $this |
||
770 | */ |
||
771 | public function add_tab( $tab_name, $fields ) { |
||
779 | } |
||
780 |