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 | * |
||
| 22 | * @var string |
||
| 23 | * @since 2.2.3 |
||
| 24 | */ |
||
| 25 | protected $properties_name = 'args'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Field arguments |
||
| 29 | * |
||
| 30 | * @var mixed |
||
| 31 | * @since 1.1.0 |
||
| 32 | */ |
||
| 33 | public $args = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Field group object or false (if no group) |
||
| 37 | * |
||
| 38 | * @var mixed |
||
| 39 | * @since 1.1.0 |
||
| 40 | */ |
||
| 41 | public $group = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Field meta value |
||
| 45 | * |
||
| 46 | * @var mixed |
||
| 47 | * @since 1.1.0 |
||
| 48 | */ |
||
| 49 | public $value = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Field meta value |
||
| 53 | * |
||
| 54 | * @var mixed |
||
| 55 | * @since 1.1.0 |
||
| 56 | */ |
||
| 57 | public $escaped_value = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Grouped Field's current numeric index during the save process |
||
| 61 | * |
||
| 62 | * @var mixed |
||
| 63 | * @since 2.0.0 |
||
| 64 | */ |
||
| 65 | public $index = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Array of field options |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | * @since 2.0.0 |
||
| 72 | */ |
||
| 73 | protected $field_options = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Array of provided field text strings |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | * @since 2.0.0 |
||
| 80 | */ |
||
| 81 | protected $strings; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | * @since 2.2.2 |
||
| 88 | */ |
||
| 89 | public $render_context = 'edit'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * All CMB2_Field callable field arguments. |
||
| 93 | * Can be used to determine if a field argument is callable. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | public static $callable_fields = array( |
||
| 98 | 'default_cb', |
||
| 99 | 'classes_cb', |
||
| 100 | 'options_cb', |
||
| 101 | 'text_cb', |
||
| 102 | 'label_cb', |
||
| 103 | 'render_row_cb', |
||
| 104 | 'display_cb', |
||
| 105 | 'before_group', |
||
| 106 | 'before_group_row', |
||
| 107 | 'before_row', |
||
| 108 | 'before', |
||
| 109 | 'before_field', |
||
| 110 | 'after_field', |
||
| 111 | 'after', |
||
| 112 | 'after_row', |
||
| 113 | 'after_group_row', |
||
| 114 | 'after_group', |
||
| 115 | ); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Constructs our field object |
||
| 119 | * |
||
| 120 | * @since 1.1.0 |
||
| 121 | * @param array $args Field arguments |
||
| 122 | */ |
||
| 123 | 111 | public function __construct( $args ) { |
|
| 124 | |||
| 125 | 111 | if ( ! empty( $args['group_field'] ) ) { |
|
| 126 | 4 | $this->group = $args['group_field']; |
|
| 127 | 4 | $this->object_id = $this->group->object_id; |
|
| 128 | 4 | $this->object_type = $this->group->object_type; |
|
| 129 | 4 | $this->cmb_id = $this->group->cmb_id; |
|
| 130 | 4 | } else { |
|
| 131 | 111 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
|
| 132 | 111 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
|
| 133 | |||
| 134 | 111 | if ( isset( $args['cmb_id'] ) ) { |
|
| 135 | 43 | $this->cmb_id = $args['cmb_id']; |
|
| 136 | 43 | } |
|
| 137 | } |
||
| 138 | |||
| 139 | 111 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
|
| 140 | |||
| 141 | 111 | if ( $this->object_id ) { |
|
| 142 | 103 | $this->value = $this->get_data(); |
|
| 143 | 103 | } |
|
| 144 | 111 | } |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Non-existent methods fallback to checking for field arguments of the same name |
||
| 148 | * |
||
| 149 | * @since 1.1.0 |
||
| 150 | * @param string $name Method name |
||
| 151 | * @param array $arguments Array of passed-in arguments |
||
| 152 | * @return mixed Value of field argument |
||
| 153 | */ |
||
| 154 | 100 | public function __call( $name, $arguments ) { |
|
| 155 | 100 | if ( 'string' === $name ) { |
|
| 156 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
| 157 | } |
||
| 158 | |||
| 159 | 100 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
|
| 160 | 100 | return $this->args( $name, $key ); |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieves the field id |
||
| 165 | * |
||
| 166 | * @since 1.1.0 |
||
| 167 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
| 168 | * @return string Field id |
||
| 169 | */ |
||
| 170 | 109 | public function id( $raw = false ) { |
|
| 171 | 109 | $id = $raw ? '_id' : 'id'; |
|
| 172 | 109 | return $this->args( $id ); |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get a field argument |
||
| 177 | * |
||
| 178 | * @since 1.1.0 |
||
| 179 | * @param string $key Argument to check |
||
| 180 | * @param string $_key Sub argument to check |
||
| 181 | * @return mixed Argument value or false if non-existent |
||
| 182 | */ |
||
| 183 | 112 | public function args( $key = '', $_key = '' ) { |
|
| 184 | 112 | $arg = $this->_data( 'args', $key ); |
|
| 185 | |||
| 186 | 112 | if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { |
|
| 187 | |||
| 188 | 1 | $arg = $this->get_default(); |
|
| 189 | |||
| 190 | 112 | } elseif ( $_key ) { |
|
| 191 | |||
| 192 | $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; |
||
| 193 | } |
||
| 194 | |||
| 195 | 112 | return $arg; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Retrieve a portion of a field property |
||
| 200 | * |
||
| 201 | * @since 1.1.0 |
||
| 202 | * @param string $var Field property to check |
||
| 203 | * @param string $key Field property array key to check |
||
| 204 | * @return mixed Queried property value or false |
||
| 205 | */ |
||
| 206 | 112 | public function _data( $var, $key = '' ) { |
|
| 207 | 112 | $vars = $this->{$var}; |
|
| 208 | 112 | if ( $key ) { |
|
| 209 | 112 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
|
| 210 | } |
||
| 211 | 62 | return $vars; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get Field's value |
||
| 216 | * |
||
| 217 | * @since 1.1.0 |
||
| 218 | * @param string $key If value is an array, is used to get array key->value |
||
| 219 | * @return mixed Field value or false if non-existent |
||
| 220 | */ |
||
| 221 | 47 | public function value( $key = '' ) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Retrieves metadata/option data |
||
| 227 | * |
||
| 228 | * @since 1.0.1 |
||
| 229 | * @param string $field_id Meta key/Option array key |
||
| 230 | * @param array $args Override arguments |
||
| 231 | * @return mixed Meta/Option value |
||
| 232 | */ |
||
| 233 | 107 | public function get_data( $field_id = '', $args = array() ) { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Updates metadata/option data |
||
| 299 | * |
||
| 300 | * @since 1.0.1 |
||
| 301 | * @param mixed $new_value Value to update data with |
||
| 302 | * @param bool $single Whether data is an array (add_metadata) |
||
| 303 | */ |
||
| 304 | 13 | public function update_data( $new_value, $single = true ) { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Removes/updates metadata/option data |
||
| 373 | * |
||
| 374 | * @since 1.0.1 |
||
| 375 | 3 | * @param string $old Old value |
|
| 376 | 3 | */ |
|
| 377 | public function remove_data( $old = '' ) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Data variables for get/set data methods |
||
| 438 | 107 | * |
|
| 439 | 107 | * @since 1.1.0 |
|
| 440 | 107 | * @param array $args Override arguments |
|
| 441 | 107 | * @return array Updated arguments |
|
| 442 | 107 | */ |
|
| 443 | 107 | public function data_args( $args = array() ) { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Checks if field has a registered sanitization callback |
||
| 456 | 15 | * |
|
| 457 | * @since 1.0.1 |
||
| 458 | 15 | * @param mixed $meta_value Meta value |
|
| 459 | * @return mixed Possibly sanitized meta value |
||
| 460 | 2 | */ |
|
| 461 | 2 | public function sanitization_cb( $meta_value ) { |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Process $_POST data to save this field's value |
||
| 507 | 3 | * |
|
| 508 | 3 | * @since 2.0.3 |
|
| 509 | * @param array $data_to_save $_POST data to check |
||
| 510 | 3 | * @return array|int|bool Result of save, false on failure |
|
| 511 | 3 | */ |
|
| 512 | 3 | public function save_field_from_data( array $data_to_save ) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Sanitize/store a value to this field |
||
| 524 | 13 | * |
|
| 525 | * @since 2.0.0 |
||
| 526 | 13 | * @param array $meta_value Desired value to sanitize/store |
|
| 527 | 13 | * @return array|int|bool Result of save. false on failure |
|
| 528 | 13 | */ |
|
| 529 | public function save_field( $meta_value ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | 47 | * Determine if current type is exempt from escaping |
|
| 602 | * |
||
| 603 | 47 | * @since 1.1.0 |
|
| 604 | 47 | * @return bool True if exempt |
|
| 605 | 47 | */ |
|
| 606 | 47 | public function escaping_exception() { |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Determine if current type cannot be repeatable |
||
| 617 | 5 | * |
|
| 618 | * @since 1.1.0 |
||
| 619 | * @param string $type Field type to check |
||
| 620 | * @return bool True if type cannot be repeatable |
||
| 621 | 5 | */ |
|
| 622 | 5 | public function repeatable_exception( $type ) { |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Escape the value before output. Defaults to 'esc_attr()' |
||
| 654 | * |
||
| 655 | 47 | * @since 1.0.1 |
|
| 656 | * @param callable $func Escaping function (if not esc_attr()) |
||
| 657 | 47 | * @param mixed $meta_value Meta value |
|
| 658 | 28 | * @return mixed Final value |
|
| 659 | */ |
||
| 660 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return non-empty value or field default if value IS empty |
||
| 703 | 47 | * |
|
| 704 | 47 | * @since 2.0.0 |
|
| 705 | * @param mixed $meta_value Field value |
||
| 706 | * @return mixed Field value, or default value |
||
| 707 | */ |
||
| 708 | public function val_or_default( $meta_value ) { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Offset a time value based on timezone |
||
| 714 | * |
||
| 715 | * @since 1.0.0 |
||
| 716 | * @return string Offset time string |
||
| 717 | */ |
||
| 718 | public function field_timezone_offset() { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Return timezone string |
||
| 724 | * |
||
| 725 | * @since 1.0.0 |
||
| 726 | * @return string Timezone string |
||
| 727 | */ |
||
| 728 | public function field_timezone() { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Format the timestamp field value based on the field date/time format arg |
||
| 745 | 10 | * |
|
| 746 | 10 | * @since 2.0.0 |
|
| 747 | * @param int $meta_value Timestamp |
||
| 748 | * @param string $format Either date_format or time_format |
||
| 749 | * @return string Formatted date |
||
| 750 | */ |
||
| 751 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Return a formatted timestamp for a field |
||
| 757 | 10 | * |
|
| 758 | 10 | * @since 2.0.0 |
|
| 759 | 10 | * @param string $format Either date_format or time_format |
|
| 760 | * @param string $meta_value Optional meta value to check |
||
| 761 | 10 | * @return string Formatted date |
|
| 762 | */ |
||
| 763 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Get timestamp from text date |
||
| 778 | * |
||
| 779 | * @since 2.2.0 |
||
| 780 | * @param string $value Date value |
||
| 781 | * @return mixed Unix timestamp representing the date. |
||
| 782 | */ |
||
| 783 | public function get_timestamp_from_value( $value ) { |
||
| 786 | 10 | ||
| 787 | 10 | /** |
|
| 788 | * Get field render callback and Render the field row |
||
| 789 | 10 | * |
|
| 790 | * @since 1.0.0 |
||
| 791 | */ |
||
| 792 | 10 | public function render_field() { |
|
| 800 | 9 | ||
| 801 | /** |
||
| 802 | * Default field render callback |
||
| 803 | 9 | * |
|
| 804 | * @since 2.1.1 |
||
| 805 | */ |
||
| 806 | public function render_field_callback() { |
||
| 850 | |||
| 851 | 9 | /** |
|
| 852 | 9 | * The default label_cb callback (if not a title field) |
|
| 853 | * |
||
| 854 | * @since 2.1.1 |
||
| 855 | * @return string Label html markup |
||
| 856 | 9 | */ |
|
| 857 | public function label() { |
||
| 866 | |||
| 867 | 45 | /** |
|
| 868 | * Defines the classes for the current CMB2 field row |
||
| 869 | 45 | * |
|
| 870 | * @since 2.0.0 |
||
| 871 | * @return string Space concatenated list of classes |
||
| 872 | */ |
||
| 873 | public function row_classes() { |
||
| 924 | |||
| 925 | |||
| 926 | 33 | ||
| 927 | 33 | /** |
|
| 928 | * Get field display callback and render the display value in the column. |
||
| 929 | 33 | * |
|
| 930 | * @since 2.2.2 |
||
| 931 | */ |
||
| 932 | 33 | public function render_column() { |
|
| 940 | 33 | ||
| 941 | /** |
||
| 942 | 33 | * Default callback to outputs field value in a display format. |
|
| 943 | * |
||
| 944 | * @since 2.2.2 |
||
| 945 | */ |
||
| 946 | 33 | public function display_value_callback() { |
|
| 989 | |||
| 990 | /** |
||
| 991 | 2 | * Replaces a hash key - {#} - with the repeatable index |
|
| 992 | * |
||
| 993 | 2 | * @since 1.2.0 |
|
| 994 | * @param string $value Value to update |
||
| 995 | * @return string Updated value |
||
| 996 | */ |
||
| 997 | public function replace_hash( $value ) { |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
| 1004 | * For back-compatibility, falls back to checking the options array. |
||
| 1005 | 10 | * |
|
| 1006 | * @since 2.2.2 |
||
| 1007 | 10 | * @param string $text_key Key in field's text array |
|
| 1008 | 10 | * @param string $fallback Fallback text |
|
| 1009 | * @return string Text |
||
| 1010 | 10 | */ |
|
| 1011 | public function get_string( $text_key, $fallback ) { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | 32 | * Retrieve options args. Calls options_cb if it exists. |
|
| 1038 | 32 | * |
|
| 1039 | 5 | * @since 2.0.0 |
|
| 1040 | 5 | * @param string $key Specific option to retrieve |
|
| 1041 | * @return array Array of options |
||
| 1042 | */ |
||
| 1043 | 1 | public function options( $key = '' ) { |
|
| 1068 | |||
| 1069 | 13 | /** |
|
| 1070 | 13 | * Store JS dependencies as part of the field args. |
|
| 1071 | 13 | * |
|
| 1072 | 13 | * @since 2.2.0 |
|
| 1073 | * @param array $dependencies Dependies to register for this field. |
||
| 1074 | 13 | */ |
|
| 1075 | 13 | public function add_js_dependencies( $dependencies = array() ) { |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | 35 | * Get CMB2_Field default value, either from default param or default_cb param. |
|
| 1085 | 35 | * |
|
| 1086 | 8 | * @since 0.2.2 |
|
| 1087 | * |
||
| 1088 | * @return mixed Default field value |
||
| 1089 | 34 | */ |
|
| 1090 | 34 | public function get_default() { |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | 111 | * Fills in empty field parameters with defaults |
|
| 1106 | * |
||
| 1107 | * @since 1.1.0 |
||
| 1108 | 111 | * @param array $args Metabox field config array |
|
| 1109 | 111 | * @param array Modified field config array. |
|
| 1110 | 111 | */ |
|
| 1111 | 111 | public function _set_field_defaults( $args ) { |
|
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Get default field arguments specific to this CMB2 object. |
||
| 1209 | 7 | * |
|
| 1210 | 7 | * @since 2.2.0 |
|
| 1211 | * @param array $field_args Metabox field config array. |
||
| 1212 | 7 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
|
| 1213 | * @return array Array of field arguments. |
||
| 1214 | */ |
||
| 1215 | 7 | protected function get_default_args( $field_args, $field_group = null ) { |
|
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Returns a cloned version of this field object with, but with |
||
| 1229 | * modified/overridden field arguments. |
||
| 1230 | * |
||
| 1231 | 7 | * @since 2.2.2 |
|
| 1232 | 7 | * @param array $field_args Array of field arguments, or entire array of |
|
| 1233 | * arguments for CMB2_Field |
||
| 1234 | * |
||
| 1235 | * @return CMB2_Field The new CMB2_Field instance. |
||
| 1236 | */ |
||
| 1237 | public function get_field_clone( $field_args ) { |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | 1 | * Returns the CMB2 instance this field is registered to. |
|
| 1243 | 1 | * |
|
| 1244 | * @since 2.2.2 |
||
| 1245 | * |
||
| 1246 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
| 1247 | 1 | */ |
|
| 1248 | public function get_cmb() { |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | 111 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
|
| 1258 | * |
||
| 1259 | 111 | * @since 2.2.3 |
|
| 1260 | * @param array $args Metabox field config array. |
||
| 1261 | * @param array Modified field config array. |
||
| 1262 | */ |
||
| 1263 | protected function convert_deprecated_params( $args ) { |
||
| 1304 | |||
| 1305 | } |
||
| 1306 |
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.