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_Meta_Box 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_Meta_Box, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class CMB2_Meta_Box { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Field prefix. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $prefix = '_cmb2_'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Current field array. |
||
| 29 | * Store the current field while adding user defined fields |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $field = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Current field arguments array. |
||
| 37 | * Store the current field arguments while adding user defined fields |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $field_args = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Instance of this class. |
||
| 45 | * |
||
| 46 | * @var object |
||
| 47 | */ |
||
| 48 | protected static $instance; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Initiate CMB2 Admin Extension object. |
||
| 52 | * |
||
| 53 | * @since 0.0.1 |
||
| 54 | */ |
||
| 55 | public function __construct() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return an instance of this class. |
||
| 64 | * |
||
| 65 | * @return object A single instance of this class. |
||
| 66 | */ |
||
| 67 | public static function get_instance() { |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Determine if current user has permission to CMB2 view plugins. |
||
| 79 | * |
||
| 80 | * @since 0.0.1 |
||
| 81 | */ |
||
| 82 | public function is_cmb2_allowed() { |
||
| 83 | |||
| 84 | $cmb2_settings = get_option( '_cmb2_settings' ); |
||
| 85 | |||
| 86 | if ( empty( $cmb2_settings ) || current_user_can( 'administrator' ) ) { |
||
| 87 | // No settings saved. |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | $current_user = wp_get_current_user(); |
||
| 92 | $allowed_users = isset( $cmb2_settings['_cmb2_user_multicheckbox'] ) ? $cmb2_settings['_cmb2_user_multicheckbox'] : array(); |
||
| 93 | |||
| 94 | return empty( $allowed_users ) || in_array( $current_user->ID, $allowed_users, true ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Only show CMB2 plugins to users defined in settings. |
||
| 99 | * |
||
| 100 | * @since 0.0.1 |
||
| 101 | */ |
||
| 102 | public function hide_cmb2_plugins() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Enqueue CMB2 Admin Extension scripts and styles. |
||
| 118 | * |
||
| 119 | * @since 0.0.8 |
||
| 120 | */ |
||
| 121 | public function enqueue_scripts() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check if the field type is repeatable. |
||
| 134 | * |
||
| 135 | * @since 0.0.6 |
||
| 136 | * @param string $field_type A CMB2 field type. |
||
| 137 | * @return boolean. |
||
| 138 | */ |
||
| 139 | public function is_repeatable( $field_type ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Check if field types should have the options argument. |
||
| 169 | * |
||
| 170 | * @since 0.0.6 |
||
| 171 | * @param string $field_type A CMB2 field type. |
||
| 172 | * @return boolean. |
||
| 173 | */ |
||
| 174 | private function has_options( $field_type ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Conditional to check if the field argument should be added.. |
||
| 193 | * |
||
| 194 | * @since 1.1.4 |
||
| 195 | * @param string $field_options String of options for fields liek select. |
||
| 196 | */ |
||
| 197 | private function add_option_arg( $field_options ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Conditional to check if the field argument should be added.. |
||
| 213 | * |
||
| 214 | * @since 1.1.4 |
||
| 215 | * @param array $arg_value A CMB2 field type. |
||
| 216 | */ |
||
| 217 | public function add_strpos_arg( $arg_value ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add the field argument. |
||
| 231 | * |
||
| 232 | * @since 1.1.4 |
||
| 233 | * @param string $arg Field definition. |
||
| 234 | * @param string|array $value A CMB2 field type. |
||
| 235 | */ |
||
| 236 | public function add_arg( $arg, $value ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Conditional to check if the field argument should be added. |
||
| 251 | * |
||
| 252 | * @since 0.1.4 |
||
| 253 | * @param array $field Field definition. |
||
| 254 | * @param string $field_type A CMB2 field type. |
||
| 255 | * @param string $field_key Field key to check. |
||
| 256 | * @return boolean. |
||
| 257 | */ |
||
| 258 | public static function should_add_arg( $field, $field_type, $field_key ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add the current field to the metabox. |
||
| 265 | * |
||
| 266 | * @since 0.2.0 |
||
| 267 | * @param array $field Field definition. |
||
| 268 | * @param object $metabox Metabox object. |
||
| 269 | * @param mixed $group_field_id If is reaptable group this is the ID, and false otherwise. |
||
| 270 | */ |
||
| 271 | public function add_field( $field, $metabox, $group_field_id ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Covert the checkbox value of 'on' / '' to boolean. |
||
| 337 | * |
||
| 338 | * @since 0.2.0 |
||
| 339 | * @param string $meta Sting value of checkbox on. |
||
| 340 | */ |
||
| 341 | public function string_to_bool( $meta ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get the metabox post meta. |
||
| 348 | * |
||
| 349 | * @since 0.2.0 |
||
| 350 | * @param object $user_meta_box Post object. |
||
| 351 | */ |
||
| 352 | public function get_meta_data( $user_meta_box ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Loop through user defined meta_box and creates the custom meta boxes and fields. |
||
| 374 | * |
||
| 375 | * @since 0.0.1 |
||
| 376 | */ |
||
| 377 | public function init_user_defined_meta_boxes_and_fields() { |
||
| 434 | } |
||
| 435 | } |
||
| 448 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state