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_Base 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_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class CMB2_Base { |
||
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * Current CMB2 instance ID |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | * @since 2.2.3 |
||
| 25 | */ |
||
| 26 | protected $cmb_id = ''; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The object properties name. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | * @since 2.2.3 |
||
| 33 | */ |
||
| 34 | protected $properties_name = 'meta_box'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Object ID |
||
| 38 | * |
||
| 39 | * @var mixed |
||
| 40 | * @since 2.2.3 |
||
| 41 | */ |
||
| 42 | protected $object_id = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Type of object being handled. (e.g., post, user, comment, or term) |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | * @since 2.2.3 |
||
| 49 | */ |
||
| 50 | protected $object_type = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Array of key => value data for saving. Likely $_POST data. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | * @since 2.2.3 |
||
| 57 | */ |
||
| 58 | public $data_to_save = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Array of field param callback results |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | * @since 2.0.0 |
||
| 65 | */ |
||
| 66 | protected $callback_results = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The deprecated_param method deprecated param message signature. |
||
| 70 | */ |
||
| 71 | const DEPRECATED_PARAM = 1; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The deprecated_param method deprecated callback param message signature. |
||
| 75 | */ |
||
| 76 | const DEPRECATED_CB_PARAM = 2; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Get started |
||
| 80 | * |
||
| 81 | * @since 2.2.3 |
||
| 82 | * @param array $args Object properties array |
||
| 83 | */ |
||
| 84 | public function __construct( $args = array() ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the object ID |
||
| 102 | * |
||
| 103 | * @since 2.2.3 |
||
| 104 | * @param integer $object_id Object ID |
||
| 105 | * @return integer Object ID |
||
| 106 | */ |
||
| 107 | 6 | public function object_id( $object_id = 0 ) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the object type |
||
| 117 | * |
||
| 118 | * @since 2.2.3 |
||
| 119 | * @param string $object_type Object Type |
||
| 120 | * @return string Object type |
||
| 121 | */ |
||
| 122 | 6 | public function object_type( $object_type = '' ) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get the object type for the current page, based on the $pagenow global. |
||
| 132 | * |
||
| 133 | * @since 2.2.2 |
||
| 134 | * @return string Page object type name. |
||
| 135 | */ |
||
| 136 | public function current_object_type() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set object property. |
||
| 157 | * |
||
| 158 | * @since 2.2.2 |
||
| 159 | * @param string $property Metabox config property to retrieve |
||
| 160 | * @param mixed $value Value to set if no value found |
||
| 161 | * @return mixed Metabox config property value or false |
||
| 162 | */ |
||
| 163 | public function set_prop( $property, $value ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get object property and optionally set a fallback |
||
| 171 | * |
||
| 172 | * @since 2.0.0 |
||
| 173 | * @param string $property Metabox config property to retrieve |
||
| 174 | * @param mixed $fallback Fallback value to set if no value found |
||
| 175 | * @return mixed Metabox config property value or false |
||
| 176 | */ |
||
| 177 | 43 | public function prop( $property, $fallback = null ) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get default field arguments specific to this CMB2 object. |
||
| 187 | * |
||
| 188 | * @since 2.2.0 |
||
| 189 | * @param array $field_args Metabox field config array. |
||
| 190 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 191 | * @return array Array of field arguments. |
||
| 192 | */ |
||
| 193 | 7 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
| 210 | |||
| 211 | /** |
||
| 212 | * Get a new field object specific to this CMB2 object. |
||
| 213 | * |
||
| 214 | * @since 2.2.0 |
||
| 215 | * @param array $field_args Metabox field config array. |
||
| 216 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 217 | * @return CMB2_Field CMB2_Field object |
||
| 218 | */ |
||
| 219 | 7 | protected function get_new_field( $field_args, $field_group = null ) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
| 225 | * |
||
| 226 | * @since 2.0.9 |
||
| 227 | * |
||
| 228 | * @return bool Whether this cmb should be shown. |
||
| 229 | */ |
||
| 230 | 43 | public function should_show() { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Displays the results of the param callbacks. |
||
| 244 | * |
||
| 245 | * @since 2.0.0 |
||
| 246 | * @param string $param Field parameter |
||
| 247 | */ |
||
| 248 | 80 | public function peform_param_callback( $param ) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Store results of the param callbacks for continual access |
||
| 254 | * |
||
| 255 | * @since 2.0.0 |
||
| 256 | * @param string $param Field parameter |
||
| 257 | * @return mixed Results of param/param callback |
||
| 258 | */ |
||
| 259 | 86 | public function get_param_callback_result( $param ) { |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Handles the parameter callbacks, and passes this object as parameter. |
||
| 293 | * |
||
| 294 | * @since 2.2.3 |
||
| 295 | * @param callable $cb The callback method/function/closure |
||
| 296 | * @return mixed Return of the callback function. |
||
| 297 | */ |
||
| 298 | 49 | protected function do_callback( $cb ) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Checks if field has a callback value |
||
| 304 | * |
||
| 305 | * @since 1.0.1 |
||
| 306 | * @param string $cb Callback string |
||
| 307 | * @return mixed NULL, false for NO validation, or $cb string if it exists. |
||
| 308 | */ |
||
| 309 | 100 | public function maybe_callback( $cb ) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Checks if this object has parameter corresponding to the given filter |
||
| 332 | * which is callable. If so, it registers the callback, and if not, |
||
| 333 | * converts the maybe-modified $val to a boolean for return. |
||
| 334 | * |
||
| 335 | * The registered handlers will have a parameter name which matches the filter, except: |
||
| 336 | * - The 'cmb2_api' prefix will be removed |
||
| 337 | * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). |
||
| 338 | * |
||
| 339 | * @since 2.2.3 |
||
| 340 | * |
||
| 341 | * @param string $hook_name The hook name. |
||
| 342 | * @param bool $val The default value. |
||
| 343 | * @param string $hook_function The hook function. Default: 'add_filter' |
||
| 344 | * |
||
| 345 | * @return null|bool Null if hook is registered, or bool for value. |
||
| 346 | */ |
||
| 347 | public function maybe_hook_parameter( $hook_name, $val = null, $hook_function = 'add_filter' ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Checks if given value is callable, and registers the callback. |
||
| 361 | * If is non-callable, converts the $val to a boolean for return. |
||
| 362 | * |
||
| 363 | * @since 2.2.3 |
||
| 364 | * |
||
| 365 | * @param bool $val The default value. |
||
| 366 | * @param string $hook_name The hook name. |
||
| 367 | * @param string $hook_function The hook function. |
||
| 368 | * |
||
| 369 | * @return null|bool Null if hook is registered, or bool for value. |
||
| 370 | */ |
||
| 371 | public static function maybe_hook( $val, $hook_name, $hook_function ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Mark a param as deprecated and inform when it has been used. |
||
| 383 | * |
||
| 384 | * There is a default WordPress hook deprecated_argument_run that will be called |
||
| 385 | * that can be used to get the backtrace up to what file and function used the |
||
| 386 | * deprecated argument. |
||
| 387 | * |
||
| 388 | * The current behavior is to trigger a user error if WP_DEBUG is true. |
||
| 389 | * |
||
| 390 | * @since 2.2.3 |
||
| 391 | * |
||
| 392 | * @param string $function The function that was called. |
||
| 393 | * @param string $version The version of CMB2 that deprecated the argument used. |
||
| 394 | * @param string $message Optional. A message regarding the change, or numeric |
||
| 395 | * key to generate message from additional arguments. |
||
| 396 | * Default null. |
||
| 397 | */ |
||
| 398 | 1 | protected function deprecated_param( $function, $version, $message = null ) { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Magic getter for our object. |
||
| 456 | * |
||
| 457 | * @param string $field |
||
| 458 | * @throws Exception Throws an exception if the field is invalid. |
||
| 459 | * @return mixed |
||
| 460 | */ |
||
| 461 | 69 | public function __get( $field ) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Allows overloading the object with methods... Whooaaa oooh it's magic, y'knoooow. |
||
| 481 | * |
||
| 482 | * @since 1.0.0 |
||
| 483 | * @param string $method Non-existent method. |
||
| 484 | * @param array $args All arguments passed to the method |
||
| 485 | */ |
||
| 486 | 2 | public function __call( $method, $args ) { |
|
| 514 | } |
||
| 515 |
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.