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 CMB2_Field 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 CMB2_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2_Field extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'args'; |
||
25 | |||
26 | /** |
||
27 | * Field arguments |
||
28 | * @var mixed |
||
29 | * @since 1.1.0 |
||
30 | */ |
||
31 | public $args = array(); |
||
32 | |||
33 | /** |
||
34 | * Field group object or false (if no group) |
||
35 | * @var mixed |
||
36 | * @since 1.1.0 |
||
37 | */ |
||
38 | public $group = false; |
||
39 | |||
40 | /** |
||
41 | * Field meta value |
||
42 | * @var mixed |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | public $value = null; |
||
46 | |||
47 | /** |
||
48 | * Field meta value |
||
49 | * @var mixed |
||
50 | * @since 1.1.0 |
||
51 | */ |
||
52 | public $escaped_value = null; |
||
53 | |||
54 | /** |
||
55 | * Grouped Field's current numeric index during the save process |
||
56 | * @var mixed |
||
57 | * @since 2.0.0 |
||
58 | */ |
||
59 | public $index = 0; |
||
60 | |||
61 | /** |
||
62 | * Array of field options |
||
63 | * @var array |
||
64 | * @since 2.0.0 |
||
65 | */ |
||
66 | protected $field_options = array(); |
||
67 | |||
68 | /** |
||
69 | * Array of provided field text strings |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $strings; |
||
74 | |||
75 | /** |
||
76 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
77 | * @var string |
||
78 | * @since 2.2.2 |
||
79 | */ |
||
80 | public $render_context = 'edit'; |
||
81 | |||
82 | /** |
||
83 | * Constructs our field object |
||
84 | * @since 1.1.0 |
||
85 | * @param array $args Field arguments |
||
86 | */ |
||
87 | 109 | public function __construct( $args ) { |
|
109 | |||
110 | /** |
||
111 | * Non-existent methods fallback to checking for field arguments of the same name |
||
112 | * @since 1.1.0 |
||
113 | * @param string $name Method name |
||
114 | * @param array $arguments Array of passed-in arguments |
||
115 | * @return mixed Value of field argument |
||
116 | */ |
||
117 | 97 | public function __call( $name, $arguments ) { |
|
121 | |||
122 | /** |
||
123 | * Retrieves the field id |
||
124 | * @since 1.1.0 |
||
125 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
126 | * @return string Field id |
||
127 | */ |
||
128 | 107 | public function id( $raw = false ) { |
|
132 | |||
133 | /** |
||
134 | * Get a field argument |
||
135 | * @since 1.1.0 |
||
136 | * @param string $key Argument to check |
||
137 | * @param string $_key Sub argument to check |
||
138 | * @return mixed Argument value or false if non-existent |
||
139 | */ |
||
140 | 110 | public function args( $key = '', $_key = '' ) { |
|
154 | |||
155 | /** |
||
156 | * Retrieve a portion of a field property |
||
157 | * @since 1.1.0 |
||
158 | * @param string $var Field property to check |
||
159 | * @param string $key Field property array key to check |
||
160 | * @return mixed Queried property value or false |
||
161 | */ |
||
162 | 110 | public function _data( $var, $key = '' ) { |
|
169 | |||
170 | /** |
||
171 | * Get Field's value |
||
172 | * @since 1.1.0 |
||
173 | * @param string $key If value is an array, is used to get array key->value |
||
174 | * @return mixed Field value or false if non-existent |
||
175 | */ |
||
176 | 46 | public function value( $key = '' ) { |
|
179 | |||
180 | /** |
||
181 | * Retrieves metadata/option data |
||
182 | * @since 1.0.1 |
||
183 | * @param string $field_id Meta key/Option array key |
||
184 | * @param array $args Override arguments |
||
185 | * @return mixed Meta/Option value |
||
186 | */ |
||
187 | 105 | public function get_data( $field_id = '', $args = array() ) { |
|
250 | |||
251 | /** |
||
252 | * Updates metadata/option data |
||
253 | * @since 1.0.1 |
||
254 | * @param mixed $new_value Value to update data with |
||
255 | * @param bool $single Whether data is an array (add_metadata) |
||
256 | */ |
||
257 | 11 | public function update_data( $new_value, $single = true ) { |
|
321 | |||
322 | /** |
||
323 | * Removes/updates metadata/option data |
||
324 | * @since 1.0.1 |
||
325 | * @param string $old Old value |
||
326 | */ |
||
327 | 3 | public function remove_data( $old = '' ) { |
|
383 | |||
384 | /** |
||
385 | * Data variables for get/set data methods |
||
386 | * @since 1.1.0 |
||
387 | * @param array $args Override arguments |
||
388 | * @return array Updated arguments |
||
389 | */ |
||
390 | 105 | public function data_args( $args = array() ) { |
|
400 | |||
401 | /** |
||
402 | * Checks if field has a registered sanitization callback |
||
403 | * @since 1.0.1 |
||
404 | * @param mixed $meta_value Meta value |
||
405 | * @return mixed Possibly sanitized meta value |
||
406 | */ |
||
407 | 13 | public function sanitization_cb( $meta_value ) { |
|
450 | |||
451 | /** |
||
452 | * Process $_POST data to save this field's value |
||
453 | * @since 2.0.3 |
||
454 | * @param array $data_to_save $_POST data to check |
||
455 | * @return bool Result of save |
||
456 | */ |
||
457 | 2 | public function save_field_from_data( array $data_to_save ) { |
|
466 | |||
467 | /** |
||
468 | * Sanitize/store a value to this field |
||
469 | * @since 2.0.0 |
||
470 | * @param array $meta_value Desired value to sanitize/store |
||
471 | * @return bool Result of save |
||
472 | */ |
||
473 | 12 | public function save_field( $meta_value ) { |
|
474 | |||
475 | 12 | $updated = false; |
|
476 | 12 | $action = ''; |
|
477 | 12 | $new_value = $this->sanitization_cb( $meta_value ); |
|
478 | |||
479 | 12 | if ( ! $this->args( 'save_field' ) ) { |
|
480 | |||
481 | // Nothing to see here. |
||
482 | 1 | $action = 'disabled'; |
|
483 | |||
484 | 12 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
485 | |||
486 | 1 | $this->remove_data(); |
|
487 | 1 | $count = 0; |
|
488 | |||
489 | 1 | if ( ! empty( $new_value ) ) { |
|
490 | 1 | foreach ( $new_value as $add_new ) { |
|
491 | 1 | if ( $this->update_data( $add_new, false ) ) { |
|
492 | 1 | $count++; |
|
493 | 1 | } |
|
494 | 1 | } |
|
495 | 1 | } |
|
496 | |||
497 | 1 | $updated = $count ? $count : false; |
|
498 | 1 | $action = 'repeatable'; |
|
499 | |||
500 | 11 | } elseif ( ! cmb2_utils()->isempty( $new_value ) && $new_value !== $this->get_data() ) { |
|
501 | 9 | $updated = $this->update_data( $new_value ); |
|
502 | 9 | $action = 'updated'; |
|
503 | 10 | } elseif ( cmb2_utils()->isempty( $new_value ) ) { |
|
504 | 2 | $updated = $this->remove_data(); |
|
505 | 2 | $action = 'removed'; |
|
506 | 2 | } |
|
507 | |||
508 | 12 | if ( $updated ) { |
|
509 | 11 | $this->value = $this->get_data(); |
|
510 | 11 | } |
|
511 | |||
512 | 12 | $field_id = $this->id( true ); |
|
513 | |||
514 | /** |
||
515 | * Hooks after save field action. |
||
516 | * |
||
517 | * @since 2.2.0 |
||
518 | * |
||
519 | * @param string $field_id the current field id paramater. |
||
520 | * @param bool $updated Whether the metadata update action occurred. |
||
521 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
522 | * @param CMB2_Field object $field This field object |
||
523 | */ |
||
524 | 12 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
|
525 | |||
526 | /** |
||
527 | * Hooks after save field action. |
||
528 | * |
||
529 | * The dynamic portion of the hook, $field_id, refers to the |
||
530 | * current field id paramater. |
||
531 | * |
||
532 | * @since 2.2.0 |
||
533 | * |
||
534 | * @param bool $updated Whether the metadata update action occurred. |
||
535 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
536 | * @param CMB2_Field object $field This field object |
||
537 | */ |
||
538 | 11 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
|
539 | |||
540 | 10 | return $updated; |
|
541 | } |
||
542 | |||
543 | /** |
||
544 | * Determine if current type is exempt from escaping |
||
545 | * @since 1.1.0 |
||
546 | * @return bool True if exempt |
||
547 | */ |
||
548 | 46 | public function escaping_exception() { |
|
556 | |||
557 | /** |
||
558 | * Determine if current type cannot be repeatable |
||
559 | * @since 1.1.0 |
||
560 | * @param string $type Field type to check |
||
561 | * @return bool True if type cannot be repeatable |
||
562 | */ |
||
563 | 4 | public function repeatable_exception( $type ) { |
|
593 | |||
594 | /** |
||
595 | * Escape the value before output. Defaults to 'esc_attr()' |
||
596 | * @since 1.0.1 |
||
597 | * @param callable $func Escaping function (if not esc_attr()) |
||
598 | * @param mixed $meta_value Meta value |
||
599 | * @return mixed Final value |
||
600 | */ |
||
601 | 46 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
641 | |||
642 | /** |
||
643 | * Return non-empty value or field default if value IS empty |
||
644 | * @since 2.0.0 |
||
645 | * @param mixed $meta_value Field value |
||
646 | * @return mixed Field value, or default value |
||
647 | */ |
||
648 | 46 | public function val_or_default( $meta_value ) { |
|
651 | |||
652 | /** |
||
653 | * Offset a time value based on timezone |
||
654 | * @since 1.0.0 |
||
655 | * @return string Offset time string |
||
656 | */ |
||
657 | public function field_timezone_offset() { |
||
660 | |||
661 | /** |
||
662 | * Return timezone string |
||
663 | * @since 1.0.0 |
||
664 | * @return string Timezone string |
||
665 | */ |
||
666 | public function field_timezone() { |
||
680 | |||
681 | /** |
||
682 | * Format the timestamp field value based on the field date/time format arg |
||
683 | * @since 2.0.0 |
||
684 | * @param int $meta_value Timestamp |
||
685 | * @param string $format Either date_format or time_format |
||
686 | * @return string Formatted date |
||
687 | */ |
||
688 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
691 | |||
692 | /** |
||
693 | * Return a formatted timestamp for a field |
||
694 | * @since 2.0.0 |
||
695 | * @param string $format Either date_format or time_format |
||
696 | * @param string $meta_value Optional meta value to check |
||
697 | * @return string Formatted date |
||
698 | */ |
||
699 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
711 | |||
712 | /** |
||
713 | * Get timestamp from text date |
||
714 | * @since 2.2.0 |
||
715 | * @param string $value Date value |
||
716 | * @return mixed Unix timestamp representing the date. |
||
717 | */ |
||
718 | public function get_timestamp_from_value( $value ) { |
||
721 | |||
722 | /** |
||
723 | * Get field render callback and Render the field row |
||
724 | * @since 1.0.0 |
||
725 | */ |
||
726 | 10 | public function render_field() { |
|
734 | |||
735 | /** |
||
736 | * Default field render callback |
||
737 | * @since 2.1.1 |
||
738 | */ |
||
739 | 9 | public function render_field_callback() { |
|
783 | |||
784 | /** |
||
785 | * The default label_cb callback (if not a title field) |
||
786 | * |
||
787 | * @since 2.1.1 |
||
788 | * @return string Label html markup |
||
789 | */ |
||
790 | 9 | public function label() { |
|
799 | |||
800 | /** |
||
801 | * Defines the classes for the current CMB2 field row |
||
802 | * |
||
803 | * @since 2.0.0 |
||
804 | * @return string Space concatenated list of classes |
||
805 | */ |
||
806 | 45 | public function row_classes() { |
|
854 | |||
855 | |||
856 | |||
857 | /** |
||
858 | * Get field display callback and render the display value in the column. |
||
859 | * @since 2.2.2 |
||
860 | */ |
||
861 | 33 | public function render_column() { |
|
869 | |||
870 | /** |
||
871 | * Default callback to outputs field value in a display format. |
||
872 | * @since 2.2.2 |
||
873 | */ |
||
874 | 33 | public function display_value_callback() { |
|
917 | |||
918 | /** |
||
919 | * Replaces a hash key - {#} - with the repeatable index |
||
920 | * @since 1.2.0 |
||
921 | * @param string $value Value to update |
||
922 | * @return string Updated value |
||
923 | */ |
||
924 | 2 | public function replace_hash( $value ) { |
|
928 | |||
929 | /** |
||
930 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
931 | * For back-compatibility, falls back to checking the options array. |
||
932 | * |
||
933 | * @since 2.2.2 |
||
934 | * @param string $text_key Key in field's text array |
||
935 | * @param string $fallback Fallback text |
||
936 | * @return string Text |
||
937 | */ |
||
938 | 8 | public function string( $text_key, $fallback ) { |
|
962 | |||
963 | /** |
||
964 | * Retrieve options args. Calls options_cb if it exists. |
||
965 | * @since 2.0.0 |
||
966 | * @param string $key Specific option to retrieve |
||
967 | * @return array Array of options |
||
968 | */ |
||
969 | 31 | public function options( $key = '' ) { |
|
994 | |||
995 | /** |
||
996 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
997 | * |
||
998 | * @since 0.2.2 |
||
999 | * |
||
1000 | * @return mixed Default field value |
||
1001 | */ |
||
1002 | 33 | public function get_default() { |
|
1015 | |||
1016 | /** |
||
1017 | * Fills in empty field parameters with defaults |
||
1018 | * @since 1.1.0 |
||
1019 | * @param array $args Metabox field config array |
||
1020 | */ |
||
1021 | 109 | public function _set_field_defaults( $args, $blah ) { |
|
1122 | |||
1123 | /** |
||
1124 | * Get default field arguments specific to this CMB2 object. |
||
1125 | * @since 2.2.0 |
||
1126 | * @param array $field_args Metabox field config array. |
||
1127 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1128 | * @return array Array of field arguments. |
||
1129 | */ |
||
1130 | 5 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1141 | |||
1142 | /** |
||
1143 | * Returns a cloned version of this field object with, but with |
||
1144 | * modified/overridden field arguments. |
||
1145 | * |
||
1146 | * @since 2.2.2 |
||
1147 | * @param array $field_args Array of field arguments, or entire array of |
||
1148 | * arguments for CMB2_Field |
||
1149 | * |
||
1150 | * @return CMB2_Field The new CMB2_Field instance. |
||
1151 | */ |
||
1152 | 5 | public function get_field_clone( $field_args ) { |
|
1155 | |||
1156 | /** |
||
1157 | * Returns the CMB2 instance this field is registered to. |
||
1158 | * |
||
1159 | * @since 2.2.2 |
||
1160 | * |
||
1161 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1162 | */ |
||
1163 | 1 | public function get_cmb() { |
|
1170 | |||
1171 | } |
||
1172 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.