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 save request is valid |
||
304 | * |
||
305 | * @return bool |
||
306 | **/ |
||
307 | final protected function _is_valid_save() { |
||
308 | $param = func_get_args(); |
||
309 | $is_valid_save = call_user_func_array( array( $this, 'is_valid_save' ), $param ); |
||
310 | return apply_filters( 'carbon_fields_container_is_valid_save', $is_valid_save, $this ); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Checks whether the current save request is valid |
||
315 | * |
||
316 | * @return bool |
||
317 | **/ |
||
318 | abstract protected function is_valid_save(); |
||
319 | |||
320 | /** |
||
321 | * Called first as part of the container attachment procedure. |
||
322 | * Responsible for checking it's OK to attach the container |
||
323 | * and if it is, calling the container-specific attach() method |
||
324 | * |
||
325 | * @see attach() |
||
326 | * @see is_valid_attach() |
||
327 | **/ |
||
328 | public function _attach() { |
||
329 | $param = func_get_args(); |
||
330 | if ( $this->is_valid_attach() ) { |
||
331 | call_user_func_array( array( $this, 'attach' ), $param ); |
||
332 | |||
333 | // Allow containers to activate but not load (useful in cases such as theme options) |
||
334 | if ( $this->should_activate() ) { |
||
335 | $this->activate(); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Attach the container rendering and helping methods |
||
342 | * to concrete WordPress Action hooks |
||
343 | **/ |
||
344 | public function attach() {} |
||
345 | |||
346 | /** |
||
347 | * Perform checks whether the container should be attached during the current request |
||
348 | * |
||
349 | * @return bool True if the container is allowed to be attached |
||
350 | **/ |
||
351 | final public function is_valid_attach() { |
||
352 | $is_valid_attach = $this->is_valid_attach_for_request(); |
||
353 | return apply_filters( 'carbon_fields_container_is_valid_attach', $is_valid_attach, $this ); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Check container attachment rules against current page request (in admin) |
||
358 | * |
||
359 | * @return bool |
||
360 | **/ |
||
361 | abstract protected function is_valid_attach_for_request(); |
||
362 | |||
363 | /** |
||
364 | * Check container attachment rules against object id |
||
365 | * |
||
366 | * @param int $object_id |
||
367 | * @return bool |
||
368 | **/ |
||
369 | abstract public function is_valid_attach_for_object( $object_id = null ); |
||
370 | |||
371 | /** |
||
372 | * Whether this container is currently viewed. |
||
373 | **/ |
||
374 | public function should_activate() { |
||
377 | |||
378 | /** |
||
379 | * Returns all the Backbone templates |
||
380 | * |
||
381 | * @return array |
||
382 | **/ |
||
383 | public function get_templates() { |
||
386 | |||
387 | /** |
||
388 | * Adds a new Backbone template |
||
389 | **/ |
||
390 | protected function add_template( $name, $callback ) { |
||
393 | |||
394 | /** |
||
395 | * Perform a check whether the current container has fields |
||
396 | * |
||
397 | * @return bool |
||
398 | **/ |
||
399 | public function has_fields() { |
||
402 | |||
403 | /** |
||
404 | * Returns the private container array of fields. |
||
405 | * Use only if you are completely aware of what you are doing. |
||
406 | * |
||
407 | * @return array |
||
408 | **/ |
||
409 | public function get_fields() { |
||
412 | |||
413 | /** |
||
414 | * Return root field from container with specified name |
||
415 | * |
||
416 | * @example crb_complex |
||
417 | * |
||
418 | * @param string $field_name |
||
419 | * @return Field |
||
420 | **/ |
||
421 | public function get_root_field_by_name( $field_name ) { |
||
430 | |||
431 | /** |
||
432 | * Get a regex to match field name patterns used to fetch specific fields |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | protected function get_field_pattern_regex() { |
||
451 | |||
452 | /** |
||
453 | * Return field from container with specified name |
||
454 | * |
||
455 | * @example crb_complex/text_field |
||
456 | * @example crb_complex/complex_2 |
||
457 | * @example crb_complex/complex_2:text_group/text_field |
||
458 | * |
||
459 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
460 | * @return Field |
||
461 | **/ |
||
462 | public function get_field_by_name( $field_name ) { |
||
506 | |||
507 | /** |
||
508 | * Perform checks whether there is a field registered with the name $name. |
||
509 | * If not, the field name is recorded. |
||
510 | * |
||
511 | * @param string $name |
||
512 | **/ |
||
513 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
520 | |||
521 | /** |
||
522 | * Remove field name $name from the list of unique field names |
||
523 | * |
||
524 | * @param string $name |
||
525 | **/ |
||
526 | public function drop_unique_field_name( $name ) { |
||
533 | |||
534 | /** |
||
535 | * Return whether the datastore instance is the default one or has been overriden |
||
536 | * |
||
537 | * @return boolean |
||
538 | **/ |
||
539 | public function has_default_datastore() { |
||
542 | 1 | ||
543 | /** |
||
544 | 6 | * Set datastore instance |
|
545 | 6 | * |
|
546 | * @param Datastore_Interface $datastore |
||
547 | 6 | * @return object $this |
|
548 | **/ |
||
549 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
561 | |||
562 | /** |
||
563 | * Get the DataStore instance |
||
564 | * |
||
565 | * @return Datastore_Interface $datastore |
||
566 | **/ |
||
567 | public function get_datastore() { |
||
570 | |||
571 | /** |
||
572 | * Return WordPress nonce name used to identify the current container instance |
||
573 | * |
||
574 | * @return string |
||
575 | **/ |
||
576 | public function get_nonce_name() { |
||
579 | |||
580 | /** |
||
581 | * Return WordPress nonce field |
||
582 | * |
||
583 | * @return string |
||
584 | **/ |
||
585 | public function get_nonce_field() { |
||
588 | |||
589 | /** |
||
590 | * Check if the nonce is present in the request and that it is verified |
||
591 | * |
||
592 | * @return bool |
||
593 | **/ |
||
594 | protected function verified_nonce_in_request() { |
||
599 | |||
600 | /** |
||
601 | * Internal function that creates the tab and associates it with particular field set |
||
602 | * |
||
603 | * @param string $tab_name |
||
604 | * @param array $fields |
||
605 | * @param int $queue_end |
||
606 | * @return object $this |
||
607 | */ |
||
608 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
629 | |||
630 | /** |
||
631 | * Whether the container is tabbed or not |
||
632 | * |
||
633 | * @return bool |
||
634 | */ |
||
635 | public function is_tabbed() { |
||
638 | |||
639 | /** |
||
640 | * Retrieve all fields that are not defined under a specific tab |
||
641 | * |
||
642 | * @return array |
||
643 | */ |
||
644 | protected function get_untabbed_fields() { |
||
666 | |||
667 | /** |
||
668 | * Retrieve all tabs. |
||
669 | * Create a default tab if there are any untabbed fields. |
||
670 | * |
||
671 | * @return array |
||
672 | */ |
||
673 | protected function get_tabs() { |
||
682 | |||
683 | /** |
||
684 | * Build the tabs JSON |
||
685 | * |
||
686 | * @return array |
||
687 | */ |
||
688 | protected function get_tabs_json() { |
||
700 | |||
701 | /** |
||
702 | * Underscore template for tabs |
||
703 | */ |
||
704 | public function template_tabs() { |
||
723 | |||
724 | /** |
||
725 | * Returns an array that holds the container data, suitable for JSON representation. |
||
726 | * This data will be available in the Underscore template and the Backbone Model. |
||
727 | * |
||
728 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
729 | * @return array |
||
730 | */ |
||
731 | public function to_json( $load ) { |
||
748 | |||
749 | /** |
||
750 | * Enqueue admin scripts |
||
751 | */ |
||
752 | public static function admin_hook_scripts() { |
||
762 | |||
763 | /** |
||
764 | * Enqueue admin styles |
||
765 | */ |
||
766 | public static function admin_hook_styles() { |
||
769 | |||
770 | /** |
||
771 | * COMMON USAGE METHODS |
||
772 | */ |
||
773 | |||
774 | /** |
||
775 | * Append array of fields to the current fields set. All items of the array |
||
776 | * must be instances of Field and their names should be unique for all |
||
777 | * Carbon containers. |
||
778 | * If a field does not have DataStore already, the container datastore is |
||
779 | * assigned to them instead. |
||
780 | * |
||
781 | * @param array $fields |
||
782 | * @return object $this |
||
783 | **/ |
||
784 | public function add_fields( $fields ) { |
||
802 | |||
803 | /** |
||
804 | * Configuration function for adding tab with fields |
||
805 | * |
||
806 | * @param string $tab_name |
||
807 | * @param array $fields |
||
808 | * @return object $this |
||
809 | */ |
||
810 | public function add_tab( $tab_name, $fields ) { |
||
818 | } |
||
819 |