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() { |
||
| 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 bollean. |
||
| 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 bollean. |
||
| 173 | */ |
||
| 174 | public 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 $options string of options for fields liek select. |
||
| 196 | */ |
||
| 197 | public function add_option_arg( $options ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Conditional to check if the field argument should be added.. |
||
| 212 | * |
||
| 213 | * @since 1.1.4 |
||
| 214 | * @param array $arg_value A CMB2 field type. |
||
| 215 | */ |
||
| 216 | public function add_strpos_arg( $arg_value ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Conditional to check if the field argument should be added.. |
||
| 230 | * |
||
| 231 | * @since 1.1.4 |
||
| 232 | * @param string $arg Field definition. |
||
| 233 | * @param string|array $value A CMB2 field type. |
||
| 234 | */ |
||
| 235 | public function add_arg( $arg, $value ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Conditional to check if the field argument should be added.. |
||
| 250 | * |
||
| 251 | * @since 1.1.4 |
||
| 252 | * @param array $field Field definition. |
||
| 253 | * @param string $field_type A CMB2 field type. |
||
| 254 | * @param string $field_key $field key to check. |
||
| 255 | * @return bollean. |
||
| 256 | */ |
||
| 257 | static function should_add_arg( $field, $field_type, $field_key ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Loop through user defined meta_box and creates the custom meta boxes and fields. |
||
| 264 | * |
||
| 265 | * @since 0.0.1 |
||
| 266 | */ |
||
| 267 | public function init_user_defined_meta_boxes_and_fields() { |
||
| 353 | } |
||
| 354 | } |
||
| 367 |
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