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 | * @var string |
||
23 | * @since 2.2.3 |
||
24 | */ |
||
25 | protected $cmb_id = ''; |
||
26 | |||
27 | /** |
||
28 | * The object properties name. |
||
29 | * @var string |
||
30 | * @since 2.2.3 |
||
31 | */ |
||
32 | protected $properties_name = 'meta_box'; |
||
33 | |||
34 | /** |
||
35 | * Object ID |
||
36 | * @var mixed |
||
37 | * @since 2.2.3 |
||
38 | */ |
||
39 | protected $object_id = 0; |
||
40 | |||
41 | /** |
||
42 | * Type of object being handled. (e.g., post, user, comment, or term) |
||
43 | * @var string |
||
44 | * @since 2.2.3 |
||
45 | */ |
||
46 | protected $object_type = ''; |
||
47 | |||
48 | /** |
||
49 | * Array of key => value data for saving. Likely $_POST data. |
||
50 | * @var array |
||
51 | * @since 2.2.3 |
||
52 | */ |
||
53 | public $data_to_save = array(); |
||
54 | |||
55 | /** |
||
56 | * Array of field param callback results |
||
57 | * @var array |
||
58 | * @since 2.0.0 |
||
59 | */ |
||
60 | protected $callback_results = array(); |
||
61 | |||
62 | /** |
||
63 | * The deprecated_param method deprecated param message signature. |
||
64 | */ |
||
65 | const DEPRECATED_PARAM = 1; |
||
66 | |||
67 | /** |
||
68 | * The deprecated_param method deprecated callback param message signature. |
||
69 | */ |
||
70 | const DEPRECATED_CB_PARAM = 2; |
||
71 | |||
72 | /** |
||
73 | * Get started |
||
74 | * @since 2.2.3 |
||
75 | * @param array $args Object properties array |
||
76 | */ |
||
77 | public function __construct( $args = array() ) { |
||
92 | |||
93 | /** |
||
94 | * Returns the object ID |
||
95 | * @since 2.2.3 |
||
96 | * @param integer $object_id Object ID |
||
97 | * @return integer Object ID |
||
98 | */ |
||
99 | 5 | public function object_id( $object_id = 0 ) { |
|
106 | |||
107 | /** |
||
108 | * Returns the object type |
||
109 | * @since 2.2.3 |
||
110 | * @param string $object_type Object Type |
||
111 | * @return string Object type |
||
112 | */ |
||
113 | 5 | public function object_type( $object_type = '' ) { |
|
120 | |||
121 | /** |
||
122 | * Get the object type for the current page, based on the $pagenow global. |
||
123 | * @since 2.2.2 |
||
124 | * @return string Page object type name. |
||
125 | */ |
||
126 | View Code Duplication | public function current_object_type() { |
|
144 | |||
145 | /** |
||
146 | * Set object property. |
||
147 | * @since 2.2.2 |
||
148 | * @param string $property Metabox config property to retrieve |
||
149 | * @param mixed $value Value to set if no value found |
||
150 | * @return mixed Metabox config property value or false |
||
151 | */ |
||
152 | public function set_prop( $property, $value ) { |
||
157 | |||
158 | /** |
||
159 | * Get object property and optionally set a fallback |
||
160 | * @since 2.0.0 |
||
161 | * @param string $property Metabox config property to retrieve |
||
162 | * @param mixed $fallback Fallback value to set if no value found |
||
163 | * @return mixed Metabox config property value or false |
||
164 | */ |
||
165 | 43 | public function prop( $property, $fallback = null ) { |
|
172 | |||
173 | /** |
||
174 | * Get default field arguments specific to this CMB2 object. |
||
175 | * @since 2.2.0 |
||
176 | * @param array $field_args Metabox field config array. |
||
177 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
178 | * @return array Array of field arguments. |
||
179 | */ |
||
180 | 6 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
181 | 6 | if ( $field_group ) { |
|
182 | $args = array( |
||
183 | 1 | 'field_args' => $field_args, |
|
184 | 1 | 'group_field' => $field_group, |
|
185 | 1 | ); |
|
186 | 1 | } else { |
|
187 | $args = array( |
||
188 | 5 | 'field_args' => $field_args, |
|
189 | 5 | 'object_type' => $this->object_type(), |
|
190 | 5 | 'object_id' => $this->object_id(), |
|
191 | 5 | 'cmb_id' => $this->cmb_id, |
|
192 | 5 | ); |
|
193 | } |
||
194 | |||
195 | 6 | return $args; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get a new field object specific to this CMB2 object. |
||
200 | * @since 2.2.0 |
||
201 | * @param array $field_args Metabox field config array. |
||
202 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
203 | * @return CMB2_Field CMB2_Field object |
||
204 | */ |
||
205 | 6 | protected function get_new_field( $field_args, $field_group = null ) { |
|
208 | |||
209 | /** |
||
210 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
211 | * |
||
212 | * @since 2.0.9 |
||
213 | * |
||
214 | * @return bool Whether this cmb should be shown. |
||
215 | */ |
||
216 | 43 | public function should_show() { |
|
227 | |||
228 | /** |
||
229 | * Displays the results of the param callbacks. |
||
230 | * |
||
231 | * @since 2.0.0 |
||
232 | * @param string $param Field parameter |
||
233 | */ |
||
234 | 80 | public function peform_param_callback( $param ) { |
|
237 | |||
238 | /** |
||
239 | * Store results of the param callbacks for continual access |
||
240 | * @since 2.0.0 |
||
241 | * @param string $param Field parameter |
||
242 | * @return mixed Results of param/param callback |
||
243 | */ |
||
244 | 85 | public function get_param_callback_result( $param ) { |
|
275 | |||
276 | /** |
||
277 | * Handles the parameter callbacks, and passes this object as parameter. |
||
278 | * @since 2.2.3 |
||
279 | * @param callable $cb The callback method/function/closure |
||
280 | * @return mixed Return of the callback function. |
||
281 | */ |
||
282 | 49 | protected function do_callback( $cb ) { |
|
285 | |||
286 | /** |
||
287 | * Checks if field has a callback value |
||
288 | * @since 1.0.1 |
||
289 | * @param string $cb Callback string |
||
290 | * @return mixed NULL, false for NO validation, or $cb string if it exists. |
||
291 | */ |
||
292 | 99 | public function maybe_callback( $cb ) { |
|
312 | |||
313 | /** |
||
314 | * Checks if this object has parameter corresponding to the given filter |
||
315 | * which is callable. If so, it registers the callback, and if not, |
||
316 | * converts the maybe-modified $val to a boolean for return. |
||
317 | * |
||
318 | * The registered handlers will have a parameter name which matches the filter, except: |
||
319 | * - The 'cmb2_api' prefix will be removed |
||
320 | * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). |
||
321 | * |
||
322 | * @since 2.2.3 |
||
323 | * |
||
324 | * @param string $hook_name The hook name. |
||
325 | * @param bool $val The default value. |
||
326 | * @param string $hook_function The hook function. Default: 'add_filter' |
||
327 | * |
||
328 | * @return null|bool Null if hook is registered, or bool for value. |
||
329 | */ |
||
330 | public function maybe_hook_parameter( $hook_name, $val = null, $hook_function = 'add_filter' ) { |
||
341 | |||
342 | /** |
||
343 | * Checks if given value is callable, and registers the callback. |
||
344 | * If is non-callable, converts the $val to a boolean for return. |
||
345 | * |
||
346 | * @since 2.2.3 |
||
347 | * |
||
348 | * @param bool $val The default value. |
||
349 | * @param string $hook_name The hook name. |
||
350 | * @param string $hook_function The hook function. |
||
351 | * |
||
352 | * @return null|bool Null if hook is registered, or bool for value. |
||
353 | */ |
||
354 | public static function maybe_hook( $val, $hook_name, $hook_function ) { |
||
363 | |||
364 | /** |
||
365 | * Mark a param as deprecated and inform when it has been used. |
||
366 | * |
||
367 | * There is a default WordPress hook deprecated_argument_run that will be called |
||
368 | * that can be used to get the backtrace up to what file and function used the |
||
369 | * deprecated argument. |
||
370 | * |
||
371 | * The current behavior is to trigger a user error if WP_DEBUG is true. |
||
372 | * |
||
373 | * @since 2.2.3 |
||
374 | * |
||
375 | * @param string $function The function that was called. |
||
376 | * @param string $version The version of CMB2 that deprecated the argument used. |
||
377 | * @param string $message Optional. A message regarding the change, or numeric |
||
378 | * key to generate message from additional arguments. |
||
379 | * Default null. |
||
380 | */ |
||
381 | 1 | protected function deprecated_param( $function, $version, $message = null ) { |
|
438 | |||
439 | /** |
||
440 | * Magic getter for our object. |
||
441 | * @param string $field |
||
442 | * @throws Exception Throws an exception if the field is invalid. |
||
443 | * @return mixed |
||
444 | */ |
||
445 | 69 | public function __get( $field ) { |
|
462 | |||
463 | /** |
||
464 | * Allows overloading the object with methods... Whooaaa oooh it's magic, y'knoooow. |
||
465 | * @since 1.0.0 |
||
466 | * @param string $method Non-existent method. |
||
467 | * @param array $args All arguments passed to the method |
||
468 | */ |
||
469 | 2 | public function __call( $method, $args ) { |
|
497 | } |
||
498 |
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.